Skip to content

Commit f48255e

Browse files
authored
fix(legacy): fix conflict with the modern build on css emitting (#6584)
Fixes #3296 Supersedes #3317 The asset emitting conflict may also exist for other types of assets, but let's fix the CSS one first. The conflict here is due to the `hasEmitted` flag that was originally intended to avoid duplicated CSS for multiple output formats 6bce108#diff-2cfbd4f4d8c32727cd8e1a561cffbde0b384a3ce0789340440e144f9d64c10f6R262-R263 When the legacy plugin is used, the flag was set to `true` for the emitted CSS of the legacy bundle. But the legacy plugin would remove all its emitted assets later to avoid duplication. So this logic results in no CSS to be actually emitted. In this PR, I used a `__vite_skip_asset_emit__` flag to prevent the CSS `generateBundle` from executing for the legacy build. If other asset emitting plugins encounter similar issues, this flag can be reused.
1 parent 8338e26 commit f48255e

File tree

6 files changed

+30
-1
lines changed

6 files changed

+30
-1
lines changed

packages/playground/legacy/__tests__/legacy.spec.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import {
2+
listAssets,
23
findAssetFile,
34
isBuild,
45
readManifest,
5-
untilUpdated
6+
untilUpdated,
7+
getColor
68
} from '../../testUtils'
79

810
test('should work', async () => {
@@ -50,6 +52,10 @@ test('generates assets', async () => {
5052
)
5153
})
5254

55+
test('correctly emits styles', async () => {
56+
expect(await getColor('#app')).toBe('red')
57+
})
58+
5359
if (isBuild) {
5460
test('should generate correct manifest', async () => {
5561
const manifest = readManifest()
@@ -73,4 +79,8 @@ if (isBuild) {
7379
expect(findAssetFile(/index\./)).not.toMatch(terserPatt)
7480
expect(findAssetFile(/polyfills-legacy/)).toMatch(terserPatt)
7581
})
82+
83+
test('should emit css file', async () => {
84+
expect(listAssets().some((filename) => filename.endsWith('.css')))
85+
})
7686
}

packages/playground/legacy/main.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import './style.css'
2+
13
async function run() {
24
const { fn } = await import('./async.js')
35
fn()

packages/playground/legacy/style.css

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

packages/playground/legacy/vite.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010
],
1111

1212
build: {
13+
cssCodeSplit: false,
1314
manifest: true,
1415
rollupOptions: {
1516
output: {

packages/plugin-legacy/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ function viteLegacyPlugin(options = {}) {
286286
// entirely.
287287
opts.__vite_force_terser__ = true
288288

289+
// @ts-ignore
290+
// In the `generateBundle` hook,
291+
// we'll delete the assets from the legacy bundle to avoid emitting duplicate assets.
292+
// But that's still a waste of computing resource.
293+
// So we add this flag to avoid emitting the asset in the first place whenever possible.
294+
opts.__vite_skip_asset_emit__ = true
295+
296+
// @ts-ignore avoid emitting assets for legacy bundle
289297
const needPolyfills =
290298
options.polyfills !== false && !Array.isArray(options.polyfills)
291299

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

+5
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,11 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
468468
},
469469

470470
async generateBundle(opts, bundle) {
471+
// @ts-ignore asset emits are skipped in legacy bundle
472+
if (opts.__vite_skip_asset_emit__) {
473+
return
474+
}
475+
471476
// remove empty css chunks and their imports
472477
if (pureCssChunks.size) {
473478
const emptyChunkFiles = [...pureCssChunks]

0 commit comments

Comments
 (0)