Skip to content

Commit 780b4f5

Browse files
shir0upatak-dev
andauthored
fix: allow css to be written for systemjs output (#5902)
Co-authored-by: patak-dev <[email protected]>
1 parent 80d113b commit 780b4f5

File tree

7 files changed

+70
-2
lines changed

7 files changed

+70
-2
lines changed

packages/vite/src/node/plugins/css.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,11 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
452452
// this is a shared CSS-only chunk that is empty.
453453
pureCssChunks.add(chunk.fileName)
454454
}
455-
if (opts.format === 'es' || opts.format === 'cjs') {
455+
if (
456+
opts.format === 'es' ||
457+
opts.format === 'cjs' ||
458+
opts.format === 'system'
459+
) {
456460
chunkCSS = await processChunkCSS(chunkCSS, {
457461
inlined: false,
458462
minify: true
@@ -513,7 +517,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
513517
.join('|')
514518
.replace(/\./g, '\\.')
515519
const emptyChunkRE = new RegExp(
516-
opts.format === 'es'
520+
opts.format === 'es' || opts.format === 'system'
517521
? `\\bimport\\s*"[^"]*(?:${emptyChunkFiles})";\n?`
518522
: `\\brequire\\(\\s*"[^"]*(?:${emptyChunkFiles})"\\);\n?`,
519523
'g'

playground/legacy/__tests__/legacy.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ test('correctly emits styles', async () => {
5656
expect(await getColor('#app')).toBe('red')
5757
})
5858

59+
// dynamic import css
60+
test('should load dynamic import with css', async () => {
61+
await page.click('#dynamic-css-button')
62+
await untilUpdated(
63+
() =>
64+
page.$eval('#dynamic-css', (node) => window.getComputedStyle(node).color),
65+
'rgb(255, 0, 0)',
66+
true
67+
)
68+
})
69+
5970
if (isBuild) {
6071
test('should generate correct manifest', async () => {
6172
const manifest = readManifest()

playground/legacy/dynamic.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#dynamic-css {
2+
color: red;
3+
}

playground/legacy/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ <h1 id="app"></h1>
44
<div id="features-after-corejs-3"></div>
55
<div id="babel-helpers"></div>
66
<div id="assets"></div>
7+
<button id="dynamic-css-button">dynamic css</button>
8+
<div id="dynamic-css"></div>
79
<script type="module" src="./main.js"></script>

playground/legacy/main.js

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ import('./immutable-chunk.js')
4242
text('#assets', assets.join('\n'))
4343
})
4444

45+
// dynamic css
46+
document
47+
.querySelector('#dynamic-css-button')
48+
.addEventListener('click', async () => {
49+
await import('./dynamic.css')
50+
text('#dynamic-css', 'dynamic import css')
51+
})
52+
4553
function text(el, text) {
4654
document.querySelector(el).textContent = text
4755
}

playground/legacy/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"dev": "vite",
77
"build": "vite build --debug legacy",
88
"build:custom-filename": "vite --config ./vite.config-custom-filename.js build --debug legacy",
9+
"build:dynamic-css": "vite --config ./vite.config-dynamic-css.js build --debug legacy",
910
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
1011
"preview": "vite preview"
1112
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const legacy = require('@vitejs/plugin-legacy').default
4+
5+
module.exports = {
6+
plugins: [
7+
legacy({
8+
targets: 'IE 11'
9+
})
10+
],
11+
12+
build: {
13+
cssCodeSplit: true,
14+
manifest: true,
15+
rollupOptions: {
16+
output: {
17+
chunkFileNames(chunkInfo) {
18+
if (chunkInfo.name === 'immutable-chunk') {
19+
return `assets/${chunkInfo.name}.js`
20+
}
21+
22+
return `assets/chunk-[name].[hash].js`
23+
}
24+
}
25+
}
26+
},
27+
28+
// special test only hook
29+
// for tests, remove `<script type="module">` tags and remove `nomodule`
30+
// attrs so that we run the legacy bundle instead.
31+
__test__() {
32+
const indexPath = path.resolve(__dirname, './dist/index.html')
33+
let index = fs.readFileSync(indexPath, 'utf-8')
34+
index = index
35+
.replace(/<script type="module".*?<\/script>/g, '')
36+
.replace(/<script nomodule/g, '<script')
37+
fs.writeFileSync(indexPath, index)
38+
}
39+
}

0 commit comments

Comments
 (0)