@@ -12,7 +12,12 @@ import {
12
12
getPrevDescriptor ,
13
13
setSrcDescriptor ,
14
14
} from './utils/descriptorCache'
15
- import { isUseInlineTemplate , resolveScript } from './script'
15
+ import {
16
+ canInlineMain ,
17
+ isUseInlineTemplate ,
18
+ resolveScript ,
19
+ scriptIdentifier ,
20
+ } from './script'
16
21
import { transformTemplateInMain } from './template'
17
22
import { isEqualBlock , isOnlyTemplateChanged } from './handleHotUpdate'
18
23
import { createRollupError } from './utils/error'
@@ -303,33 +308,31 @@ async function genScriptCode(
303
308
code : string
304
309
map : RawSourceMap | undefined
305
310
} > {
306
- let scriptCode = `const _sfc_main = {}`
311
+ let scriptCode = `const ${ scriptIdentifier } = {}`
307
312
let map : RawSourceMap | undefined
308
313
309
314
const script = resolveScript ( descriptor , options , ssr )
310
315
if ( script ) {
311
316
// If the script is js/ts and has no external src, it can be directly placed
312
317
// in the main module.
313
- if (
314
- ( ! script . lang || ( script . lang === 'ts' && options . devServer ) ) &&
315
- ! script . src
316
- ) {
317
- const userPlugins = options . script ?. babelParserPlugins || [ ]
318
- const defaultPlugins =
319
- script . lang === 'ts'
320
- ? userPlugins . includes ( 'decorators' )
321
- ? ( [ 'typescript' ] as const )
322
- : ( [ 'typescript' , 'decorators-legacy' ] as const )
323
- : [ ]
324
- const as = '_sfc_main'
325
- if ( options . compiler . rewriteDefaultAST && script . scriptAst && script . s ) {
326
- options . compiler . rewriteDefaultAST ( script . scriptAst , script . s , as )
327
- scriptCode = script . s . toString ( )
318
+ if ( canInlineMain ( descriptor , options ) ) {
319
+ if ( ! options . compiler . version ) {
320
+ // if compiler-sfc exposes no version, it's < 3.3 and doesn't support
321
+ // genDefaultAs option.
322
+ const userPlugins = options . script ?. babelParserPlugins || [ ]
323
+ const defaultPlugins =
324
+ script . lang === 'ts'
325
+ ? userPlugins . includes ( 'decorators' )
326
+ ? ( [ 'typescript' ] as const )
327
+ : ( [ 'typescript' , 'decorators-legacy' ] as const )
328
+ : [ ]
329
+ scriptCode = options . compiler . rewriteDefault (
330
+ script . content ,
331
+ scriptIdentifier ,
332
+ [ ...defaultPlugins , ...userPlugins ] ,
333
+ )
328
334
} else {
329
- scriptCode = options . compiler . rewriteDefault ( script . content , as , [
330
- ...defaultPlugins ,
331
- ...userPlugins ,
332
- ] )
335
+ scriptCode = script . content
333
336
}
334
337
map = script . map
335
338
} else {
0 commit comments