Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid css leaking into emitted javascript #3402

Merged
merged 6 commits into from
May 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
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