Skip to content

Commit

Permalink
fix(server): watch env files creating and deleting (fix #12127) (#12129)
Browse files Browse the repository at this point in the history
  • Loading branch information
sun0day authored Feb 23, 2023
1 parent 2556f88 commit cc3724f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
5 changes: 5 additions & 0 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function getShortName(file: string, root: string): string {
export async function handleHMRUpdate(
file: string,
server: ViteDevServer,
configOnly: boolean,
): Promise<void> {
const { ws, config, moduleGraph } = server
const shortFile = getShortName(file, config.root)
Expand Down Expand Up @@ -71,6 +72,10 @@ export async function handleHMRUpdate(
return
}

if (configOnly) {
return
}

debugHmr(`[file change] ${colors.dim(shortFile)}`)

// (dev only) the client itself cannot be hot updated.
Expand Down
35 changes: 21 additions & 14 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,32 +484,39 @@ export async function createServer(
return setPackageData(id, pkg)
}

watcher.on('change', async (file) => {
file = normalizePath(file)
if (file.endsWith('/package.json')) {
return invalidatePackageData(packageCache, file)
}
// invalidate module graph cache on file change
moduleGraph.onFileChange(file)
const onHMRUpdate = async (file: string, configOnly: boolean) => {
if (serverConfig.hmr !== false) {
try {
await handleHMRUpdate(file, server)
await handleHMRUpdate(file, server, configOnly)
} catch (err) {
ws.send({
type: 'error',
err: prepareError(err),
})
}
}
})
}

watcher.on('add', (file) => {
handleFileAddUnlink(normalizePath(file), server)
})
watcher.on('unlink', (file) => {
handleFileAddUnlink(normalizePath(file), server)
const onFileAddUnlink = async (file: string) => {
file = normalizePath(file)
await handleFileAddUnlink(file, server)
await onHMRUpdate(file, true)
}

watcher.on('change', async (file) => {
file = normalizePath(file)
if (file.endsWith('/package.json')) {
return invalidatePackageData(packageCache, file)
}
// invalidate module graph cache on file change
moduleGraph.onFileChange(file)

await onHMRUpdate(file, false)
})

watcher.on('add', onFileAddUnlink)
watcher.on('unlink', onFileAddUnlink)

ws.on('vite:invalidate', async ({ path, message }: InvalidatePayload) => {
const mod = moduleGraph.urlToModuleMap.get(path)
if (mod && mod.isSelfAccepting && mod.lastHMRTimestamp > 0) {
Expand Down

0 comments on commit cc3724f

Please sign in to comment.