From 1dedc19e1f0a2039d2ab8f55af6e27034b8dcde5 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 26 Feb 2021 11:05:20 -0500 Subject: [PATCH] fix(compiler-sfc): handle more edge cases in default rewrite --- .../__tests__/rewriteDefault.spec.ts | 51 +++++++++++++++++++ packages/compiler-sfc/src/rewriteDefault.ts | 12 ++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/packages/compiler-sfc/__tests__/rewriteDefault.spec.ts b/packages/compiler-sfc/__tests__/rewriteDefault.spec.ts index 8bce2ab88c0..a6b45e128ad 100644 --- a/packages/compiler-sfc/__tests__/rewriteDefault.spec.ts +++ b/packages/compiler-sfc/__tests__/rewriteDefault.spec.ts @@ -26,4 +26,55 @@ describe('compiler sfc: rewriteDefault', () => { const script = a" `) }) + + test('w/ comments', async () => { + expect(rewriteDefault(`// export default\nexport default {}`, 'script')) + .toMatchInlineSnapshot(` + "// export default + const script = {}" + `) + }) + + test('export default class', async () => { + expect(rewriteDefault(`export default class Foo {}`, 'script')) + .toMatchInlineSnapshot(` + "class Foo {} + const script = Foo" + `) + }) + + test('export default class w/ comments', async () => { + expect( + rewriteDefault(`// export default\nexport default class Foo {}`, 'script') + ).toMatchInlineSnapshot(` + "// export default + class Foo {} + const script = Foo" + `) + }) + + test('export default class w/ comments 2', async () => { + expect( + rewriteDefault( + `export default {}\n` + `// export default class Foo {}`, + 'script' + ) + ).toMatchInlineSnapshot(` + "const script = {} + // export default class Foo {}" + `) + }) + + test('export default class w/ comments 3', async () => { + expect( + rewriteDefault( + `/*\nexport default class Foo {}*/\n` + `export default class Bar {}`, + 'script' + ) + ).toMatchInlineSnapshot(` + "/* + export default class Foo {}*/ + const script = class Bar {}" + `) + }) }) diff --git a/packages/compiler-sfc/src/rewriteDefault.ts b/packages/compiler-sfc/src/rewriteDefault.ts index 6dc52fe4bf4..417c70c3aab 100644 --- a/packages/compiler-sfc/src/rewriteDefault.ts +++ b/packages/compiler-sfc/src/rewriteDefault.ts @@ -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 }