Skip to content

Commit 3891d05

Browse files
thebanjomaticAdam Hines
and
Adam Hines
authored
fix(css): don't mock css-module if inline is specified (#3952)
Co-authored-by: Adam Hines <[email protected]>
1 parent 5a07cff commit 3891d05

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

packages/vitest/src/node/plugins/cssEnabler.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { toArray } from '../../utils'
77
const cssLangs = '\\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\\?)'
88
const cssLangRE = new RegExp(cssLangs)
99
const cssModuleRE = new RegExp(`\\.module${cssLangs}`)
10+
const cssInlineRE = /[?&]inline(&|$)/
1011

1112
function isCSS(id: string) {
1213
return cssLangRE.test(id)
@@ -16,6 +17,12 @@ function isCSSModule(id: string) {
1617
return cssModuleRE.test(id)
1718
}
1819

20+
// inline css requests are expected to just return the
21+
// string content directly and not the proxy module
22+
function isInline(id: string) {
23+
return cssInlineRE.test(id)
24+
}
25+
1926
function getCSSModuleProxyReturn(strategy: CSSModuleScopeStrategy, filename: string) {
2027
if (strategy === 'non-scoped')
2128
return 'style'
@@ -55,7 +62,7 @@ export function CSSEnablerPlugin(ctx: { config: ResolvedConfig }): VitePlugin[]
5562
if (!isCSS(id) || shouldProcessCSS(id))
5663
return
5764

58-
if (isCSSModule(id)) {
65+
if (isCSSModule(id) && !isInline(id)) {
5966
// return proxy for css modules, so that imported module has names:
6067
// styles.foo returns a "foo" instead of "undefined"
6168
// we don't use code content to generate hash for "scoped", because it's empty

test/css/test/process-inline.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, test } from 'vitest'
2+
import { useRemoveStyles } from './utils'
3+
4+
describe('processing inline css', () => {
5+
useRemoveStyles()
6+
7+
test('doesn\'t apply css', async () => {
8+
await import('../src/App.module.css?inline')
9+
10+
const element = document.createElement('div')
11+
element.className = 'main'
12+
const computed = window.getComputedStyle(element)
13+
expect(computed.display, 'css is not processed').toBe('block')
14+
})
15+
16+
test('returns a string', async () => {
17+
const { default: style } = await import('../src/App.module.css?inline')
18+
expect(typeof style).toBe('string')
19+
})
20+
})

test/css/testing.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import { startVitest } from 'vitest/node'
33
const configs = [
44
['test/default-css', {}],
55
['test/process-css', { include: [/App\.css/] }],
6-
['test/process-module', { include: [/App\.module\.css/] }],
6+
[['test/process-module', 'test/process-inline'], { include: [/App\.module\.css/] }],
77
['test/scope-module', { include: [/App\.module\.css/], modules: { classNameStrategy: 'scoped' } }],
88
['test/non-scope-module', { include: [/App\.module\.css/], modules: { classNameStrategy: 'non-scoped' } }],
99
]
1010

1111
async function runTests() {
1212
for (const [name, config] of configs) {
13-
await startVitest('test', [name], {
13+
const names = Array.isArray(name) ? name : [name];
14+
await startVitest('test', names, {
1415
run: true,
1516
css: config,
1617
update: false,

0 commit comments

Comments
 (0)