Skip to content

Commit 8b232a7

Browse files
committed
refactor(plugin-vue): ensure style processing in custom elements mode
1 parent 1ba31c4 commit 8b232a7

File tree

3 files changed

+48
-41
lines changed

3 files changed

+48
-41
lines changed

packages/plugin-vue/src/handleHotUpdate.ts

+3
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ export async function handleHotUpdate({
9595
)
9696
if (mod) {
9797
affectedModules.add(mod)
98+
if (mod.url.includes('&inline')) {
99+
affectedModules.add(mainModule)
100+
}
98101
} else {
99102
// new style block - force reload
100103
affectedModules.add(mainModule)

packages/plugin-vue/src/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ export interface Options {
4545
style?: Partial<SFCStyleCompileOptions>
4646

4747
/**
48-
* Transform Vue SFCs into custom elements (requires Vue >= 3.2.0)
49-
* - `true` -> all `*.vue` imports are converted into custom elements
50-
* - `string | RegExp` -> matched files are converted into custom elements
48+
* Transform Vue SFCs into custom elements.
49+
* **requires Vue >= 3.2.0 & Vite >= 2.4.4**
50+
* - `true`: all `*.vue` imports are converted into custom elements
51+
* - `string | RegExp`: matched files are converted into custom elements
5152
*
5253
* @default /\.ce\.vue$/
5354
*/

packages/plugin-vue/src/main.ts

+41-38
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { transformTemplateInMain } from './template'
1414
import { isOnlyTemplateChanged, isEqualBlock } from './handleHotUpdate'
1515
import { RawSourceMap, SourceMapConsumer, SourceMapGenerator } from 'source-map'
1616
import { createRollupError } from './utils/error'
17-
import { transformStyle } from './style'
1817

1918
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
2019
export async function transformMain(
@@ -94,9 +93,11 @@ export async function transformMain(
9493
}
9594

9695
// 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+
)
100101

101102
// custom blocks
102103
const customBlocksCode = await genCustomBlockCode(descriptor, pluginContext)
@@ -298,7 +299,8 @@ async function genScriptCode(
298299

299300
async function genStyleCode(
300301
descriptor: SFCDescriptor,
301-
pluginContext: PluginContext
302+
pluginContext: PluginContext,
303+
asCustomElement: boolean
302304
) {
303305
let stylesCode = ``
304306
let hasCSSModules = false
@@ -313,23 +315,55 @@ async function genStyleCode(
313315
// that the module needs to export the modules json
314316
const attrsQuery = attrsToQuery(style.attrs, 'css')
315317
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}`
317320
const styleRequest = src + query + attrsQuery
318321
if (style.module) {
322+
if (asCustomElement) {
323+
throw new Error(
324+
`<style module> is not supported in custom elements mode.`
325+
)
326+
}
319327
if (!hasCSSModules) {
320328
stylesCode += `\nconst cssModules = _sfc_main.__cssModules = {}`
321329
hasCSSModules = true
322330
}
323331
stylesCode += genCSSModulesCode(i, styleRequest, style.module)
324332
} 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+
}
326340
}
327341
// TODO SSR critical CSS collection
328342
}
343+
if (asCustomElement) {
344+
stylesCode += `\n_sfc_main.styles = [${descriptor.styles
345+
.map((_, i) => `_style_${i}`)
346+
.join(',')}]`
347+
}
329348
}
330349
return stylesCode
331350
}
332351

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+
333367
async function genCustomBlockCode(
334368
descriptor: SFCDescriptor,
335369
pluginContext: PluginContext
@@ -351,21 +385,6 @@ async function genCustomBlockCode(
351385
return code
352386
}
353387

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-
369388
/**
370389
* For blocks with src imports, it is important to link the imported file
371390
* with its owner SFC descriptor so that we can get the information about
@@ -411,19 +430,3 @@ function attrsToQuery(
411430
}
412431
return query
413432
}
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

Comments
 (0)