Skip to content

fix: require parent template after when condition evaluated as truthy #4330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2019
Merged
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
23 changes: 16 additions & 7 deletions packages/@vue/cli/lib/GeneratorAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,22 @@ function renderFile (name, data, ejsOptions) {
const parsed = yaml.loadFront(template)
const content = parsed.__content
let finalTemplate = content.trim() + `\n`

if (parsed.when) {
finalTemplate = (
`<%_ if (${parsed.when}) { _%>` +
finalTemplate +
`<%_ } _%>`
)

// use ejs.render to test the conditional expression
// if evaluated to falsy vaule, return early to avoid extra cost for extend expression
const result = ejs.render(finalTemplate, data, ejsOptions)
if (!result) {
return
}
}

if (parsed.extend) {
const extendPath = path.isAbsolute(parsed.extend)
? parsed.extend
Expand All @@ -437,13 +453,6 @@ function renderFile (name, data, ejsOptions) {
finalTemplate = finalTemplate.replace(parsed.replace, content.trim())
}
}
if (parsed.when) {
finalTemplate = (
`<%_ if (${parsed.when}) { _%>` +
finalTemplate +
`<%_ } _%>`
)
}
}

return ejs.render(finalTemplate, data, ejsOptions)
Expand Down