@@ -12,8 +12,9 @@ import { resolveScript } from './script'
1212import { transformTemplateInMain } from './template'
1313import { isOnlyTemplateChanged } from './handleHotUpdate'
1414import { createRollupError } from './utils/error'
15- import { EXPORT_HELPER_ID } from './helper'
1615import type { ResolvedOptions } from '.'
16+ import { NORMALIZER_ID } from './utils/componentNormalizer'
17+ import { HMR_RUNTIME_ID } from './utils/hmrRuntime'
1718
1819// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
1920export async function transformMain (
@@ -38,8 +39,10 @@ export async function transformMain(
3839 }
3940
4041 // feature information
41- const attachedProps : [ string , string ] [ ] = [ ]
4242 const hasScoped = descriptor . styles . some ( ( s ) => s . scoped )
43+ const hasCssModules = descriptor . styles . some ( ( s ) => s . module )
44+ const hasFunctional =
45+ descriptor . template && descriptor . template . attrs . functional
4346
4447 // script
4548 const { code : scriptCode , map : scriptMap } = await genScriptCode (
@@ -57,17 +60,8 @@ export async function transformMain(
5760 ssr
5861 )
5962
60- attachedProps . push (
61- ssr ? [ 'ssrRender' , '_sfc_ssrRender' ] : [ 'render' , '_sfc_render' ]
62- )
63-
6463 // styles
65- const stylesCode = await genStyleCode (
66- descriptor ,
67- pluginContext ,
68- // asCustomElement,
69- attachedProps
70- )
64+ const stylesCode = await genStyleCode ( descriptor , pluginContext )
7165
7266 // custom blocks
7367 const customBlocksCode = await genCustomBlockCode ( descriptor , pluginContext )
@@ -78,15 +72,29 @@ export async function transformMain(
7872 stylesCode ,
7973 customBlocksCode
8074 ]
81- if ( hasScoped ) {
82- attachedProps . push ( [ `__scopeId` , JSON . stringify ( `data-v-${ descriptor . id } ` ) ] )
83- }
75+
76+ output . push (
77+ `/* normalize component */
78+ import __normalizer from "${ NORMALIZER_ID } "
79+ var __component__ = /*#__PURE__*/__normalizer(
80+ _sfc_main,
81+ _sfc_render,
82+ _sfc_staticRenderFns,
83+ ${ hasFunctional ? 'true' : 'false' } ,
84+ ${ hasCssModules ? `_sfc_injectStyles` : `null` } ,
85+ ${ hasScoped ? JSON . stringify ( descriptor . id ) : 'null' } ,
86+ null,
87+ null
88+ )`
89+ )
90+
8491 if ( devToolsEnabled || ( devServer && ! isProduction ) ) {
8592 // expose filename during serve for devtools to pickup
86- attachedProps . push ( [
87- `__file` ,
88- JSON . stringify ( isProduction ? path . basename ( filename ) : filename )
89- ] )
93+ output . push (
94+ `__component__.options.__file = ${ JSON . stringify (
95+ isProduction ? path . basename ( filename ) : filename
96+ ) } `
97+ )
9098 }
9199
92100 // HMR
@@ -96,21 +104,26 @@ export async function transformMain(
96104 ! ssr &&
97105 ! isProduction
98106 ) {
99- output . push ( `_sfc_main.__hmrId = ${ JSON . stringify ( descriptor . id ) } ` )
107+ const id = JSON . stringify ( descriptor . id )
100108 output . push (
101- `typeof __VUE_HMR_RUNTIME__ !== 'undefined' && ` +
102- `__VUE_HMR_RUNTIME__.createRecord(_sfc_main.__hmrId, _sfc_main)`
109+ `import __VUE_HMR_RUNTIME__ from "${ HMR_RUNTIME_ID } "` ,
110+ `if (!__VUE_HMR_RUNTIME__.isRecorded(${ id } )) {` ,
111+ ` __VUE_HMR_RUNTIME__.createRecord(${ id } , __component__.options)` ,
112+ `}`
103113 )
104114 // check if the template is the only thing that changed
105- if ( prevDescriptor && isOnlyTemplateChanged ( prevDescriptor , descriptor ) ) {
115+ if (
116+ hasFunctional ||
117+ ( prevDescriptor && isOnlyTemplateChanged ( prevDescriptor , descriptor ) )
118+ ) {
106119 output . push ( `export const _rerender_only = true` )
107120 }
108121 output . push (
109122 `import.meta.hot.accept(({ default: updated, _rerender_only }) => {` ,
110123 ` if (_rerender_only) {` ,
111- ` __VUE_HMR_RUNTIME__.rerender(updated.__hmrId , updated.render )` ,
124+ ` __VUE_HMR_RUNTIME__.rerender(${ id } , updated)` ,
112125 ` } else {` ,
113- ` __VUE_HMR_RUNTIME__.reload(updated.__hmrId , updated)` ,
126+ ` __VUE_HMR_RUNTIME__.reload(${ id } , updated)` ,
114127 ` }` ,
115128 `})`
116129 )
@@ -123,16 +136,7 @@ export async function transformMain(
123136
124137 let resolvedMap : RawSourceMap | undefined = scriptMap
125138
126- if ( ! attachedProps . length ) {
127- output . push ( `export default _sfc_main` )
128- } else {
129- output . push (
130- `import _export_sfc from '${ EXPORT_HELPER_ID } '` ,
131- `export default /*#__PURE__*/_export_sfc(_sfc_main, [${ attachedProps
132- . map ( ( [ key , val ] ) => `['${ key } ',${ val } ]` )
133- . join ( ',' ) } ])`
134- )
135- }
139+ output . push ( `export default __component__.exports` )
136140
137141 // handle TS transpilation
138142 let resolvedCode = output . join ( '\n' )
@@ -256,9 +260,7 @@ async function genScriptCode(
256260
257261async function genStyleCode (
258262 descriptor : SFCDescriptor ,
259- pluginContext : PluginContext ,
260- // asCustomElement: boolean,
261- attachedProps : [ string , string ] [ ]
263+ pluginContext : PluginContext
262264) {
263265 let stylesCode = ``
264266 let cssModulesMap : Record < string , string > | undefined
@@ -306,8 +308,12 @@ async function genStyleCode(
306308 ( code , [ key , value ] ) => code + `"${ key } ":${ value } ,\n` ,
307309 '{\n'
308310 ) + '}'
309- stylesCode += `\nconst cssModules = ${ mappingCode } `
310- attachedProps . push ( [ `__cssModules` , `cssModules` ] )
311+ stylesCode += `\nconst __cssModules = ${ mappingCode } `
312+ stylesCode += `\nfunction _sfc_injectStyles(ctx) {
313+ for (var key in __cssModules) {
314+ this[key] = __cssModules[key]
315+ }
316+ }`
311317 }
312318 return stylesCode
313319}
0 commit comments