diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index c13a4c830..1e1bcef68 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -452,7 +452,11 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { // this is a shared CSS-only chunk that is empty. pureCssChunks.add(chunk.fileName) } - if (opts.format === 'es' || opts.format === 'cjs') { + if ( + opts.format === 'es' || + opts.format === 'cjs' || + opts.format === 'system' + ) { chunkCSS = await processChunkCSS(chunkCSS, { inlined: false, minify: true @@ -513,7 +517,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin { .join('|') .replace(/\./g, '\\.') const emptyChunkRE = new RegExp( - opts.format === 'es' + opts.format === 'es' || opts.format === 'system' ? `\\bimport\\s*"[^"]*(?:${emptyChunkFiles})";\n?` : `\\brequire\\(\\s*"[^"]*(?:${emptyChunkFiles})"\\);\n?`, 'g' diff --git a/playground/legacy/__tests__/legacy.spec.ts b/playground/legacy/__tests__/legacy.spec.ts index 89cbadcac..9fd341933 100644 --- a/playground/legacy/__tests__/legacy.spec.ts +++ b/playground/legacy/__tests__/legacy.spec.ts @@ -56,6 +56,17 @@ test('correctly emits styles', async () => { expect(await getColor('#app')).toBe('red') }) +// dynamic import css +test('should load dynamic import with css', async () => { + await page.click('#dynamic-css-button') + await untilUpdated( + () => + page.$eval('#dynamic-css', (node) => window.getComputedStyle(node).color), + 'rgb(255, 0, 0)', + true + ) +}) + if (isBuild) { test('should generate correct manifest', async () => { const manifest = readManifest() diff --git a/playground/legacy/dynamic.css b/playground/legacy/dynamic.css new file mode 100644 index 000000000..160ee45a8 --- /dev/null +++ b/playground/legacy/dynamic.css @@ -0,0 +1,3 @@ +#dynamic-css { + color: red; +} diff --git a/playground/legacy/index.html b/playground/legacy/index.html index d48176646..cbf6242fa 100644 --- a/playground/legacy/index.html +++ b/playground/legacy/index.html @@ -4,4 +4,6 @@
+ +
diff --git a/playground/legacy/main.js b/playground/legacy/main.js index 31579b471..157b6c844 100644 --- a/playground/legacy/main.js +++ b/playground/legacy/main.js @@ -42,6 +42,14 @@ import('./immutable-chunk.js') text('#assets', assets.join('\n')) }) +// dynamic css +document + .querySelector('#dynamic-css-button') + .addEventListener('click', async () => { + await import('./dynamic.css') + text('#dynamic-css', 'dynamic import css') + }) + function text(el, text) { document.querySelector(el).textContent = text } diff --git a/playground/legacy/package.json b/playground/legacy/package.json index f8803564f..4f11c2345 100644 --- a/playground/legacy/package.json +++ b/playground/legacy/package.json @@ -6,6 +6,7 @@ "dev": "vite", "build": "vite build --debug legacy", "build:custom-filename": "vite --config ./vite.config-custom-filename.js build --debug legacy", + "build:dynamic-css": "vite --config ./vite.config-dynamic-css.js build --debug legacy", "debug": "node --inspect-brk ../../packages/vite/bin/vite", "preview": "vite preview" }, diff --git a/playground/legacy/vite.config-dynamic-css.js b/playground/legacy/vite.config-dynamic-css.js new file mode 100644 index 000000000..10565768a --- /dev/null +++ b/playground/legacy/vite.config-dynamic-css.js @@ -0,0 +1,39 @@ +const fs = require('fs') +const path = require('path') +const legacy = require('@vitejs/plugin-legacy').default + +module.exports = { + plugins: [ + legacy({ + targets: 'IE 11' + }) + ], + + build: { + cssCodeSplit: true, + manifest: true, + rollupOptions: { + output: { + chunkFileNames(chunkInfo) { + if (chunkInfo.name === 'immutable-chunk') { + return `assets/${chunkInfo.name}.js` + } + + return `assets/chunk-[name].[hash].js` + } + } + } + }, + + // special test only hook + // for tests, remove `