@@ -15,6 +15,7 @@ import { isOnlyTemplateChanged, isEqualBlock } from './handleHotUpdate'
15
15
import { RawSourceMap , SourceMapConsumer , SourceMapGenerator } from 'source-map'
16
16
import { createRollupError } from './utils/error'
17
17
import { transformWithEsbuild } from 'vite'
18
+ import { EXPORT_HELPER_ID } from './helper'
18
19
19
20
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
20
21
export async function transformMain (
@@ -39,6 +40,7 @@ export async function transformMain(
39
40
}
40
41
41
42
// feature information
43
+ const attachedProps : [ string , string ] [ ] = [ ]
42
44
const hasScoped = descriptor . styles . some ( ( s ) => s . scoped )
43
45
44
46
// script
@@ -70,29 +72,27 @@ export async function transformMain(
70
72
) )
71
73
}
72
74
73
- let renderReplace = ''
74
75
if ( hasTemplateImport ) {
75
- renderReplace = ssr
76
- ? `_sfc_main. ssrRender = _sfc_ssrRender`
77
- : `_sfc_main.render = _sfc_render`
76
+ attachedProps . push (
77
+ ssr ? [ ' ssrRender' , ' _sfc_ssrRender' ] : [ 'render' , '_sfc_render' ]
78
+ )
78
79
} else {
79
80
// #2128
80
81
// User may empty the template but we didn't provide rerender function before
81
82
if (
82
83
prevDescriptor &&
83
84
! isEqualBlock ( descriptor . template , prevDescriptor . template )
84
85
) {
85
- renderReplace = ssr
86
- ? `_sfc_main.ssrRender = () => {}`
87
- : `_sfc_main.render = () => {}`
86
+ attachedProps . push ( [ ssr ? 'ssrRender' : 'render' , '() => {}' ] )
88
87
}
89
88
}
90
89
91
90
// styles
92
91
const stylesCode = await genStyleCode (
93
92
descriptor ,
94
93
pluginContext ,
95
- asCustomElement
94
+ asCustomElement ,
95
+ attachedProps
96
96
)
97
97
98
98
// custom blocks
@@ -102,17 +102,14 @@ export async function transformMain(
102
102
scriptCode ,
103
103
templateCode ,
104
104
stylesCode ,
105
- customBlocksCode ,
106
- renderReplace
105
+ customBlocksCode
107
106
]
108
107
if ( hasScoped ) {
109
- output . push (
110
- `_sfc_main.__scopeId = ${ JSON . stringify ( `data-v-${ descriptor . id } ` ) } `
111
- )
108
+ attachedProps . push ( [ `__scopeId` , JSON . stringify ( `data-v-${ descriptor . id } ` ) ] )
112
109
}
113
110
if ( devServer && ! isProduction ) {
114
111
// expose filename during serve for devtools to pickup
115
- output . push ( `_sfc_main. __file = ${ JSON . stringify ( filename ) } ` )
112
+ attachedProps . push ( [ ` __file` , JSON . stringify ( filename ) ] )
116
113
}
117
114
118
115
// HMR
@@ -185,7 +182,16 @@ export async function transformMain(
185
182
resolvedMap . sourcesContent = templateMap . sourcesContent
186
183
}
187
184
188
- output . push ( `export default _sfc_main` )
185
+ if ( ! attachedProps . length ) {
186
+ output . push ( `export default _sfc_main` )
187
+ } else {
188
+ output . push (
189
+ `import _export_sfc from '${ EXPORT_HELPER_ID } '` ,
190
+ `export default /*#__PURE__*/_export_sfc(_sfc_main, [${ attachedProps
191
+ . map ( ( [ key , val ] ) => `['${ key } ',${ val } ]` )
192
+ . join ( ',' ) } ])`
193
+ )
194
+ }
189
195
190
196
// handle TS transpilation
191
197
let resolvedCode = output . join ( '\n' )
@@ -290,7 +296,8 @@ async function genScriptCode(
290
296
async function genStyleCode (
291
297
descriptor : SFCDescriptor ,
292
298
pluginContext : PluginContext ,
293
- asCustomElement : boolean
299
+ asCustomElement : boolean ,
300
+ attachedProps : [ string , string ] [ ]
294
301
) {
295
302
let stylesCode = ``
296
303
let hasCSSModules = false
@@ -315,7 +322,8 @@ async function genStyleCode(
315
322
)
316
323
}
317
324
if ( ! hasCSSModules ) {
318
- stylesCode += `\nconst cssModules = _sfc_main.__cssModules = {}`
325
+ stylesCode += `\nconst cssModules = {}`
326
+ attachedProps . push ( [ `__cssModules` , `cssModules` ] )
319
327
hasCSSModules = true
320
328
}
321
329
stylesCode += genCSSModulesCode ( i , styleRequest , style . module )
@@ -331,9 +339,10 @@ async function genStyleCode(
331
339
// TODO SSR critical CSS collection
332
340
}
333
341
if ( asCustomElement ) {
334
- stylesCode += `\n_sfc_main.styles = [${ descriptor . styles
335
- . map ( ( _ , i ) => `_style_${ i } ` )
336
- . join ( ',' ) } ]`
342
+ attachedProps . push ( [
343
+ `styles` ,
344
+ `[${ descriptor . styles . map ( ( _ , i ) => `_style_${ i } ` ) . join ( ',' ) } ]`
345
+ ] )
337
346
}
338
347
}
339
348
return stylesCode
0 commit comments