Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(css): tree shake scoped styles #533

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/plugin-vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { version } from '../package.json'
import { resolveCompiler } from './compiler'
import { parseVueRequest } from './utils/query'
import {
type ExtendedSFCDescriptor,
getDescriptor,
getSrcDescriptor,
getTempSrcDescriptor,
Expand Down Expand Up @@ -399,7 +400,7 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin<Api> {
)
} else {
// sub block request
const descriptor = query.src
const descriptor: ExtendedSFCDescriptor = query.src
? getSrcDescriptor(filename, query) ||
getTempSrcDescriptor(filename, query)
: getDescriptor(filename, options.value)!
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-vue/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ export async function transformMain(

return {
code: resolvedCode,
map: resolvedMap || {
map: (resolvedMap || {
mappings: '',
},
}) as any,
meta: {
vite: {
lang: descriptor.script?.lang || descriptor.scriptSetup?.lang || 'js',
Expand Down
12 changes: 10 additions & 2 deletions packages/plugin-vue/src/style.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { SFCDescriptor } from 'vue/compiler-sfc'
import type { ExistingRawSourceMap, TransformPluginContext } from 'rollup'
import type { RawSourceMap } from 'source-map-js'
import { formatPostcssSourceMap } from 'vite'
import type { ExtendedSFCDescriptor } from './utils/descriptorCache'
import type { ResolvedOptions } from './index'

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export async function transformStyle(
code: string,
descriptor: SFCDescriptor,
descriptor: ExtendedSFCDescriptor,
index: number,
options: ResolvedOptions,
pluginContext: TransformPluginContext,
Expand Down Expand Up @@ -62,5 +62,13 @@ export async function transformStyle(
return {
code: result.code,
map: map,
meta:
block.scoped && !descriptor.isTemp
? {
vite: {
cssScopeTo: [descriptor.filename, 'default'],
},
}
: undefined,
}
}
11 changes: 8 additions & 3 deletions packages/plugin-vue/src/utils/descriptorCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export function invalidateDescriptor(filename: string, hmr = false): void {
}
}

export interface ExtendedSFCDescriptor extends SFCDescriptor {
isTemp?: boolean
}

export function getDescriptor(
filename: string,
options: ResolvedOptions,
Expand Down Expand Up @@ -113,7 +117,7 @@ export function getSrcDescriptor(
export function getTempSrcDescriptor(
filename: string,
query: VueQuery,
): SFCDescriptor {
): ExtendedSFCDescriptor {
// this is only used for pre-compiled <style src> with scoped flag
return {
filename,
Expand All @@ -124,9 +128,10 @@ export function getTempSrcDescriptor(
loc: {
start: { line: 0, column: 0 },
},
},
} as any,
],
} as SFCDescriptor
isTemp: true,
} as ExtendedSFCDescriptor
}

export function setSrcDescriptor(
Expand Down
4 changes: 3 additions & 1 deletion playground/ssr-vue/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ export default defineConfig(({ command, ssrBuild, isSsrBuild }) => ({
) {
return {
code:
`import { __ssr_vue_processAssetPath } from '${virtualId}';__ssr_vue_processAssetPath;` +
`import { __ssr_vue_processAssetPath } from '${virtualId}';` +
// make `__ssr_vue_processAssetPath` not to be tree-shaken (`globalThis.__ssr_vue` doesn't exist)
`globalThis.__ssr_vue?.(__ssr_vue_processAssetPath);` +
code,
sourcemap: null, // no sourcemap support to speed up CI
}
Expand Down
7 changes: 7 additions & 0 deletions playground/vue/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ import PreCompiledExternalScoped from './pre-compiled/external-scoped.vue'
import PreCompiledExternalCssModules from './pre-compiled/external-cssmodules.vue'
import ParserOptions from './ParserOptions.vue'
import HmrCircularReference from './HmrCircularReference.vue'
import TreeShakeScopedStyle from './TreeShakeScopedStyle.vue'

// NOTE: this function is not used intentionally
function foo() {
console.log(TreeShakeScopedStyle)
}

import ExportTypeProps1 from './ExportTypeProps1.vue'
import ExportTypeProps2 from './ExportTypeProps2.vue'

Expand Down
9 changes: 9 additions & 0 deletions playground/vue/TreeShakeScopedStyle.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<div>tree shake scoped style</div>
</template>

<style scoped>
.tree-shake-scoped-style {
color: red;
}
</style>
6 changes: 6 additions & 0 deletions playground/vue/__tests__/vue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { version } from 'vue'
import {
browserLogs,
editFile,
findAssetFile,
getBg,
getColor,
isBuild,
Expand Down Expand Up @@ -455,3 +456,8 @@ describe('template parse options', () => {
)
})
})

test.runIf(isBuild)('scoped style should be tree-shakeable', async () => {
const indexCss = findAssetFile(/index-[\w-]+\.css/)
expect(indexCss).not.toContain('.tree-shake-scoped-style')
})