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(css): handle pure css chunk heuristic with special queries #12091

Merged
merged 2 commits into from
Feb 18, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,15 +464,13 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
let isPureCssChunk = true
const ids = Object.keys(chunk.modules)
for (const id of ids) {
if (
!isCSSRequest(id) ||
cssModuleRE.test(id) ||
commonjsProxyRE.test(id)
) {
if (cssModuleRE.test(id)) {
isPureCssChunk = false
}
if (styles.has(id)) {
chunkCSS += styles.get(id)
} else {
isPureCssChunk = false
bluwy marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
5 changes: 5 additions & 0 deletions playground/css-codesplit/__tests__/css-codesplit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ test('should load all stylesheets', async () => {
expect(await getColor('h1')).toBe('red')
expect(await getColor('h2')).toBe('blue')
expect(await getColor('.dynamic')).toBe('green')
expect(await getColor('.chunk')).toBe('magenta')
})

test('should load dynamic import with inline', async () => {
Expand Down Expand Up @@ -40,4 +41,8 @@ describe.runIf(isBuild)('build', () => {
expect(manifest['index.html'].css.length).toBe(2)
expect(manifest['other.js'].css.length).toBe(1)
})

test('should not mark a css chunk with ?url and normal import as pure css chunk', () => {
expect(findAssetFile(/chunk-.*\.js$/)).toBeTruthy()
})
})
3 changes: 3 additions & 0 deletions playground/css-codesplit/chunk.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.chunk {
color: magenta;
}
2 changes: 2 additions & 0 deletions playground/css-codesplit/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ <h2>This should be blue</h2>
<button class="order-bulk-update">change to green</button>
</p>

<p class="chunk">This should be magenta</p>

<script type="module" src="/main.js"></script>
<div id="app"></div>
6 changes: 6 additions & 0 deletions playground/css-codesplit/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import './style.css'
import './main.css'
import './order'

import './chunk.css'
import chunkCssUrl from './chunk.css?url'

// use this to not treeshake
globalThis.__test_chunkCssUrl = chunkCssUrl

import('./async.css')

import('./inline.css?inline').then((css) => {
Expand Down
5 changes: 5 additions & 0 deletions playground/css-codesplit/other.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
import './style.css'
import './chunk.css'
import chunkCssUrl from './chunk.css?url'

// use this to not treeshake
globalThis.__test_chunkCssUrl = chunkCssUrl
8 changes: 8 additions & 0 deletions playground/css-codesplit/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ module.exports = {
main: resolve(__dirname, './index.html'),
other: resolve(__dirname, './other.js'),
},
output: {
manualChunks(id) {
// make `chunk.css` it's own chunk for easier testing of pure css chunks
if (id.includes('chunk.css')) {
return 'chunk'
}
},
},
},
},
}