Skip to content

Commit

Permalink
fix(build): fix stale build manifest on watch rebuild (#19361)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Feb 5, 2025
1 parent a5e306f commit fcd5785
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
57 changes: 57 additions & 0 deletions packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { basename, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { stripVTControlCharacters } from 'node:util'
import fsp from 'node:fs/promises'
import colors from 'picocolors'
import { afterEach, describe, expect, test, vi } from 'vitest'
import type {
Expand Down Expand Up @@ -1037,6 +1038,62 @@ describe('onRollupLog', () => {
)
})

test('watch rebuild manifest', async (ctx) => {
// this doesn't actually test watch rebuild
// but it simulates something similar by running two builds for the same environment
const root = resolve(__dirname, 'fixtures/watch-rebuild-manifest')
const builder = await createBuilder({
root,
logLevel: 'error',
environments: {
client: {
build: {
rollupOptions: {
input: '/entry.js',
},
},
},
},
build: {
manifest: true,
},
})

function getManifestKeys(output: RollupOutput) {
return Object.keys(
JSON.parse(
(output.output.find((o) => o.fileName === '.vite/manifest.json') as any)
.source,
),
)
}

const result = await builder.build(builder.environments.client)
expect(getManifestKeys(result as RollupOutput)).toMatchInlineSnapshot(`
[
"dep.js",
"entry.js",
]
`)

const entry = resolve(root, 'entry.js')
const content = await fsp.readFile(entry, 'utf-8')
await fsp.writeFile(
entry,
content.replace(`import('./dep.js')`, `'dep.js removed'`),
)
ctx.onTestFinished(async () => {
await fsp.writeFile(entry, content)
})

const result2 = await builder.build(builder.environments.client)
expect(getManifestKeys(result2 as RollupOutput)).toMatchInlineSnapshot(`
[
"entry.js",
]
`)
})

/**
* for each chunks in output1, if there's a chunk in output2 with the same fileName,
* ensure that the chunk code is the same. if not, the chunk hash should have changed.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('dep.js')
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log('entry.js')
console.log(import('./dep.js'))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
1 change: 1 addition & 0 deletions packages/vite/src/node/plugins/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function manifestPlugin(): Plugin {
manifest: {} as Manifest,
outputCount: 0,
reset() {
this.manifest = {}
this.outputCount = 0
},
}
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fcd5785

Please sign in to comment.