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: allow css to be written for systemjs output #5902

Merged
merged 2 commits into from May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down
11 changes: 11 additions & 0 deletions playground/legacy/__tests__/legacy.spec.ts
Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions playground/legacy/dynamic.css
@@ -0,0 +1,3 @@
#dynamic-css {
color: red;
}
2 changes: 2 additions & 0 deletions playground/legacy/index.html
Expand Up @@ -4,4 +4,6 @@ <h1 id="app"></h1>
<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>
8 changes: 8 additions & 0 deletions playground/legacy/main.js
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions playground/legacy/package.json
Expand Up @@ -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"
},
Expand Down
39 changes: 39 additions & 0 deletions 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 `<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)
}
}