fix(compiler-sfc): handle more edge cases in default rewrite

This commit is contained in:
Evan You
2021-02-26 11:05:20 -05:00
parent 012dc5a303
commit 1dedc19e1f
2 changed files with 62 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ import MagicString from 'magic-string'
const defaultExportRE = /((?:^|\n|;)\s*)export(\s*)default/
const namedDefaultExportRE = /((?:^|\n|;)\s*)export(.+)as(\s*)default/
const exportDefaultClassRE = /((?:^|\n|;)\s*)export\s+default\s+class\s+([\w$]+)/
/**
* Utility for rewriting `export default` in a script block into a variable
@@ -17,7 +18,16 @@ export function rewriteDefault(
return input + `\nconst ${as} = {}`
}
const replaced = input.replace(defaultExportRE, `$1const ${as} =`)
let replaced: string | undefined
const classMatch = input.match(exportDefaultClassRE)
if (classMatch) {
replaced =
input.replace(exportDefaultClassRE, '$1class $2') +
`\nconst ${as} = ${classMatch[2]}`
} else {
replaced = input.replace(defaultExportRE, `$1const ${as} =`)
}
if (!hasDefaultExport(replaced)) {
return replaced
}