@@ -21,7 +21,6 @@ import {
21
21
SFCBlock ,
22
22
SFCTemplateCompileOptions ,
23
23
SFCScriptCompileOptions ,
24
- SFCStyleBlock ,
25
24
} from '@vue/compiler-sfc'
26
25
import { selectBlock } from './select'
27
26
import { genHotReloadCode } from './hotReload'
@@ -41,6 +40,8 @@ export interface VueLoaderOptions {
41
40
compiler ?: TemplateCompiler | string
42
41
compilerOptions ?: CompilerOptions
43
42
refSugar ?: boolean
43
+ customElement ?: boolean | RegExp
44
+
44
45
hotReload ?: boolean
45
46
exposeFilename ?: boolean
46
47
appendExtension ?: boolean
@@ -98,6 +99,11 @@ export default function loader(
98
99
sourceMap,
99
100
} )
100
101
102
+ const asCustomElement =
103
+ typeof options . customElement === 'boolean'
104
+ ? options . customElement
105
+ : ( options . customElement || / \. c e \. v u e $ / ) . test ( filename )
106
+
101
107
// cache descriptor
102
108
setDescriptor ( filename , descriptor )
103
109
@@ -184,15 +190,21 @@ export default function loader(
184
190
if ( descriptor . styles . length ) {
185
191
descriptor . styles
186
192
. filter ( ( style ) => style . src || nonWhitespaceRE . test ( style . content ) )
187
- . forEach ( ( style : SFCStyleBlock , i : number ) => {
193
+ . forEach ( ( style , i ) => {
188
194
const src = style . src || resourcePath
189
195
const attrsQuery = attrsToQuery ( style . attrs , 'css' )
190
196
// make sure to only pass id when necessary so that we don't inject
191
197
// duplicate tags when multiple components import the same css file
192
198
const idQuery = ! style . src || style . scoped ? `&id=${ id } ` : ``
193
- const query = `?vue&type=style&index=${ i } ${ idQuery } ${ attrsQuery } ${ resourceQuery } `
199
+ const inlineQuery = asCustomElement ? `&inline` : ``
200
+ const query = `?vue&type=style&index=${ i } ${ idQuery } ${ inlineQuery } ${ attrsQuery } ${ resourceQuery } `
194
201
const styleRequest = stringifyRequest ( src + query )
195
202
if ( style . module ) {
203
+ if ( asCustomElement ) {
204
+ loaderContext . emitError (
205
+ `<style module> is not supported in custom element mode.`
206
+ )
207
+ }
196
208
if ( ! hasCSSModules ) {
197
209
stylesCode += `\nconst cssModules = script.__cssModules = {}`
198
210
hasCSSModules = true
@@ -205,10 +217,19 @@ export default function loader(
205
217
needsHotReload
206
218
)
207
219
} else {
208
- stylesCode += `\nimport ${ styleRequest } `
220
+ if ( asCustomElement ) {
221
+ stylesCode += `\nimport _style_${ i } from ${ styleRequest } `
222
+ } else {
223
+ stylesCode += `\nimport ${ styleRequest } `
224
+ }
209
225
}
210
226
// TODO SSR critical CSS collection
211
227
} )
228
+ if ( asCustomElement ) {
229
+ stylesCode += `\nscript.styles = [${ descriptor . styles . map (
230
+ ( _ , i ) => `_style_${ i } `
231
+ ) } ]`
232
+ }
212
233
}
213
234
214
235
let code = [
@@ -264,7 +285,10 @@ export default function loader(
264
285
}
265
286
266
287
// finalize
267
- code += `\n\nexport default script`
288
+ code += asCustomElement
289
+ ? `\n\nimport { defineCustomElement as __ce } from 'vue';` +
290
+ `export default __ce(script)`
291
+ : `\n\nexport default script`
268
292
return code
269
293
}
270
294
0 commit comments