8000 feat(css): tree shake scoped styles · unplugin/unplugin-vue@c615656 · GitHub
[go: up one dir, main page]

Skip to content

Commit c615656

Browse files
committed
feat(css): tree shake scoped styles
ref vitejs/vite-plugin-vue@333094f
1 parent 6ccadd3 commit c615656

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

src/core/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
getDescriptor,
3030
getSrcDescriptor,
3131
getTempSrcDescriptor,
32+
type ExtendedSFCDescriptor,
3233
} from './utils/descriptorCache'
3334
import { parseVueRequest } from './utils/query'
3435
import type {
@@ -462,7 +463,7 @@ export const plugin: UnpluginInstance<Options | undefined, false> =
462463
)
463464
} else {
464465
// sub block request
465-
const descriptor = query.src
466+
const descriptor: ExtendedSFCDescriptor = query.src
466467
? getSrcDescriptor(filename, query) ||
467468
getTempSrcDescriptor(filename, query)
468469
: getDescriptor(filename, options.value)!

src/core/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ export async function transformMain(
272272

273273
return {
274274
code: resolvedCode,
275-
map: resolvedMap || {
275+
map: (resolvedMap || {
276276
mappings: '',
277-
},
277+
}) as any,
278278
meta: {
279279
vite: {
280280
lang: descriptor.script?.lang || descriptor.scriptSetup?.lang || 'js',

src/core/style.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { formatPostcssSourceMap } from 'vite'
2+
import type { ExtendedSFCDescriptor } from './utils/descriptorCache'
23
import type { ResolvedOptions } from '.'
34
import type { ExistingRawSourceMap } from 'rollup'
45
import type { RawSourceMap } from 'source-map-js'
56
import type { UnpluginContext } from 'unplugin'
6-
import type { SFCDescriptor } from 'vue/compiler-sfc'
77

88
export async function transformStyle(
99
code: string,
10-
descriptor: SFCDescriptor,
10+
descriptor: ExtendedSFCDescriptor,
1111
index: number,
1212
options: ResolvedOptions,
1313
context: UnpluginContext,
1414
filename: string,
15-
): Promise<{ code: string; map: any } | null> {
15+
): Promise<{ code: string; map: any; meta: any } | null> {
1616
const block = descriptor.styles[index]
1717
// vite already handles pre-processors and CSS module so this is only
1818
// applying SFC-specific transforms like scoped mode and CSS vars rewrite (v-bind(var))
@@ -62,5 +62,13 @@ export async function transformStyle(
6262
return {
6363
code: result.code,
6464
map,
65+
meta:
66+
block.scoped && !descriptor.isTemp
8000 67+
? {
68+
vite: {
69+
cssScopeTo: [descriptor.filename, 'default'],
70+
},
71+
}
72+
: undefined,
6573
}
6674
}

src/core/utils/descriptorCache.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ export function invalidateDescriptor(filename: string, hmr = false): void {
7676
}
7777
}
7878

79+
export interface ExtendedSFCDescriptor extends SFCDescriptor {
80+
isTemp?: boolean
81+
}
82+
7983
export function getDescriptor(
8084
filename: string,
8185
options: ResolvedOptions,
@@ -114,21 +118,22 @@ export function getSrcDescriptor(
114118
export function getTempSrcDescriptor(
115119
filename: string,
116120
query: VueQuery,
117-
): SFCDescriptor {
121+
): ExtendedSFCDescriptor {
118122
// this is only used for pre-compiled <style src> with scoped flag
123+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
119124
return {
120125
filename,
121126
id: query.id || '',
122127
styles: [
123128
{
124129
scoped: query.scoped,
125130
loc: {
126-
// @ts-expect-error
127131
start: { line: 0, column: 0 },
128132
},
129-
},
133+
} as any,
130134
],
131-
}
135+
isTemp: true,
136+
} as ExtendedSFCDescriptor
132137
}
133138

134139
export function setSrcDescriptor(

0 commit comments

Comments
 (0)
0