Skip to content

Commit 896ce80

Browse files
committed
feat: add customElement option to compiler
ref: vitejs/vite-plugin-vue@99564d5
1 parent dbadc58 commit 896ce80

File tree

5 files changed

+49
-12
lines changed

5 files changed

+49
-12
lines changed

src/core/handleHotUpdate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const directRequestRE = /[&?]direct\b/
2727
export async function handleHotUpdate(
2828
{ file, modules, read }: HmrContext,
2929
options: ResolvedOptions,
30+
customElement: boolean,
3031
): Promise<ModuleNode[] | undefined> {
3132
const prevDescriptor = getDescriptor(file, options, false, true)
3233
if (!prevDescriptor) {
@@ -43,7 +44,7 @@ export async function handleHotUpdate(
4344
const templateModule = modules.find((m) => /type=template/.test(m.url))
4445

4546
// trigger resolveScript for descriptor so that we'll have the AST ready
46-
resolveScript('vite', descriptor, options, false)
47+
resolveScript('vite', descriptor, options, false, customElement)
4748
const scriptChanged = hasScriptChanged(prevDescriptor, descriptor)
4849
if (scriptChanged) {
4950
affectedModules.add(getScriptModule(modules) || mainModule)

src/core/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,11 @@ export const plugin = createUnplugin<Options | undefined, false>(
162162
return handleTypeDepChange(typeDepToSFCMap.get(ctx.file)!, ctx)
163163
}
164164
if (filter.value(ctx.file)) {
165-
return handleHotUpdate(ctx, options.value)
165+
return handleHotUpdate(
166+
ctx,
167+
options.value,
168+
customElementFilter.value(ctx.file),
169+
)
166170
}
167171
},
168172

@@ -308,6 +312,7 @@ export const plugin = createUnplugin<Options | undefined, false>(
308312
options.value,
309313
context,
310314
ssr,
315+
customElementFilter.value(filename),
311316
)
312317
} else if (query.type === 'style') {
313318
return transformStyle(

src/core/main.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export async function transformMain(
3939
options: ResolvedOptions,
4040
pluginContext: Context,
4141
ssr: boolean,
42-
asCustomElement: boolean,
42+
customElement: boolean,
4343
) {
4444
const { devServer, isProduction, devToolsEnabled } = options
4545

@@ -80,6 +80,7 @@ export async function transformMain(
8080
options,
8181
pluginContext,
8282
ssr,
83+
customElement,
8384
)
8485

8586
// template
@@ -94,6 +95,7 @@ export async function transformMain(
9495
options,
9596
pluginContext,
9697
ssr,
98+
customElement,
9799
))
98100
}
99101

@@ -114,7 +116,7 @@ export async function transformMain(
114116
const stylesCode = await genStyleCode(
115117
descriptor,
116118
pluginContext,
117-
asCustomElement,
119+
customElement,
118120
attachedProps,
119121
)
120122

@@ -279,6 +281,7 @@ async function genTemplateCode(
279281
options: ResolvedOptions,
280282
pluginContext: Context,
281283
ssr: boolean,
284+
customElement: boolean,
282285
) {
283286
const template = descriptor.template!
284287
const hasScoped = descriptor.styles.some((style) => style.scoped)
@@ -293,6 +296,7 @@ async function genTemplateCode(
293296
options,
294297
pluginContext,
295298
ssr,
299+
customElement,
296300
)
297301
} else {
298302
if (template.src) {
@@ -326,6 +330,7 @@ async function genScriptCode(
326330
options: ResolvedOptions,
327331
pluginContext: Context,
328332
ssr: boolean,
333+
customElement: boolean,
329334
): Promise<{
330335
code: string
331336
map: RawSourceMap | undefined
@@ -338,6 +343,7 @@ async function genScriptCode(
338343
descriptor,
339344
options,
340345
ssr,
346+
customElement,
341347
)
342348
if (script) {
343349
// If the script is js/ts and has no external src, it can be directly placed
@@ -385,7 +391,7 @@ async function genScriptCode(
385391
async function genStyleCode(
386392
descriptor: SFCDescriptor,
387393
pluginContext: Context,
388-
asCustomElement: boolean,
394+
customElement: boolean,
389395
attachedProps: [string, string][],
390396
) {
391397
let stylesCode = ``
@@ -410,12 +416,12 @@ async function genStyleCode(
410416
? `&src=${descriptor.id}`
411417
: '&src=true'
412418
: ''
413-
const directQuery = asCustomElement ? `&inline` : ``
419+
const directQuery = customElement ? `&inline` : ``
414420
const scopedQuery = style.scoped ? `&scoped=${descriptor.id}` : ``
415421
const query = `?vue&type=style&index=${i}${srcQuery}${directQuery}${scopedQuery}`
416422
const styleRequest = src + query + attrsQuery
417423
if (style.module) {
418-
if (asCustomElement) {
424+
if (customElement) {
419425
throw new Error(
420426
`<style module> is not supported in custom elements mode.`,
421427
)
@@ -427,7 +433,7 @@ async function genStyleCode(
427433
)
428434
stylesCode += importCode
429435
Object.assign((cssModulesMap ||= {}), nameMap)
430-
} else if (asCustomElement) {
436+
} else if (customElement) {
431437
stylesCode += `\nimport _style_${i} from ${JSON.stringify(
432438
styleRequest,
433439
)}`
@@ -436,7 +442,7 @@ async function genStyleCode(
436442
}
437443
// TODO SSR critical CSS collection
438444
}
439-
if (asCustomElement) {
445+
if (customElement) {
440446
attachedProps.push([
441447
`styles`,
442448
`[${descriptor.styles.map((_, i) => `_style_${i}`).join(',')}]`,

src/core/script.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export function resolveScript(
5555
descriptor: SFCDescriptor,
5656
options: ResolvedOptions,
5757
ssr: boolean,
58+
customElement: boolean,
5859
): SFCScriptBlock | null {
5960
if (!descriptor.script && !descriptor.scriptSetup) {
6061
return null
@@ -77,6 +78,7 @@ export function resolveScript(
7778
genDefaultAs: canInlineMain(framework, descriptor, options)
7879
? scriptIdentifier
7980
: undefined,
81+
customElement,
8082
})
8183

8284
if (!options.isProduction && resolved?.deps) {

src/core/template.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@ export async function transformTemplateAsModule(
1717
options: ResolvedOptions,
1818
pluginContext: Context,
1919
ssr: boolean,
20+
customElement: boolean,
2021
): Promise<{
2122
code: string
2223
map: any
2324
}> {
24-
const result = compile(code, descriptor, options, pluginContext, ssr)
25+
const result = compile(
26+
code,
27+
descriptor,
28+
options,
29+
pluginContext,
30+
ssr,
31+
customElement,
32+
)
2533

2634
let returnCode = result.code
2735
if (
@@ -50,8 +58,16 @@ export function transformTemplateInMain(
5058
options: ResolvedOptions,
5159
pluginContext: Context,
5260
ssr: boolean,
61+
customElement: boolean,
5362
): SFCTemplateCompileResults {
54-
const result = compile(code, descriptor, options, pluginContext, ssr)
63+
const result = compile(
64+
code,
65+
descriptor,
66+
options,
67+
pluginContext,
68+
ssr,
69+
customElement,
70+
)
5571
return {
5672
...result,
5773
code: result.code.replace(
@@ -67,9 +83,16 @@ export function compile(
6783
options: ResolvedOptions,
6884
pluginContext: Context,
6985
ssr: boolean,
86+
customElement: boolean,
7087
) {
7188
const filename = descriptor.filename
72-
resolveScript(pluginContext.framework, descriptor, options, ssr)
89+
resolveScript(
90+
pluginContext.framework,
91+
descriptor,
92+
options,
93+
ssr,
94+
customElement,
95+
)
7396
const result = options.compiler.compileTemplate({
7497
...resolveTemplateCompilerOptions(descriptor, options, ssr)!,
7598
source: code,

0 commit comments

Comments
 (0)