diff --git a/packages/vite/src/node/__tests__/build.spec.ts b/packages/vite/src/node/__tests__/build.spec.ts index d0a9750763c055..f36811178cc238 100644 --- a/packages/vite/src/node/__tests__/build.spec.ts +++ b/packages/vite/src/node/__tests__/build.spec.ts @@ -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 { @@ -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. diff --git a/packages/vite/src/node/__tests__/fixtures/watch-rebuild-manifest/dep.js b/packages/vite/src/node/__tests__/fixtures/watch-rebuild-manifest/dep.js new file mode 100644 index 00000000000000..b2d751bb9596b9 --- /dev/null +++ b/packages/vite/src/node/__tests__/fixtures/watch-rebuild-manifest/dep.js @@ -0,0 +1 @@ +console.log('dep.js') diff --git a/packages/vite/src/node/__tests__/fixtures/watch-rebuild-manifest/entry.js b/packages/vite/src/node/__tests__/fixtures/watch-rebuild-manifest/entry.js new file mode 100644 index 00000000000000..518e2ea9f42ffa --- /dev/null +++ b/packages/vite/src/node/__tests__/fixtures/watch-rebuild-manifest/entry.js @@ -0,0 +1,2 @@ +console.log('entry.js') +console.log(import('./dep.js')) diff --git a/packages/vite/src/node/__tests__/fixtures/watch-rebuild-manifest/package.json b/packages/vite/src/node/__tests__/fixtures/watch-rebuild-manifest/package.json new file mode 100644 index 00000000000000..3dbc1ca591c055 --- /dev/null +++ b/packages/vite/src/node/__tests__/fixtures/watch-rebuild-manifest/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/packages/vite/src/node/plugins/manifest.ts b/packages/vite/src/node/plugins/manifest.ts index ea58d16a21903f..52b99cf9bc0b81 100644 --- a/packages/vite/src/node/plugins/manifest.ts +++ b/packages/vite/src/node/plugins/manifest.ts @@ -32,6 +32,7 @@ export function manifestPlugin(): Plugin { manifest: {} as Manifest, outputCount: 0, reset() { + this.manifest = {} this.outputCount = 0 }, } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0d05316c53ed72..a256a473394a1a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -436,6 +436,8 @@ importers: packages/vite/src/node/__tests__/fixtures/test-dep-conditions: {} + packages/vite/src/node/__tests__/fixtures/watch-rebuild-manifest: {} + packages/vite/src/node/__tests__/packages: {} packages/vite/src/node/__tests__/packages/child: {}