Skip to content

Commit ee52267

Browse files
authored
fix(build): emptyOutDir should happen for watch rebuilds (#22207)
1 parent 938dda2 commit ee52267

File tree

7 files changed

+30
-3
lines changed

7 files changed

+30
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function prepareOutDirPlugin(): Plugin {
1111
const rendered = new Set<Environment>()
1212
return {
1313
name: 'vite:prepare-out-dir',
14-
options() {
14+
watchChange() {
1515
rendered.delete(this.environment)
1616
},
1717
renderStart: {

playground/assets/__tests__/assets.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,27 @@ describe.runIf(isBuild)('css and assets in css in build watch', () => {
705705
expect(cssFile).not.toMatch(/undefined/)
706706
})
707707

708+
test('old file is removed when the content changes', async () => {
709+
await expect.poll(() => page.textContent('.update-content')).toBe('hello')
710+
711+
const oldMainJsFiles = listAssets('foo').filter((f) =>
712+
/index-[-\w]+\.js$/.test(f),
713+
)
714+
expect(oldMainJsFiles.length).toBe(1)
715+
const oldMainJsFile = oldMainJsFiles[0]
716+
717+
editFile('asset/update.js', (code) => code.replace('hello', 'world'))
718+
await notifyRebuildComplete(watcher)
719+
await page.reload()
720+
await expect.poll(() => page.textContent('.update-content')).toBe('world')
721+
722+
const newMainJsFiles = listAssets('foo').filter((f) =>
723+
/index-[-\w]+\.js$/.test(f),
724+
)
725+
expect(newMainJsFiles).not.toContain(oldMainJsFile)
726+
expect(newMainJsFiles.length).toBe(1)
727+
})
728+
708729
test('import module.css', async () => {
709730
expect(await getColor('#foo')).toBe('red')
710731
editFile('css/foo.module.css', (code) => code.replace('red', 'blue'))

playground/assets/asset/update.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function text(el, text) {
2+
document.querySelector(el).textContent = text
3+
}
4+
text('.update-content', 'hello')

playground/assets/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,9 @@ <h3>assets in template</h3>
482482
<link rel="stylesheet" href="asset/style.css" />
483483
<div class="relative-css">link style</div>
484484
<div class="relative-js"></div>
485+
<div class="update-content"></div>
485486
<script src="asset/main.js" type="module"></script>
487+
<script src="asset/update.js" type="module"></script>
486488
<style>
487489
@import '/foo.css';
488490
</style>

playground/css/vite.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default defineConfig({
5555
},
5656
},
5757
},
58+
emptyOutDir: false, // the dist directory is shared with other configs
5859
},
5960
esbuild: {
6061
logOverride: {

playground/legacy/vite.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default defineConfig({
1717
manifest: true,
1818
sourcemap: true,
1919
assetsInlineLimit: 100, // keep SVG as assets URL
20+
emptyOutDir: false, // the dist directory is shared with other configs
2021
rollupOptions: {
2122
input: {
2223
index: path.resolve(import.meta.dirname, 'index.html'),

playground/vitestSetup.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,6 @@ async function loadConfig(configEnv: ConfigEnv) {
265265
// esbuild do not minify ES lib output since that would remove pure annotations and break tree-shaking
266266
// skip transpilation during tests to make it faster
267267
target: 'esnext',
268-
// tests are flaky when `emptyOutDir` is `true`
269-
emptyOutDir: false,
270268
},
271269
customLogger: createInMemoryLogger(serverLogs),
272270
plugins: [throwHtmlParseError()],

0 commit comments

Comments
 (0)