Skip to content

Commit fcd5785

Browse files
authored
fix(build): fix stale build manifest on watch rebuild (#19361)
1 parent a5e306f commit fcd5785

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed

packages/vite/src/node/__tests__/build.spec.ts

+57
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { basename, resolve } from 'node:path'
22
import { fileURLToPath } from 'node:url'
33
import { stripVTControlCharacters } from 'node:util'
4+
import fsp from 'node:fs/promises'
45
import colors from 'picocolors'
56
import { afterEach, describe, expect, test, vi } from 'vitest'
67
import type {
@@ -1037,6 +1038,62 @@ describe('onRollupLog', () => {
10371038
)
10381039
})
10391040

1041+
test('watch rebuild manifest', async (ctx) => {
1042+
// this doesn't actually test watch rebuild
1043+
// but it simulates something similar by running two builds for the same environment
1044+
const root = resolve(__dirname, 'fixtures/watch-rebuild-manifest')
1045+
const builder = await createBuilder({
1046+
root,
1047+
logLevel: 'error',
1048+
environments: {
1049+
client: {
1050+
build: {
1051+
rollupOptions: {
1052+
input: '/entry.js',
1053+
},
1054+
},
1055+
},
1056+
},
1057+
build: {
1058+
manifest: true,
1059+
},
1060+
})
1061+
1062+
function getManifestKeys(output: RollupOutput) {
1063+
return Object.keys(
1064+
JSON.parse(
1065+
(output.output.find((o) => o.fileName === '.vite/manifest.json') as any)
1066+
.source,
1067+
),
1068+
)
1069+
}
1070+
1071+
const result = await builder.build(builder.environments.client)
1072+
expect(getManifestKeys(result as RollupOutput)).toMatchInlineSnapshot(`
1073+
[
1074+
"dep.js",
1075+
"entry.js",
1076+
]
1077+
`)
1078+
1079+
const entry = resolve(root, 'entry.js')
1080+
const content = await fsp.readFile(entry, 'utf-8')
1081+
await fsp.writeFile(
1082+
entry,
1083+
content.replace(`import('./dep.js')`, `'dep.js removed'`),
1084+
)
1085+
ctx.onTestFinished(async () => {
1086+
await fsp.writeFile(entry, content)
1087+
})
1088+
1089+
const result2 = await builder.build(builder.environments.client)
1090+
expect(getManifestKeys(result2 as RollupOutput)).toMatchInlineSnapshot(`
1091+
[
1092+
"entry.js",
1093+
]
1094+
`)
1095+
})
1096+
10401097
/**
10411098
* for each chunks in output1, if there's a chunk in output2 with the same fileName,
10421099
* ensure that the chunk code is the same. if not, the chunk hash should have changed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('dep.js')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
console.log('entry.js')
2+
console.log(import('./dep.js'))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export function manifestPlugin(): Plugin {
3232
manifest: {} as Manifest,
3333
outputCount: 0,
3434
reset() {
35+
this.manifest = {}
3536
this.outputCount = 0
3637
},
3738
}

pnpm-lock.yaml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)