@@ -14,14 +14,16 @@ import { transformTemplateInMain } from './template'
14
14
import { isOnlyTemplateChanged , isEqualBlock } from './handleHotUpdate'
15
15
import { RawSourceMap , SourceMapConsumer , SourceMapGenerator } from 'source-map'
16
16
import { createRollupError } from './utils/error'
17
+ import { transformStyle } from './style'
17
18
18
19
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
19
20
export async function transformMain (
20
21
code : string ,
21
22
filename : string ,
22
23
options : ResolvedOptions ,
23
24
pluginContext : TransformPluginContext ,
24
- ssr : boolean
25
+ ssr : boolean ,
26
+ asCustomElement : boolean
25
27
) {
26
28
const { root, devServer, isProduction } = options
27
29
@@ -92,7 +94,9 @@ export async function transformMain(
92
94
}
93
95
94
96
// styles
95
- const stylesCode = await genStyleCode ( descriptor , pluginContext )
97
+ const stylesCode = asCustomElement
98
+ ? await genCustomElementStyleCode ( descriptor , options , pluginContext )
99
+ : await genStyleCode ( descriptor , pluginContext )
96
100
97
101
// custom blocks
98
102
const customBlocksCode = await genCustomBlockCode ( descriptor , pluginContext )
@@ -113,7 +117,6 @@ export async function transformMain(
113
117
// expose filename during serve for devtools to pickup
114
118
output . push ( `_sfc_main.__file = ${ JSON . stringify ( filename ) } ` )
115
119
}
116
- output . push ( 'export default _sfc_main' )
117
120
118
121
// HMR
119
122
if (
@@ -132,7 +135,9 @@ export async function transformMain(
132
135
output . push ( `export const _rerender_only = true` )
133
136
}
134
137
output . push (
135
- `import.meta.hot.accept(({ default: updated, _rerender_only }) => {` ,
138
+ `import.meta.hot.accept(({ default: ${
139
+ asCustomElement ? `{ def: updated }` : `updated`
140
+ } , _rerender_only }) => {`,
136
141
` if (_rerender_only) {` ,
137
142
` __VUE_HMR_RUNTIME__.rerender(updated.__hmrId, updated.render)` ,
138
143
` } else {` ,
@@ -185,6 +190,15 @@ export async function transformMain(
185
190
resolvedMap . sourcesContent = templateMap . sourcesContent
186
191
}
187
192
193
+ if ( asCustomElement ) {
194
+ output . push (
195
+ `import { defineCustomElement as __ce } from 'vue'` ,
196
+ `export default __ce(_sfc_main)`
197
+ )
198
+ } else {
199
+ output . push ( `export default _sfc_main` )
200
+ }
201
+
188
202
return {
189
203
code : output . join ( '\n' ) ,
190
204
map : resolvedMap || {
@@ -397,3 +411,19 @@ function attrsToQuery(
397
411
}
398
412
return query
399
413
}
414
+
415
+ async function genCustomElementStyleCode (
416
+ descriptor : SFCDescriptor ,
417
+ options : ResolvedOptions ,
418
+ pluginContext : TransformPluginContext
419
+ ) {
420
+ const styles = (
421
+ await Promise . all (
422
+ descriptor . styles . map ( ( style , index ) =>
423
+ transformStyle ( style . content , descriptor , index , options , pluginContext )
424
+ )
425
+ )
426
+ ) . map ( ( res ) => JSON . stringify ( res ! . code ) )
427
+
428
+ return `_sfc_main.styles = [${ styles . join ( ',' ) } ]`
429
+ }
0 commit comments