Skip to content

Commit

Permalink
fix: avoid css leaking into emitted javascript (vitejs#3402)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdinando-ferreira authored and ygj6 committed Jun 1, 2021
1 parent c16f9aa commit dfccbf6
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/playground/css/__tests__/css.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,19 @@ test('linked css', async () => {
})

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

expect(await getColor(importedNoVars)).toBe('magenta')
expect(await getColor(imported)).toBe('green')
expect(await getColor(atImport)).toBe('purple')

editFile('imported-without-variable.css', (code) =>
code.replace('color: magenta', 'color: cyan')
)
await untilUpdated(() => getColor(importedNoVars), 'cyan')

editFile('imported.css', (code) => code.replace('color: green', 'color: red'))
await untilUpdated(() => getColor(imported), 'red')

Expand Down
3 changes: 3 additions & 0 deletions packages/playground/css/imported-without-variable.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.imported-no-vars {
color: magenta;
}
4 changes: 4 additions & 0 deletions packages/playground/css/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ <h1>CSS</h1>
<p class="linked">&lt;link&gt;: This should be blue</p>
<p class="linked-at-import">@import in &lt;link&gt;: This should be red</p>

<p class="imported-no-vars">
import from js, no vars: This should be magenta
</p>

<p class="imported">import from js: This should be green</p>
<p class="imported-at-import">
@import in import from js: This should be purple
Expand Down
2 changes: 2 additions & 0 deletions packages/playground/css/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import './imported-without-variable.css'

import css from './imported.css'
text('.imported-css', css)

Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
} else {
// server only
if (ssr) {
return modulesCode || `export default ${JSON.stringify(css)}`
return modulesCode || `export default ''`
}
return [
`import { updateStyle, removeStyle } from ${JSON.stringify(
Expand All @@ -267,7 +267,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
styles.set(id, css)

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

0 comments on commit dfccbf6

Please sign in to comment.