fix: allow css to be written for systemjs output (#5902)

Co-authored-by: patak-dev <matias.capeletto@gmail.com>
This commit is contained in:
shir0u 2022-05-10 20:06:29 +10:00 committed by GitHub
parent 80d113b1d6
commit 780b4f5cdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 70 additions and 2 deletions

View File

@ -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'

View File

@ -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()

View File

@ -0,0 +1,3 @@
#dynamic-css {
color: red;
}

View File

@ -4,4 +4,6 @@
<div id="features-after-corejs-3"></div>
<div id="babel-helpers"></div>
<div id="assets"></div>
<button id="dynamic-css-button">dynamic css</button>
<div id="dynamic-css"></div>
<script type="module" src="./main.js"></script>

View File

@ -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
}

View File

@ -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"
},

View File

@ -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 `<script type="module">` tags and remove `nomodule`
// attrs so that we run the legacy bundle instead.
__test__() {
const indexPath = path.resolve(__dirname, './dist/index.html')
let index = fs.readFileSync(indexPath, 'utf-8')
index = index
.replace(/<script type="module".*?<\/script>/g, '')
.replace(/<script nomodule/g, '<script')
fs.writeFileSync(indexPath, index)
}
}