@@ -14,7 +14,6 @@ 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'
18
17
19
18
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
20
19
export async function transformMain (
@@ -94,9 +93,11 @@ export async function transformMain(
94
93
}
95
94
96
95
// styles
97
- const stylesCode = asCustomElement
98
- ? await genCustomElementStyleCode ( descriptor , options , pluginContext )
99
- : await genStyleCode ( descriptor , pluginContext )
96
+ const stylesCode = await genStyleCode (
97
+ descriptor ,
98
+ pluginContext ,
99
+ asCustomElement
100
+ )
100
101
101
102
// custom blocks
102
103
const customBlocksCode = await genCustomBlockCode ( descriptor , pluginContext )
@@ -298,7 +299,8 @@ async function genScriptCode(
298
299
299
300
async function genStyleCode (
300
301
descriptor : SFCDescriptor ,
301
- pluginContext : PluginContext
302
+ pluginContext : PluginContext ,
303
+ asCustomElement : boolean
302
304
) {
303
305
let stylesCode = ``
304
306
let hasCSSModules = false
@@ -313,23 +315,55 @@ async function genStyleCode(
313
315
// that the module needs to export the modules json
314
316
const attrsQuery = attrsToQuery ( style . attrs , 'css' )
315
317
const srcQuery = style . src ? `&src` : ``
316
- const query = `?vue&type=style&index=${ i } ${ srcQuery } `
318
+ const directQuery = asCustomElement ? `&inline` : ``
319
+ const query = `?vue&type=style&index=${ i } ${ srcQuery } ${ directQuery } `
317
320
const styleRequest = src + query + attrsQuery
318
321
if ( style . module ) {
322
+ if ( asCustomElement ) {
323
+ throw new Error (
324
+ `<style module> is not supported in custom elements mode.`
325
+ )
326
+ }
319
327
if ( ! hasCSSModules ) {
320
328
stylesCode += `\nconst cssModules = _sfc_main.__cssModules = {}`
321
329
hasCSSModules = true
322
330
}
323
331
stylesCode += genCSSModulesCode ( i , styleRequest , style . module )
324
332
} else {
325
- stylesCode += `\nimport ${ JSON . stringify ( styleRequest ) } `
333
+ if ( asCustomElement ) {
334
+ stylesCode += `\nimport _style_${ i } from ${ JSON . stringify (
335
+ styleRequest
336
+ ) } `
337
+ } else {
338
+ stylesCode += `\nimport ${ JSON . stringify ( styleRequest ) } `
339
+ }
326
340
}
327
341
// TODO SSR critical CSS collection
328
342
}
343
+ if ( asCustomElement ) {
344
+ stylesCode += `\n_sfc_main.styles = [${ descriptor . styles
345
+ . map ( ( _ , i ) => `_style_${ i } ` )
346
+ . join ( ',' ) } ]`
347
+ }
329
348
}
330
349
return stylesCode
331
350
}
332
351
352
+ function genCSSModulesCode (
353
+ index : number ,
354
+ request : string ,
355
+ moduleName : string | boolean
356
+ ) : string {
357
+ const styleVar = `style${ index } `
358
+ const exposedName = typeof moduleName === 'string' ? moduleName : '$style'
359
+ // inject `.module` before extension so vite handles it as css module
360
+ const moduleRequest = request . replace ( / \. ( \w + ) $ / , '.module.$1' )
361
+ return (
362
+ `\nimport ${ styleVar } from ${ JSON . stringify ( moduleRequest ) } ` +
363
+ `\ncssModules["${ exposedName } "] = ${ styleVar } `
364
+ )
365
+ }
366
+
333
367
async function genCustomBlockCode (
334
368
descriptor : SFCDescriptor ,
335
369
pluginContext : PluginContext
@@ -351,21 +385,6 @@ async function genCustomBlockCode(
351
385
return code
352
386
}
353
387
354
- function genCSSModulesCode (
355
- index : number ,
356
- request : string ,
357
- moduleName : string | boolean
358
- ) : string {
359
- const styleVar = `style${ index } `
360
- const exposedName = typeof moduleName === 'string' ? moduleName : '$style'
361
- // inject `.module` before extension so vite handles it as css module
362
- const moduleRequest = request . replace ( / \. ( \w + ) $ / , '.module.$1' )
363
- return (
364
- `\nimport ${ styleVar } from ${ JSON . stringify ( moduleRequest ) } ` +
365
- `\ncssModules["${ exposedName } "] = ${ styleVar } `
366
- )
367
- }
368
-
369
388
/**
370
389
* For blocks with src imports, it is important to link the imported file
371
390
* with its owner SFC descriptor so that we can get the information about
@@ -411,19 +430,3 @@ function attrsToQuery(
411
430
}
412
431
return query
413
432
}
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