Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion packages/vite/src/node/plugins/prepareOutDir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,44 @@ import colors from 'picocolors'
import type { Plugin } from '../plugin'
import { getResolvedOutDirs, resolveEmptyOutDir } from '../watch'
import type { Environment } from '../environment'
import { copyDir, emptyDir, normalizePath } from '../utils'
import {
ERR_SYMLINK_IN_RECURSIVE_READDIR,
copyDir,
emptyDir,
normalizePath,
recursiveReaddir,
} from '../utils'
import { withTrailingSlash } from '../../shared/utils'

export function prepareOutDirPlugin(): Plugin {
const rendered = new Set<Environment>()
return {
name: 'vite:prepare-out-dir',
async buildStart() {
const { build: options, publicDir } = this.environment.config
if (
!options.watch ||
!options.write ||
!options.copyPublicDir ||
!publicDir
) {
return
}
// Public files bypass the bundler, so nothing else adds them to the
// watch set. A rebuild re-copies them in `renderStart` below.
let publicFiles: string[]
try {
publicFiles = await recursiveReaddir(publicDir)
} catch (e) {
if (e.code === ERR_SYMLINK_IN_RECURSIVE_READDIR) {
return
}
throw e
}
for (const file of publicFiles) {
this.addWatchFile(file)
}
},
watchChange() {
rendered.delete(this.environment)
},
Expand Down
18 changes: 18 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { existsSync } from 'node:fs'
import path from 'node:path'
import { describe, expect, test } from 'vitest'
import {
Expand All @@ -15,7 +16,9 @@ import {
page,
readFile,
readManifest,
removeFile,
serverLogs,
testDir,
viteTestUrl,
watcher,
} from '~utils'
Expand Down Expand Up @@ -811,6 +814,21 @@ describe.runIf(isBuild)('css and assets in css in build watch', () => {
await page.reload()
expect(await page.textContent('.raw-query')).toBe('zoo2')
})

// `static/bar` is not imported anywhere, so it only reaches outDir if
// publicDir itself is watched
test('editing a file in publicDir updates outDir', async () => {
expect(readFile('dist/foo/bar')).toBe('bar\n')
editFile('static/bar', (code) => code.replace('bar', 'bar2'))
await notifyRebuildComplete(watcher)
expect(readFile('dist/foo/bar')).toBe('bar2\n')
})

test('removing a file from publicDir updates outDir', async () => {
removeFile('static/bar')
await notifyRebuildComplete(watcher)
expect(existsSync(path.resolve(testDir, 'dist/foo/bar'))).toBe(false)
})
})

test('inline style test', async () => {
Expand Down
Loading