diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 77e8ed01..52bda6f4 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -4,8 +4,10 @@ exports[`SFC compile + + `) + assertCode(content) + }) + + test('script setup first', () => { + const { content } = compile(` + + + `) + assertCode(content) + }) + + test('script setup first, named default export', () => { + const { content } = compile(` + + + `) + assertCode(content) + }) + + // #4395 + test('script setup first, lang="ts", script block content export default', () => { + const { content } = compile(` + + + `) + // ensure __default__ is declared before used + expect(content).toMatch(/const __default__[\S\s]*\.\.\.__default__/m) + assertCode(content) + }) + describe('spaces in ExportDefaultDeclaration node', () => { // #4371 test('with many spaces and newline', () => { @@ -205,50 +267,6 @@ defineExpose({ foo: 123 }) assertCode(content) }) }) - - test('script first', () => { - const { content } = compile(` - - - `) - assertCode(content) - }) - - test('script setup first', () => { - const { content } = compile(` - - - `) - assertCode(content) - }) - - // #4395 - test('script setup first, lang="ts", script block content export default', () => { - const { content } = compile(` - - - `) - // ensure __default__ is declared before used - expect(content).toMatch(/const __default__[\S\s]*\.\.\.__default__/m) - assertCode(content) - }) }) describe('imports', () => { diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 9aa10811..75203d42 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -59,6 +59,9 @@ const DEFINE_EMITS = 'defineEmits' const DEFINE_EXPOSE = 'defineExpose' const WITH_DEFAULTS = 'withDefaults' +// constants +const DEFAULT_VAR = `__default__` + const isBuiltInDir = makeMap( `once,memo,if,else,else-if,slot,text,html,on,bind,model,show,cloak,is` ) @@ -214,14 +217,14 @@ export function compileScript( } } if (cssVars.length) { - content = rewriteDefault(content, `__default__`, plugins) + content = rewriteDefault(content, DEFAULT_VAR, plugins) content += genNormalScriptCssVarsCode( cssVars, bindings, scopeId, isProd ) - content += `\nexport default __default__` + content += `\nexport default ${DEFAULT_VAR}` } return { ...script, @@ -251,7 +254,6 @@ export function compileScript( // metadata that needs to be returned const bindingMetadata: BindingMetadata = {} - const defaultTempVar = `__default__` const helperImports: Set = new Set() const userImports: Record = Object.create(null) const userImportAlias: Record = Object.create(null) @@ -780,7 +782,6 @@ export function compileScript( // 1. process normal