fix(compiler-sfc): handle more edge cases in default rewrite
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user