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: multiple entries with shared css and no JS #13962

Merged
merged 6 commits into from Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/css.ts
Expand Up @@ -537,7 +537,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
if (cssModuleRE.test(id)) {
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
isPureCssChunk = false
}
} else {
} else if (chunk.modules[id].code) {
// if the module does not have a style, then it's not a pure css chunk.
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
// this is true because in the `transform` hook above, only modules
// that are css gets added to the `styles` map.
Expand Down
12 changes: 12 additions & 0 deletions playground/css-nojs/__tests__/css-nojs.spec.ts
@@ -0,0 +1,12 @@
import { describe, expect, test } from 'vitest'
import { findAssetFile, getColor, isBuild } from '~utils'

test('should load stylesheet', async () => {
expect(await getColor('h1')).toBe('red')
})

describe.runIf(isBuild)('build', () => {
test('should remove empty chunk', async () => {
expect(findAssetFile('main.*.js$')).toMatch(`/* empty css`)
})
})
5 changes: 5 additions & 0 deletions playground/css-nojs/index.html
@@ -0,0 +1,5 @@
<link rel="stylesheet" type="text/css" href="./src/theme.css" />
<script type="module" src="./src/main.js"></script>
<body>
<h1>includes main.js</h1>
</body>
4 changes: 4 additions & 0 deletions playground/css-nojs/nojs.html
@@ -0,0 +1,4 @@
<link rel="stylesheet" type="text/css" href="./src/theme.css" />
<body>
<h1>Share CSS, no JS</h1>
</body>
12 changes: 12 additions & 0 deletions playground/css-nojs/package.json
@@ -0,0 +1,12 @@
{
"name": "@vitejs/test-css-nojs",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
}
}
5 changes: 5 additions & 0 deletions playground/css-nojs/src/main.js
@@ -0,0 +1,5 @@
document.querySelector('#app').innerHTML = `
<div>
<h1>Shared CSS, with JS</h1>
</div>
`
3 changes: 3 additions & 0 deletions playground/css-nojs/src/theme.css
@@ -0,0 +1,3 @@
h1 {
color: red;
}
13 changes: 13 additions & 0 deletions playground/css-nojs/vite.config.js
@@ -0,0 +1,13 @@
import { resolve } from 'node:path'
import { defineConfig } from 'vite'

export default defineConfig({
build: {
rollupOptions: {
input: {
main: resolve(__dirname, 'index.html'),
nojs: resolve(__dirname, 'nojs.html'),
},
},
},
})