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

make sure <template> contains only one element #124

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/vueTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,20 @@ async function processStyle (styles, id, content, options) {
return outputs
}

function parseTemplate (code) {
function checkIfTemplateContainsMoreElements (node) {
let count = 0

node.content.childNodes.forEach((node) => {
// filter text/comment node
if (node.nodeName.indexOf('#') === -1) {
count++
}
})

return count > 1
}

function parseTemplate (id, code) {
debug('Parsing template....')
const fragment = parse5.parseFragment(code, { locationInfo: true })

Expand All @@ -180,6 +193,10 @@ function parseTemplate (code) {
continue
}

if (name === 'template' && checkIfTemplateContainsMoreElements(fragment.childNodes[i])) {
throw new Error(`[rollup-plugin-vue] Error in ${id}:\n <template> should only contain exactly one root element.`)
}

const start = fragment.childNodes[i].__location.startTag.endOffset
const end = fragment.childNodes[i].__location.endTag.startOffset

Expand Down Expand Up @@ -221,7 +238,7 @@ const hasScoped = function (styles) {
}, false)
}
export default async function vueTransform (code, id, options) {
const nodes = parseTemplate(code)
const nodes = parseTemplate(id, code)
const css = await processStyle(nodes.style, id, code, options, nodes)
const modules = getModules(css)
const scoped = hasScoped(css)
Expand Down