@@ -69,9 +69,6 @@ const DEFINE_EXPOSE = 'defineExpose'
69
69
const WITH_DEFAULTS = 'withDefaults'
70
70
const DEFINE_OPTIONS = 'defineOptions'
71
71
72
- // constants
73
- const DEFAULT_VAR = `__default__`
74
-
75
72
const isBuiltInDir = makeMap (
76
73
`once,memo,if,for,else,else-if,slot,text,html,on,bind,model,show,cloak,is`
77
74
)
@@ -110,6 +107,12 @@ export interface SFCScriptCompileOptions {
110
107
* from being hot-reloaded separately from component state.
111
108
*/
112
109
inlineTemplate ?: boolean
110
+ /**
111
+ * Generate the final component as a variable instead of default export.
112
+ * This is useful in e.g. @vitejs/plugin-vue where the script needs to be
113
+ * placed inside the main module.
114
+ */
115
+ genDefaultAs ?: string
113
116
/**
114
117
* Options for template compilation when inlining. Note these are options that
115
118
* would normally be passed to `compiler-sfc`'s own `compileTemplate()`, not
@@ -178,6 +181,10 @@ export function compileScript(
178
181
const cssVars = sfc . cssVars
179
182
const scriptLang = script && script . lang
180
183
const scriptSetupLang = scriptSetup && scriptSetup . lang
184
+ const genDefaultAs = options . genDefaultAs
185
+ ? `const ${ options . genDefaultAs } =`
186
+ : `export default`
187
+ const normalScriptDefaultVar = `__default__`
181
188
const isJS =
182
189
scriptLang === 'js' ||
183
190
scriptLang === 'jsx' ||
@@ -216,6 +223,7 @@ export function compileScript(
216
223
// do not process non js/ts script blocks
217
224
return script
218
225
}
226
+ // normal <script> only
219
227
try {
220
228
let content = script . content
221
229
let map = script . map
@@ -247,17 +255,23 @@ export function compileScript(
247
255
} ) as unknown as RawSourceMap
248
256
}
249
257
}
250
- if ( cssVars . length ) {
258
+ if ( cssVars . length || options . genDefaultAs ) {
259
+ const defaultVar = options . genDefaultAs || normalScriptDefaultVar
251
260
const s = new MagicString ( content )
252
- rewriteDefaultAST ( scriptAst . body , s , DEFAULT_VAR )
261
+ rewriteDefaultAST ( scriptAst . body , s , defaultVar )
253
262
content = s . toString ( )
254
- content += genNormalScriptCssVarsCode (
255
- cssVars ,
256
- bindings ,
257
- scopeId ,
258
- isProd
259
- )
260
- content += `\nexport default ${ DEFAULT_VAR } `
263
+ if ( cssVars . length ) {
264
+ content += genNormalScriptCssVarsCode (
265
+ cssVars ,
266
+ bindings ,
267
+ scopeId ,
268
+ isProd ,
269
+ defaultVar
270
+ )
271
+ }
272
+ if ( ! options . genDefaultAs ) {
273
+ content += `\nexport default ${ defaultVar } `
274
+ }
261
275
}
262
276
return {
263
277
...script ,
@@ -1189,7 +1203,7 @@ export function compileScript(
1189
1203
// export default { ... } --> const __default__ = { ... }
1190
1204
const start = node . start ! + scriptStartOffset !
1191
1205
const end = node . declaration . start ! + scriptStartOffset !
1192
- s . overwrite ( start , end , `const ${ DEFAULT_VAR } = ` )
1206
+ s . overwrite ( start , end , `const ${ normalScriptDefaultVar } = ` )
1193
1207
} else if ( node . type === 'ExportNamedDeclaration' ) {
1194
1208
const defaultSpecifier = node . specifiers . find (
1195
1209
s => s . exported . type === 'Identifier' && s . exported . name === 'default'
@@ -1213,14 +1227,14 @@ export function compileScript(
1213
1227
// rewrite to `import { x as __default__ } from './x'` and
1214
1228
// add to top
1215
1229
s . prepend (
1216
- `import { ${ defaultSpecifier . local . name } as ${ DEFAULT_VAR } } from '${ node . source . value } '\n`
1230
+ `import { ${ defaultSpecifier . local . name } as ${ normalScriptDefaultVar } } from '${ node . source . value } '\n`
1217
1231
)
1218
1232
} else {
1219
1233
// export { x as default }
1220
1234
// rewrite to `const __default__ = x` and move to end
1221
1235
s . appendLeft (
1222
1236
scriptEndOffset ! ,
1223
- `\nconst ${ DEFAULT_VAR } = ${ defaultSpecifier . local . name } \n`
1237
+ `\nconst ${ normalScriptDefaultVar } = ${ defaultSpecifier . local . name } \n`
1224
1238
)
1225
1239
}
1226
1240
}
@@ -1793,11 +1807,11 @@ export function compileScript(
1793
1807
// user's TS setting should compile it down to proper targets
1794
1808
// export default defineComponent({ ...__default__, ... })
1795
1809
const def =
1796
- ( defaultExport ? `\n ...${ DEFAULT_VAR } ,` : `` ) +
1810
+ ( defaultExport ? `\n ...${ normalScriptDefaultVar } ,` : `` ) +
1797
1811
( definedOptions ? `\n ...${ definedOptions } ,` : '' )
1798
1812
s . prependLeft (
1799
1813
startOffset ,
1800
- `\nexport default /*#__PURE__*/${ helper (
1814
+ `\n ${ genDefaultAs } /*#__PURE__*/${ helper (
1801
1815
`defineComponent`
1802
1816
) } ({${ def } ${ runtimeOptions } \n ${
1803
1817
hasAwait ? `async ` : ``
@@ -1810,16 +1824,16 @@ export function compileScript(
1810
1824
// export default Object.assign(__default__, { ... })
1811
1825
s . prependLeft (
1812
1826
startOffset ,
1813
- `\nexport default /*#__PURE__*/Object.assign(${
1814
- defaultExport ? `${ DEFAULT_VAR } , ` : ''
1827
+ `\n ${ genDefaultAs } /*#__PURE__*/Object.assign(${
1828
+ defaultExport ? `${ normalScriptDefaultVar } , ` : ''
1815
1829
} ${ definedOptions ? `${ definedOptions } , ` : '' } {${ runtimeOptions } \n ` +
1816
1830
`${ hasAwait ? `async ` : `` } setup(${ args } ) {\n${ exposeCall } `
1817
1831
)
1818
1832
s . appendRight ( endOffset , `})` )
1819
1833
} else {
1820
1834
s . prependLeft (
1821
1835
startOffset ,
1822
- `\nexport default {${ runtimeOptions } \n ` +
1836
+ `\n ${ genDefaultAs } {${ runtimeOptions } \n ` +
1823
1837
`${ hasAwait ? `async ` : `` } setup(${ args } ) {\n${ exposeCall } `
1824
1838
)
1825
1839
s . appendRight ( endOffset , `}` )
@@ -1839,7 +1853,6 @@ export function compileScript(
1839
1853
1840
1854
return {
1841
1855
...scriptSetup ,
1842
- s,
1843
1856
bindings : bindingMetadata ,
1844
1857
imports : userImports ,
1845
1858
content : s . toString ( ) ,
0 commit comments