From 7ce73d6c59003af34cfb075eda820a93524728dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Wed, 9 Nov 2022 11:10:54 +0800 Subject: [PATCH 1/3] fix(compiler-sfc): analyze export default by AST closes #7038 --- .../__tests__/rewriteDefault.spec.ts | 97 ++++++++++++---- packages/compiler-sfc/src/compileScript.ts | 7 +- packages/compiler-sfc/src/index.ts | 2 +- packages/compiler-sfc/src/parse.ts | 2 + packages/compiler-sfc/src/rewriteDefault.ts | 104 +++++++++--------- 5 files changed, 134 insertions(+), 78 deletions(-) diff --git a/packages/compiler-sfc/__tests__/rewriteDefault.spec.ts b/packages/compiler-sfc/__tests__/rewriteDefault.spec.ts index 40561da17db..b15ba70d214 100644 --- a/packages/compiler-sfc/__tests__/rewriteDefault.spec.ts +++ b/packages/compiler-sfc/__tests__/rewriteDefault.spec.ts @@ -2,8 +2,9 @@ import { rewriteDefault } from '../src' describe('compiler sfc: rewriteDefault', () => { test('without export default', () => { - expect(rewriteDefault(`export a = {}`, 'script')).toMatchInlineSnapshot(` - "export a = {} + expect(rewriteDefault(`export const a = {}`, 'script')) + .toMatchInlineSnapshot(` + "export const a = {} const script = {}" `) }) @@ -14,6 +15,14 @@ describe('compiler sfc: rewriteDefault', () => { ).toMatchInlineSnapshot(`"const script = {}"`) }) + test('rewrite variable value default', () => { + expect(rewriteDefault(`export const foo = 'default'`, 'script')) + .toMatchInlineSnapshot(` + "export const foo = 'default' + const script = {}" + `) + }) + test('rewrite export named default', () => { expect( rewriteDefault( @@ -36,6 +45,18 @@ describe('compiler sfc: rewriteDefault', () => { export { a as b, a as c} const script = a" `) + + expect( + rewriteDefault( + `const a = 1 \n export { a as b } \n export { a as default, a as c }`, + 'script' + ) + ).toMatchInlineSnapshot(` + "const a = 1 + export { a as b } + export { a as c } + const script = a" + `) }) test('w/ comments', async () => { @@ -52,7 +73,7 @@ describe('compiler sfc: rewriteDefault', () => { ).toMatchInlineSnapshot(` "let App = {} export { - + } const _sfc_main = App" `) @@ -96,25 +117,25 @@ describe('compiler sfc: rewriteDefault', () => { expect( rewriteDefault(`export { default, foo } from './index.js'`, 'script') ).toMatchInlineSnapshot(` - "import { default as __VUE_DEFAULT__ } from './index.js' - export { foo } from './index.js' - const script = __VUE_DEFAULT__" + "import { default as __VUE_DEFAULT__ } from './index.js' + export { foo } from './index.js' + const script = __VUE_DEFAULT__" `) expect( rewriteDefault(`export { default , foo } from './index.js'`, 'script') ).toMatchInlineSnapshot(` - "import { default as __VUE_DEFAULT__ } from './index.js' - export { foo } from './index.js' - const script = __VUE_DEFAULT__" + "import { default as __VUE_DEFAULT__ } from './index.js' + export { foo } from './index.js' + const script = __VUE_DEFAULT__" `) expect( rewriteDefault(`export { foo, default } from './index.js'`, 'script') ).toMatchInlineSnapshot(` - "import { default as __VUE_DEFAULT__ } from './index.js' - export { foo, } from './index.js' - const script = __VUE_DEFAULT__" + "import { default as __VUE_DEFAULT__ } from './index.js' + export { foo, } from './index.js' + const script = __VUE_DEFAULT__" `) expect( @@ -123,9 +144,9 @@ describe('compiler sfc: rewriteDefault', () => { 'script' ) ).toMatchInlineSnapshot(` - "import { foo } from './index.js' - export { bar } from './index.js' - const script = foo" + "import { foo as __VUE_DEFAULT__ } from './index.js' + export { bar } from './index.js' + const script = __VUE_DEFAULT__" `) expect( @@ -134,9 +155,9 @@ describe('compiler sfc: rewriteDefault', () => { 'script' ) ).toMatchInlineSnapshot(` - "import { foo } from './index.js' - export { bar } from './index.js' - const script = foo" + "import { foo as __VUE_DEFAULT__ } from './index.js' + export { bar } from './index.js' + const script = __VUE_DEFAULT__" `) expect( @@ -145,9 +166,33 @@ describe('compiler sfc: rewriteDefault', () => { 'script' ) ).toMatchInlineSnapshot(` - "import { foo } from './index.js' - export { bar, } from './index.js' - const script = foo" + "import { foo as __VUE_DEFAULT__ } from './index.js' + export { bar, } from './index.js' + const script = __VUE_DEFAULT__" + `) + + expect( + rewriteDefault( + `export { foo as default } from './index.js' \n const foo = 1`, + 'script' + ) + ).toMatchInlineSnapshot(` + "import { foo as __VUE_DEFAULT__ } from './index.js' + export { } from './index.js' + const foo = 1 + const script = __VUE_DEFAULT__" + `) + + expect( + rewriteDefault( + `const a = 1 \nexport { a as default } from 'xxx'`, + 'script' + ) + ).toMatchInlineSnapshot(` + "import { a as __VUE_DEFAULT__ } from 'xxx' + const a = 1 + export { } from 'xxx' + const script = __VUE_DEFAULT__" `) }) @@ -196,8 +241,11 @@ describe('compiler sfc: rewriteDefault', () => { }) test('@Component\nexport default class', async () => { - expect(rewriteDefault(`@Component\nexport default class Foo {}`, 'script')) - .toMatchInlineSnapshot(` + expect( + rewriteDefault(`@Component\nexport default class Foo {}`, 'script', [ + 'decorators-legacy' + ]) + ).toMatchInlineSnapshot(` "@Component class Foo {} const script = Foo" @@ -208,7 +256,8 @@ describe('compiler sfc: rewriteDefault', () => { expect( rewriteDefault( `// export default\n@Component\nexport default class Foo {}`, - 'script' + 'script', + ['decorators-legacy'] ) ).toMatchInlineSnapshot(` "// export default diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index b7e4c0ea778..0d9de1d2bcd 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -53,7 +53,7 @@ import { } from './cssVars' import { compileTemplate, SFCTemplateCompileOptions } from './compileTemplate' import { warnOnce } from './warn' -import { rewriteDefault } from './rewriteDefault' +import { rewriteDefaultAST } from './rewriteDefault' import { createCache } from './cache' import { shouldTransform, transformAST } from '@vue/reactivity-transform' @@ -241,7 +241,9 @@ export function compileScript( } } if (cssVars.length) { - content = rewriteDefault(content, DEFAULT_VAR, plugins) + const s = new MagicString(content) + rewriteDefaultAST(scriptAst.body, s, DEFAULT_VAR) + content = s.toString() content += genNormalScriptCssVarsCode( cssVars, bindings, @@ -1699,6 +1701,7 @@ export function compileScript( return { ...scriptSetup, + s, bindings: bindingMetadata, imports: userImports, content: s.toString(), diff --git a/packages/compiler-sfc/src/index.ts b/packages/compiler-sfc/src/index.ts index c56b1266220..e42678de4bb 100644 --- a/packages/compiler-sfc/src/index.ts +++ b/packages/compiler-sfc/src/index.ts @@ -3,7 +3,7 @@ export { parse } from './parse' export { compileTemplate } from './compileTemplate' export { compileStyle, compileStyleAsync } from './compileStyle' export { compileScript } from './compileScript' -export { rewriteDefault } from './rewriteDefault' +export { rewriteDefault, rewriteDefaultAST } from './rewriteDefault' export { shouldTransform as shouldTransformRef, transform as transformRef, diff --git a/packages/compiler-sfc/src/parse.ts b/packages/compiler-sfc/src/parse.ts index 79065fc667e..b36d133eb10 100644 --- a/packages/compiler-sfc/src/parse.ts +++ b/packages/compiler-sfc/src/parse.ts @@ -12,6 +12,7 @@ import { TemplateCompiler } from './compileTemplate' import { parseCssVars } from './cssVars' import { createCache } from './cache' import { hmrShouldReload, ImportBinding } from './compileScript' +import MagicString from 'magic-string' export const DEFAULT_FILENAME = 'anonymous.vue' @@ -41,6 +42,7 @@ export interface SFCTemplateBlock extends SFCBlock { export interface SFCScriptBlock extends SFCBlock { type: 'script' + s: MagicString setup?: string | boolean bindings?: BindingMetadata imports?: Record diff --git a/packages/compiler-sfc/src/rewriteDefault.ts b/packages/compiler-sfc/src/rewriteDefault.ts index 3efd8cefac2..b7be27eb615 100644 --- a/packages/compiler-sfc/src/rewriteDefault.ts +++ b/packages/compiler-sfc/src/rewriteDefault.ts @@ -1,45 +1,40 @@ -import { parse, ParserPlugin } from '@babel/parser' +import { parse } from '@babel/parser' import MagicString from 'magic-string' +import type { ParserPlugin } from '@babel/parser' +import type { Identifier, Statement } from '@babel/types' -const defaultExportRE = /((?:^|\n|;)\s*)export(\s*)default/ -const namedDefaultExportRE = /((?:^|\n|;)\s*)export(.+)(?:as)?(\s*)default/s -const exportDefaultClassRE = - /((?:^|\n|;)\s*)export\s+default\s+class\s+([\w$]+)/ - -/** - * Utility for rewriting `export default` in a script block into a variable - * declaration so that we can inject things into it - */ export function rewriteDefault( input: string, as: string, parserPlugins?: ParserPlugin[] ): string { - if (!hasDefaultExport(input)) { - return input + `\nconst ${as} = {}` - } + const ast = parse(input, { + sourceType: 'module', + plugins: parserPlugins + }).program.body + const s = new MagicString(input) - let replaced: string | undefined + rewriteDefaultAST(ast, s, as) - 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 + return s.toString() +} + +/** + * Utility for rewriting `export default` in a script block into a variable + * declaration so that we can inject things into it + */ +export function rewriteDefaultAST( + ast: Statement[], + s: MagicString, + as: string +): void { + if (!hasDefaultExport(ast)) { + s.append(`\nconst ${as} = {}`) + return } // if the script somehow still contains `default export`, it probably has // multi-line comments or template strings. fallback to a full parse. - const s = new MagicString(input) - const ast = parse(input, { - sourceType: 'module', - plugins: parserPlugins - }).program.body ast.forEach(node => { if (node.type === 'ExportDefaultDeclaration') { if (node.declaration.type === 'ClassDeclaration') { @@ -48,8 +43,7 @@ export function rewriteDefault( } else { s.overwrite(node.start!, node.declaration.start!, `const ${as} = `) } - } - if (node.type === 'ExportNamedDeclaration') { + } else if (node.type === 'ExportNamedDeclaration') { for (const specifier of node.specifiers) { if ( specifier.type === 'ExportSpecifier' && @@ -58,56 +52,64 @@ export function rewriteDefault( ) { if (node.source) { if (specifier.local.name === 'default') { - const end = specifierEnd(input, specifier.local.end!, node.end!) s.prepend( `import { default as __VUE_DEFAULT__ } from '${node.source.value}'\n` ) - s.overwrite(specifier.start!, end, ``) + const end = specifierEnd(s, specifier.local.end!, node.end!) + s.remove(specifier.start!, end) s.append(`\nconst ${as} = __VUE_DEFAULT__`) continue } else { - const end = specifierEnd( - input, - specifier.exported.end!, - node.end! - ) s.prepend( - `import { ${input.slice( + `import { ${s.slice( specifier.local.start!, specifier.local.end! - )} } from '${node.source.value}'\n` + )} as __VUE_DEFAULT__ } from '${node.source.value}'\n` ) - s.overwrite(specifier.start!, end, ``) - s.append(`\nconst ${as} = ${specifier.local.name}`) + const end = specifierEnd(s, specifier.exported.end!, node.end!) + s.remove(specifier.start!, end) + s.append(`\nconst ${as} = __VUE_DEFAULT__`) continue } } - const end = specifierEnd(input, specifier.end!, node.end!) - s.overwrite(specifier.start!, end, ``) + + const end = specifierEnd(s, specifier.end!, node.end!) + s.remove(specifier.start!, end) s.append(`\nconst ${as} = ${specifier.local.name}`) } } } }) - return s.toString() } -export function hasDefaultExport(input: string): boolean { - return defaultExportRE.test(input) || namedDefaultExportRE.test(input) +export function hasDefaultExport(ast: Statement[]): boolean { + for (const stmt of ast) { + if (stmt.type === 'ExportDefaultDeclaration') { + return true + } else if ( + stmt.type === 'ExportNamedDeclaration' && + stmt.specifiers.some( + spec => (spec.exported as Identifier).name === 'default' + ) + ) { + return true + } + } + return false } -function specifierEnd(input: string, end: number, nodeEnd: number | null) { +function specifierEnd(s: MagicString, end: number, nodeEnd: number | null) { // export { default , foo } ... let hasCommas = false let oldEnd = end while (end < nodeEnd!) { - if (/\s/.test(input.charAt(end))) { + if (/\s/.test(s.slice(end, end + 1))) { end++ - } else if (input.charAt(end) === ',') { + } else if (s.slice(end, end + 1) === ',') { end++ hasCommas = true break - } else if (input.charAt(end) === '}') { + } else if (s.slice(end, end + 1) === '}') { break } } From 5c65d4d3bbb6f6b5ee408169923e9279fe14b3fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Sun, 5 Feb 2023 12:55:10 +0800 Subject: [PATCH 2/3] test: update snapshot --- packages/compiler-sfc/__tests__/rewriteDefault.spec.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/compiler-sfc/__tests__/rewriteDefault.spec.ts b/packages/compiler-sfc/__tests__/rewriteDefault.spec.ts index b15ba70d214..49110436c6e 100644 --- a/packages/compiler-sfc/__tests__/rewriteDefault.spec.ts +++ b/packages/compiler-sfc/__tests__/rewriteDefault.spec.ts @@ -246,8 +246,7 @@ describe('compiler sfc: rewriteDefault', () => { 'decorators-legacy' ]) ).toMatchInlineSnapshot(` - "@Component - class Foo {} + "class Foo {} const script = Foo" `) }) @@ -261,7 +260,6 @@ describe('compiler sfc: rewriteDefault', () => { ) ).toMatchInlineSnapshot(` "// export default - @Component class Foo {} const script = Foo" `) From 3e2a46d30ef0891b1a117d9dc242a59dce138033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Sun, 5 Feb 2023 23:46:22 +0800 Subject: [PATCH 3/3] fix: decorator --- .../__tests__/rewriteDefault.spec.ts | 16 ++++++++-------- packages/compiler-sfc/src/rewriteDefault.ts | 8 +++++++- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/compiler-sfc/__tests__/rewriteDefault.spec.ts b/packages/compiler-sfc/__tests__/rewriteDefault.spec.ts index 49110436c6e..bed27e3ae81 100644 --- a/packages/compiler-sfc/__tests__/rewriteDefault.spec.ts +++ b/packages/compiler-sfc/__tests__/rewriteDefault.spec.ts @@ -199,9 +199,9 @@ describe('compiler sfc: rewriteDefault', () => { test('export default class', async () => { expect(rewriteDefault(`export default class Foo {}`, 'script')) .toMatchInlineSnapshot(` - "class Foo {} - const script = Foo" - `) + " class Foo {} + const script = Foo" + `) }) test('export default class w/ comments', async () => { @@ -209,7 +209,7 @@ describe('compiler sfc: rewriteDefault', () => { rewriteDefault(`// export default\nexport default class Foo {}`, 'script') ).toMatchInlineSnapshot(` "// export default - class Foo {} + class Foo {} const script = Foo" `) }) @@ -235,7 +235,7 @@ describe('compiler sfc: rewriteDefault', () => { ).toMatchInlineSnapshot(` "/* export default class Foo {}*/ - class Bar {} + class Bar {} const script = Bar" `) }) @@ -246,7 +246,7 @@ describe('compiler sfc: rewriteDefault', () => { 'decorators-legacy' ]) ).toMatchInlineSnapshot(` - "class Foo {} + "@Component class Foo {} const script = Foo" `) }) @@ -260,7 +260,7 @@ describe('compiler sfc: rewriteDefault', () => { ) ).toMatchInlineSnapshot(` "// export default - class Foo {} + @Component class Foo {} const script = Foo" `) }) @@ -289,7 +289,7 @@ describe('compiler sfc: rewriteDefault', () => { "/* @Component export default class Foo {}*/ - class Bar {} + class Bar {} const script = Bar" `) }) diff --git a/packages/compiler-sfc/src/rewriteDefault.ts b/packages/compiler-sfc/src/rewriteDefault.ts index b7be27eb615..ae5e7366bde 100644 --- a/packages/compiler-sfc/src/rewriteDefault.ts +++ b/packages/compiler-sfc/src/rewriteDefault.ts @@ -38,7 +38,13 @@ export function rewriteDefaultAST( ast.forEach(node => { if (node.type === 'ExportDefaultDeclaration') { if (node.declaration.type === 'ClassDeclaration') { - s.overwrite(node.start!, node.declaration.id.start!, `class `) + let start: number = + node.declaration.decorators && node.declaration.decorators.length > 0 + ? node.declaration.decorators[ + node.declaration.decorators.length - 1 + ].end! + : node.start! + s.overwrite(start, node.declaration.id.start!, ` class `) s.append(`\nconst ${as} = ${node.declaration.id.name}`) } else { s.overwrite(node.start!, node.declaration.start!, `const ${as} = `)