From 71635be68d25887f91d624bb7f78281a851bc0cb Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 30 Mar 2023 16:58:41 +0800 Subject: [PATCH] feat(compiler-sfc): support generating variable instead of default export in compileScript --- .../__snapshots__/compileScript.spec.ts.snap | 99 +++++++++++++++ .../__tests__/compileScript.spec.ts | 119 ++++++++++++++++++ packages/compiler-sfc/src/compileScript.ts | 55 ++++---- packages/compiler-sfc/src/cssVars.ts | 7 +- packages/compiler-sfc/src/index.ts | 2 + packages/compiler-sfc/src/parse.ts | 2 - 6 files changed, 258 insertions(+), 26 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 2d6df5607a3..e78522ee3af 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -1963,3 +1963,102 @@ return { props } })" `; + +exports[`SFC genDefaultAs > `, + { + genDefaultAs: '_sfc_' + } + ) + expect(content).not.toMatch('export default') + expect(content).toMatch(`const _sfc_ = {}`) + assertCode(content) + }) + + test('normal + `, + { + genDefaultAs: '_sfc_' + } + ) + expect(content).not.toMatch('export default') + expect(content).not.toMatch('__default__') + expect(content).toMatch(`const _sfc_ = {}`) + assertCode(content) + }) + + test(' + `, + { + genDefaultAs: '_sfc_' + } + ) + expect(content).not.toMatch('export default') + expect(content).toMatch( + `const _sfc_ = /*#__PURE__*/Object.assign(__default__` + ) + assertCode(content) + }) + + test(' + `, + { + genDefaultAs: '_sfc_' + } + ) + expect(content).not.toMatch('export default') + expect(content).toMatch( + `const _sfc_ = /*#__PURE__*/Object.assign(__default__` + ) + assertCode(content) + }) + + test('`, + { + genDefaultAs: '_sfc_' + } + ) + expect(content).not.toMatch('export default') + expect(content).toMatch(`const _sfc_ = {\n setup`) + assertCode(content) + }) + + test('`, + { + genDefaultAs: '_sfc_' + } + ) + expect(content).not.toMatch('export default') + expect(content).toMatch(`const _sfc_ = /*#__PURE__*/_defineComponent(`) + assertCode(content) + }) + + test(' + `, + { + genDefaultAs: '_sfc_' + } + ) + expect(content).not.toMatch('export default') + expect(content).toMatch( + `const _sfc_ = /*#__PURE__*/_defineComponent({\n ...__default__` + ) + assertCode(content) + }) +}) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 61b4b7c0ee1..9a5e19c84d4 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -69,9 +69,6 @@ const DEFINE_EXPOSE = 'defineExpose' const WITH_DEFAULTS = 'withDefaults' const DEFINE_OPTIONS = 'defineOptions' -// constants -const DEFAULT_VAR = `__default__` - const isBuiltInDir = makeMap( `once,memo,if,for,else,else-if,slot,text,html,on,bind,model,show,cloak,is` ) @@ -110,6 +107,12 @@ export interface SFCScriptCompileOptions { * from being hot-reloaded separately from component state. */ inlineTemplate?: boolean + /** + * Generate the final component as a variable instead of default export. + * This is useful in e.g. @vitejs/plugin-vue where the script needs to be + * placed inside the main module. + */ + genDefaultAs?: string /** * Options for template compilation when inlining. Note these are options that * would normally be passed to `compiler-sfc`'s own `compileTemplate()`, not @@ -178,6 +181,10 @@ export function compileScript( const cssVars = sfc.cssVars const scriptLang = script && script.lang const scriptSetupLang = scriptSetup && scriptSetup.lang + const genDefaultAs = options.genDefaultAs + ? `const ${options.genDefaultAs} =` + : `export default` + const normalScriptDefaultVar = `__default__` const isJS = scriptLang === 'js' || scriptLang === 'jsx' || @@ -216,6 +223,7 @@ export function compileScript( // do not process non js/ts script blocks return script } + // normal