Skip to content

Commit e6021f5

Browse files
authored
fix(css): detect css modules from full id for vue virtual sfc styles (#917)
1 parent abe87e7 commit e6021f5

3 files changed

Lines changed: 57 additions & 3 deletions

File tree

packages/css/src/plugin.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ export function CssPlugin(
115115

116116
const isInline = RE_INLINE.test(id)
117117
const isModule =
118-
!isInline &&
119-
cssConfig.css.modules !== false &&
120-
CSS_MODULE_RE.test(cleanId)
118+
!isInline && cssConfig.css.modules !== false && CSS_MODULE_RE.test(id)
121119

122120
const deps: string[] = []
123121
let modules: Record<string, string> | undefined
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## index.mjs
2+
3+
```mjs
4+
import { createElementBlock, normalizeClass, openBlock } from "vue";
5+
//#region MyButton.vue?vue&type=style&index=0&lang.module.css
6+
var MyButton_vue_vue_type_style_index_0_lang_module_default = { "btn": "mod_btn" };
7+
//#endregion
8+
//#region \0/plugin-vue/export-helper
9+
var export_helper_default = (sfc, props) => {
10+
const target = sfc.__vccOpts || sfc;
11+
for (const [key, val] of props) target[key] = val;
12+
return target;
13+
};
14+
//#endregion
15+
//#region MyButton.vue
16+
const _sfc_main = {};
17+
function _sfc_render(_ctx, _cache) {
18+
return openBlock(), createElementBlock("button", { class: normalizeClass(_ctx.$style.btn) }, "Click", 2);
19+
}
20+
var MyButton_default = /* @__PURE__ */ export_helper_default(_sfc_main, [["render", _sfc_render], ["__cssModules", { "$style": MyButton_vue_vue_type_style_index_0_lang_module_default }]]);
21+
//#endregion
22+
export { MyButton_default as MyButton };
23+
24+
```
25+
26+
## style.css
27+
28+
```css
29+
.mod_btn {
30+
color: red;
31+
}
32+
33+
```

tests/issues.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,29 @@ button {
252252
expect(css).toMatch(/\[data-v-[\da-f]+\]/)
253253
})
254254

255+
test('#916', async (context) => {
256+
const Vue = (await import('unplugin-vue/rolldown')).default
257+
const { outputFiles, fileMap } = await testBuild({
258+
context,
259+
files: {
260+
'index.ts': `export { default as MyButton } from './MyButton.vue'`,
261+
'MyButton.vue': `<template><button :class="$style.btn">Click</button></template>
262+
<style module>
263+
.btn { color: red; }
264+
</style>`,
265+
},
266+
options: {
267+
plugins: [Vue({ isProduction: true })],
268+
deps: { skipNodeModulesBundle: true },
269+
css: { modules: { generateScopedName: 'mod_[local]' } },
270+
},
271+
})
272+
expect(outputFiles).toContain('index.mjs')
273+
expect(outputFiles).toContain('style.css')
274+
expect(fileMap['style.css']).toContain('.mod_btn')
275+
expect(fileMap['index.mjs']).toContain('mod_btn')
276+
})
277+
255278
test('#903', async (context) => {
256279
const { outputFiles } = await testBuild({
257280
context,

0 commit comments

Comments
 (0)