@@ -15,6 +15,7 @@ import { isOnlyTemplateChanged, isEqualBlock } from './handleHotUpdate'
1515import { RawSourceMap , SourceMapConsumer , SourceMapGenerator } from 'source-map'
1616import { createRollupError } from './utils/error'
1717import { transformWithEsbuild } from 'vite'
18+ import { EXPORT_HELPER_ID } from './helper'
1819
1920// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
2021export async function transformMain (
@@ -39,6 +40,7 @@ export async function transformMain(
3940 }
4041
4142 // feature information
43+ const attachedProps : [ string , string ] [ ] = [ ]
4244 const hasScoped = descriptor . styles . some ( ( s ) => s . scoped )
4345
4446 // script
@@ -70,29 +72,27 @@ export async function transformMain(
7072 ) )
7173 }
7274
73- let renderReplace = ''
7475 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+ )
7879 } else {
7980 // #2128
8081 // User may empty the template but we didn't provide rerender function before
8182 if (
8283 prevDescriptor &&
8384 ! isEqualBlock ( descriptor . template , prevDescriptor . template )
8485 ) {
85- renderReplace = ssr
86- ? `_sfc_main.ssrRender = () => {}`
87- : `_sfc_main.render = () => {}`
86+ attachedProps . push ( [ ssr ? 'ssrRender' : 'render' , '() => {}' ] )
8887 }
8988 }
9089
9190 // styles
9291 const stylesCode = await genStyleCode (
9392 descriptor ,
9493 pluginContext ,
95- asCustomElement
94+ asCustomElement ,
95+ attachedProps
9696 )
9797
9898 // custom blocks
@@ -102,17 +102,14 @@ export async function transformMain(
102102 scriptCode ,
103103 templateCode ,
104104 stylesCode ,
105- customBlocksCode ,
106- renderReplace
105+ customBlocksCode
107106 ]
108107 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 } ` ) ] )
112109 }
113110 if ( devServer && ! isProduction ) {
114111 // expose filename during serve for devtools to pickup
115- output . push ( `_sfc_main. __file = ${ JSON . stringify ( filename ) } ` )
112+ attachedProps . push ( [ ` __file` , JSON . stringify ( filename ) ] )
116113 }
117114
118115 // HMR
@@ -185,7 +182,16 @@ export async function transformMain(
185182 resolvedMap . sourcesContent = templateMap . sourcesContent
186183 }
187184
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+ }
189195
190196 // handle TS transpilation
191197 let resolvedCode = output . join ( '\n' )
@@ -290,7 +296,8 @@ async function genScriptCode(
290296async function genStyleCode (
291297 descriptor : SFCDescriptor ,
292298 pluginContext : PluginContext ,
293- asCustomElement : boolean
299+ asCustomElement : boolean ,
300+ attachedProps : [ string , string ] [ ]
294301) {
295302 let stylesCode = ``
296303 let hasCSSModules = false
@@ -315,7 +322,8 @@ async function genStyleCode(
315322 )
316323 }
317324 if ( ! hasCSSModules ) {
318- stylesCode += `\nconst cssModules = _sfc_main.__cssModules = {}`
325+ stylesCode += `\nconst cssModules = {}`
326+ attachedProps . push ( [ `__cssModules` , `cssModules` ] )
319327 hasCSSModules = true
320328 }
321329 stylesCode += genCSSModulesCode ( i , styleRequest , style . module )
@@ -331,9 +339,10 @@ async function genStyleCode(
331339 // TODO SSR critical CSS collection
332340 }
333341 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+ ] )
337346 }
338347 }
339348 return stylesCode
0 commit comments