Skip to content

Commit

Permalink
fix(build): support cssCodeSplit for cjs format, fix #2575 (#2621)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
underfin and antfu authored Apr 4, 2021
1 parent 2e018ff commit 2a89c57
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { findAssetFile, getColor, isBuild, readManifest } from '../../testUtils'

test('should load both stylesheets', async () => {
expect(await getColor('h1')).toBe('red')
expect(await getColor('h2')).toBe('blue')
})

if (isBuild) {
test('should remove empty chunk', async () => {
expect(findAssetFile(/style.*\.js$/)).toBe('')
expect(findAssetFile('main.*.js$')).toMatch(`/* empty css`)
expect(findAssetFile('other.*.js$')).toMatch(`/* empty css`)
})

test('should generate correct manifest', async () => {
const manifest = readManifest()
expect(manifest['index.html'].css.length).toBe(2)
expect(manifest['other.js'].css.length).toBe(1)
})
}
2 changes: 2 additions & 0 deletions packages/playground/css-codesplit-cjs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<script type="module" src="/main.js"></script>
<div id="app"></div>
3 changes: 3 additions & 0 deletions packages/playground/css-codesplit-cjs/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 {
color: red;
}
6 changes: 6 additions & 0 deletions packages/playground/css-codesplit-cjs/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import './style.css'
import './main.css'

document.getElementById(
'app'
).innerHTML = `<h1>This should be red</h1><h2>This should be blue</h2>`
1 change: 1 addition & 0 deletions packages/playground/css-codesplit-cjs/other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.css'
11 changes: 11 additions & 0 deletions packages/playground/css-codesplit-cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "test-css-codesplit-cjs",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../vite/bin/vite",
"serve": "vite preview"
}
}
3 changes: 3 additions & 0 deletions packages/playground/css-codesplit-cjs/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h2 {
color: blue;
}
20 changes: 20 additions & 0 deletions packages/playground/css-codesplit-cjs/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { resolve } = require('path')

module.exports = {
build: {
outDir: './dist',
manifest: true,
rollupOptions: {
input: {
main: resolve(__dirname, './index.html'),
other: resolve(__dirname, './other.js')
},
treeshake: false,
output: {
format: 'cjs',
freeze: false,
externalLiveBindings: false
}
}
}
}
6 changes: 4 additions & 2 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
// this is a shared CSS-only chunk that is empty.
pureCssChunks.add(chunk.fileName)
}
if (opts.format === 'es') {
if (opts.format === 'es' || opts.format === 'cjs') {
chunkCSS = await processChunkCSS(chunkCSS, {
inlined: false,
minify: true
Expand Down Expand Up @@ -397,7 +397,9 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
.join('|')
.replace(/\./g, '\\.')
const emptyChunkRE = new RegExp(
`\\bimport\\s*"[^"]*(?:${emptyChunkFiles})";\n?`,
opts.format === 'es'
? `\\bimport\\s*"[^"]*(?:${emptyChunkFiles})";\n?`
: `\\brequire\\(\\s*"[^"]*(?:${emptyChunkFiles})"\\);\n?`,
'g'
)
for (const file in bundle) {
Expand Down

0 comments on commit 2a89c57

Please sign in to comment.