Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(css): tree shake scoped styles
  • Loading branch information
sapphi-red committed Mar 3, 2025
commit bb5b18f0b3f42773d670de0ba8d4c433c60249ac
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
7 changes: 7 additions & 0 deletions packages/plugin-vue/src/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,12 @@ export async function transformStyle(
return {
code: result.code,
map: map,
meta: block.scoped
? {
vite: {
cssScopeTo: [descriptor.filename, 'default'],
},
}
: undefined,
}
}
6 changes: 6 additions & 0 deletions playground/vue/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ 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)
}

const TsGeneric = defineAsyncComponent(() => import('./TsGeneric.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 @@ -441,3 +442,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')
})
Loading