Skip to content
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

fix: do not throw when api.render is called from an anonymous function #5801

Merged
merged 1 commit into from Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/@vue/cli/__tests__/Generator.spec.js
Expand Up @@ -531,6 +531,26 @@ test('api: render fs directory', async () => {
expect(fs.readFileSync('/.vscode/config.json', 'utf-8')).toMatch('{}')
})

// #4774
test('api: call render inside an anonymous function', async () => {
const generator = new Generator('/', { plugins: [
{
id: 'test1',
apply: api => {
(() => {
api.render('./template', { m: 2 })
})()
},
options: {
n: 1
}
}
] })

await generator.generate()
expect(fs.readFileSync('/foo.js', 'utf-8')).toMatch('foo(1)')
})

test('api: render object', async () => {
const generator = new Generator('/', { plugins: [
{
Expand Down
13 changes: 12 additions & 1 deletion packages/@vue/cli/lib/GeneratorAPI.js
Expand Up @@ -457,7 +457,18 @@ function extractCallDir () {
const obj = {}
Error.captureStackTrace(obj)
const callSite = obj.stack.split('\n')[3]
const fileName = callSite.match(/\s\((.*):\d+:\d+\)$/)[1]

// the regexp for the stack when called inside a named function
const namedStackRegExp = /\s\((.*):\d+:\d+\)$/
// the regexp for the stack when called inside an anonymous
const anonymousStackRegExp = /at (.*):\d+:\d+$/

let matchResult = callSite.match(namedStackRegExp)
if (!matchResult) {
matchResult = callSite.match(anonymousStackRegExp)
}

const fileName = matchResult[1]
return path.dirname(fileName)
}

Expand Down