Skip to content

Commit 65d333d

Browse files
fix: avoid css leaking into emitted javascript (#3402)
1 parent 9a4183d commit 65d333d

File tree

5 files changed

+18
-2
lines changed

5 files changed

+18
-2
lines changed

packages/playground/css/__tests__/css.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,19 @@ test('linked css', async () => {
3030
})
3131

3232
test('css import from js', async () => {
33+
const importedNoVars = await page.$('.imported-no-vars')
3334
const imported = await page.$('.imported')
3435
const atImport = await page.$('.imported-at-import')
3536

37+
expect(await getColor(importedNoVars)).toBe('magenta')
3638
expect(await getColor(imported)).toBe('green')
3739
expect(await getColor(atImport)).toBe('purple')
3840

41+
editFile('imported-without-variable.css', (code) =>
42+
code.replace('color: magenta', 'color: cyan')
43+
)
44+
await untilUpdated(() => getColor(importedNoVars), 'cyan')
45+
3946
editFile('imported.css', (code) => code.replace('color: green', 'color: red'))
4047
await untilUpdated(() => getColor(imported), 'red')
4148

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.imported-no-vars {
2+
color: magenta;
3+
}

packages/playground/css/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ <h1>CSS</h1>
66
<p class="linked">&lt;link&gt;: This should be blue</p>
77
<p class="linked-at-import">@import in &lt;link&gt;: This should be red</p>
88

9+
<p class="imported-no-vars">
10+
import from js, no vars: This should be magenta
11+
</p>
12+
913
<p class="imported">import from js: This should be green</p>
1014
<p class="imported-at-import">
1115
@import in import from js: This should be purple

packages/playground/css/main.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import './imported-without-variable.css'
2+
13
import css from './imported.css'
24
text('.imported-css', css)
35

packages/vite/src/node/plugins/css.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
245245
} else {
246246
// server only
247247
if (ssr) {
248-
return modulesCode || `export default ${JSON.stringify(css)}`
248+
return modulesCode || `export default ''`
249249
}
250250
return [
251251
`import { updateStyle, removeStyle } from ${JSON.stringify(
@@ -267,7 +267,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
267267
styles.set(id, css)
268268

269269
return {
270-
code: modulesCode || `export default ${JSON.stringify(css)}`,
270+
code: modulesCode || `export default ''`,
271271
map: { mappings: '' },
272272
// avoid the css module from being tree-shaken so that we can retrieve
273273
// it in renderChunk()

0 commit comments

Comments
 (0)