Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit a013526

Browse files
committed
make sure <template> contains only one element
1 parent 0027e9f commit a013526

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/vueTransform.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,20 @@ async function processStyle (styles, id, content, options) {
164164
return outputs
165165
}
166166

167-
function parseTemplate (code) {
167+
function checkIfTemplateContainsMoreElements(node) {
168+
let count = 0
169+
170+
node.content.childNodes.forEach(( node ) => {
171+
//filter text/comment node
172+
if ( node.nodeName.indexOf('#') === -1 ) {
173+
count++
174+
}
175+
})
176+
177+
return count > 1
178+
}
179+
180+
function parseTemplate (id, code) {
168181
debug('Parsing template....')
169182
const fragment = parse5.parseFragment(code, { locationInfo: true })
170183

@@ -180,6 +193,10 @@ function parseTemplate (code) {
180193
continue
181194
}
182195

196+
if (name === 'template' && checkIfTemplateContainsMoreElements(fragment.childNodes[i])) {
197+
throw new Error(`[rollup-plugin-vue] Error in ${id}:\n <template> should only contain exactly one root element.`)
198+
}
199+
183200
const start = fragment.childNodes[i].__location.startTag.endOffset
184201
const end = fragment.childNodes[i].__location.endTag.startOffset
185202

@@ -221,7 +238,7 @@ const hasScoped = function (styles) {
221238
}, false)
222239
}
223240
export default async function vueTransform (code, id, options) {
224-
const nodes = parseTemplate(code)
241+
const nodes = parseTemplate(id, code)
225242
const css = await processStyle(nodes.style, id, code, options, nodes)
226243
const modules = getModules(css)
227244
const scoped = hasScoped(css)

0 commit comments

Comments
 (0)