Skip to content

Commit b9ee620

Browse files
adamkoppedesapphi-red
andauthoredNov 20, 2023
fix(legacy): error in build with --watch and manifest enabled (#14450)
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
1 parent 03c371e commit b9ee620

File tree

6 files changed

+81
-6
lines changed

6 files changed

+81
-6
lines changed
 

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -606,11 +606,12 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
606606
}
607607

608608
if (config.build.cssCodeSplit) {
609-
if (isPureCssChunk) {
610-
// this is a shared CSS-only chunk that is empty.
611-
pureCssChunks.add(chunk)
612-
}
613609
if (opts.format === 'es' || opts.format === 'cjs') {
610+
if (isPureCssChunk) {
611+
// this is a shared CSS-only chunk that is empty.
612+
pureCssChunks.add(chunk)
613+
}
614+
614615
const isEntry = chunk.isEntry && isPureCssChunk
615616
const cssAssetName = ensureFileExt(chunk.name, '.css')
616617
const originalFilename = getChunkOriginalFileName(

‎packages/vite/src/node/plugins/manifest.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,14 @@ export function manifestPlugin(config: ResolvedConfig): Plugin {
111111
const fileNameToAssetMeta = new Map<string, GeneratedAssetMeta>()
112112
const assets = generatedAssets.get(config)!
113113
assets.forEach((asset, referenceId) => {
114-
const fileName = this.getFileName(referenceId)
115-
fileNameToAssetMeta.set(fileName, asset)
114+
try {
115+
const fileName = this.getFileName(referenceId)
116+
fileNameToAssetMeta.set(fileName, asset)
117+
} catch (error: unknown) {
118+
// The asset was generated as part of a different output option.
119+
// It was already handled during the previous run of this plugin.
120+
assets.delete(referenceId)
121+
}
116122
})
117123

118124
const fileNameToAsset = new Map<string, ManifestChunk>()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { expect, test } from 'vitest'
2+
import {
3+
editFile,
4+
findAssetFile,
5+
isBuild,
6+
notifyRebuildComplete,
7+
readManifest,
8+
watcher,
9+
} from '~utils'
10+
11+
test.runIf(isBuild)('rebuilds styles only entry on change', async () => {
12+
expect(findAssetFile(/style-only-entry-.+\.css/, 'watch')).toContain(
13+
'hotpink',
14+
)
15+
expect(findAssetFile(/style-only-entry-legacy-.+\.js/, 'watch')).toContain(
16+
'hotpink',
17+
)
18+
expect(findAssetFile(/polyfills-legacy-.+\.js/, 'watch')).toBeTruthy()
19+
const numberOfManifestEntries = Object.keys(readManifest('watch')).length
20+
expect(numberOfManifestEntries).toBe(3)
21+
22+
editFile(
23+
'style-only-entry.css',
24+
(originalContents) => originalContents.replace('hotpink', 'lightpink'),
25+
true,
26+
)
27+
await notifyRebuildComplete(watcher)
28+
29+
const updatedManifest = readManifest('watch')
30+
expect(Object.keys(updatedManifest)).toHaveLength(numberOfManifestEntries)
31+
32+
// We must use the file referenced in the manifest here,
33+
// since there'll be different versions of the file with different hashes.
34+
const reRenderedCssFile = findAssetFile(
35+
updatedManifest['style-only-entry.css']!.file.substring('assets/'.length),
36+
'watch',
37+
)
38+
expect(reRenderedCssFile).toContain('lightpink')
39+
const reRenderedCssLegacyFile = findAssetFile(
40+
updatedManifest['style-only-entry-legacy.css']!.file.substring(
41+
'assets/'.length,
42+
),
43+
'watch',
44+
)
45+
expect(reRenderedCssLegacyFile).toContain('lightpink')
46+
expect(findAssetFile(/polyfills-legacy-.+\.js/, 'watch')).toBeTruthy()
47+
})

‎playground/legacy/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"build:multiple-output": "vite --config ./vite.config-multiple-output.js build",
1111
"build:no-polyfills": "vite --config ./vite.config-no-polyfills.js build",
1212
"build:no-polyfills-no-systemjs": "vite --config ./vite.config-no-polyfills-no-systemjs.js build",
13+
"build:watch": "vite --config ./vite.config-watch.js build --debug legacy",
1314
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
1415
"preview": "vite preview"
1516
},
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:root {
2+
background: hotpink;
3+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { resolve } from 'node:path'
2+
import legacy from '@vitejs/plugin-legacy'
3+
import { defineConfig } from 'vite'
4+
5+
export default defineConfig({
6+
plugins: [legacy()],
7+
build: {
8+
manifest: true,
9+
rollupOptions: {
10+
input: {
11+
'style-only-entry': resolve(__dirname, 'style-only-entry.css'),
12+
},
13+
},
14+
watch: {},
15+
outDir: 'dist/watch',
16+
},
17+
})

0 commit comments

Comments
 (0)
Please sign in to comment.