Skip to content

Commit a78588f

Browse files
authoredApr 6, 2023
fix: update package cache watcher (#12772)
1 parent 2fdec3c commit a78588f

File tree

4 files changed

+22
-27
lines changed

4 files changed

+22
-27
lines changed
 

‎packages/vite/src/node/build.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import {
5151
initDepsOptimizer,
5252
} from './optimizer'
5353
import { loadFallbackPlugin } from './plugins/loadFallback'
54-
import { findNearestPackageData, watchPackageDataPlugin } from './packages'
54+
import { findNearestPackageData } from './packages'
5555
import type { PackageCache } from './packages'
5656
import { ensureWatchPlugin } from './plugins/ensureWatch'
5757
import { ESBUILD_MODULES_TARGET, VERSION } from './constants'
@@ -436,7 +436,6 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
436436
pre: [
437437
completeSystemWrapPlugin(),
438438
...(options.watch ? [ensureWatchPlugin()] : []),
439-
watchPackageDataPlugin(config),
440439
...(usePluginCommonjs ? [commonjsPlugin(options.commonjsOptions)] : []),
441440
dataURIPlugin(),
442441
...((

‎packages/vite/src/node/packages.ts

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
33
import { createRequire } from 'node:module'
4-
import { createFilter, safeRealpathSync } from './utils'
5-
import type { ResolvedConfig } from './config'
4+
import { createFilter, isInNodeModules, safeRealpathSync } from './utils'
65
import type { Plugin } from './plugin'
76

87
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
@@ -37,11 +36,10 @@ export interface PackageData {
3736
}
3837
}
3938

40-
export function invalidatePackageData(
39+
function invalidatePackageData(
4140
packageCache: PackageCache,
4241
pkgPath: string,
4342
): void {
44-
packageCache.delete(pkgPath)
4543
const pkgDir = path.dirname(pkgPath)
4644
packageCache.forEach((pkg, cacheKey) => {
4745
if (pkg.dir === pkgDir) {
@@ -217,34 +215,43 @@ export function loadPackageData(pkgPath: string): PackageData {
217215
return pkg
218216
}
219217

220-
export function watchPackageDataPlugin(config: ResolvedConfig): Plugin {
218+
export function watchPackageDataPlugin(packageCache: PackageCache): Plugin {
219+
// a list of files to watch before the plugin is ready
221220
const watchQueue = new Set<string>()
222-
let watchFile = (id: string) => {
221+
const watchedDirs = new Set<string>()
222+
223+
const watchFileStub = (id: string) => {
223224
watchQueue.add(id)
224225
}
226+
let watchFile = watchFileStub
225227

226-
const { packageCache } = config
227228
const setPackageData = packageCache.set.bind(packageCache)
228229
packageCache.set = (id, pkg) => {
229-
if (id.endsWith('.json')) {
230-
watchFile(id)
230+
if (!isInNodeModules(pkg.dir) && !watchedDirs.has(pkg.dir)) {
231+
watchedDirs.add(pkg.dir)
232+
watchFile(path.join(pkg.dir, 'package.json'))
231233
}
232234
return setPackageData(id, pkg)
233235
}
234236

235237
return {
236238
name: 'vite:watch-package-data',
237239
buildStart() {
238-
watchFile = this.addWatchFile
240+
watchFile = this.addWatchFile.bind(this)
239241
watchQueue.forEach(watchFile)
240242
watchQueue.clear()
241243
},
242244
buildEnd() {
243-
watchFile = (id) => watchQueue.add(id)
245+
watchFile = watchFileStub
244246
},
245247
watchChange(id) {
246248
if (id.endsWith('/package.json')) {
247-
invalidatePackageData(packageCache, id)
249+
invalidatePackageData(packageCache, path.normalize(id))
250+
}
251+
},
252+
handleHotUpdate({ file }) {
253+
if (file.endsWith('/package.json')) {
254+
invalidatePackageData(packageCache, path.normalize(file))
248255
}
249256
},
250257
}

‎packages/vite/src/node/plugins/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { isDepsOptimizerEnabled } from '../config'
44
import type { HookHandler, Plugin } from '../plugin'
55
import { getDepsOptimizer } from '../optimizer'
66
import { shouldExternalizeForSSR } from '../ssr/ssrExternal'
7+
import { watchPackageDataPlugin } from '../packages'
78
import { jsonPlugin } from './json'
89
import { resolvePlugin } from './resolve'
910
import { optimizedDepsBuildPlugin, optimizedDepsPlugin } from './optimizedDeps'
@@ -41,6 +42,7 @@ export async function resolvePlugins(
4142
return [
4243
isWatch ? ensureWatchPlugin() : null,
4344
isBuild ? metadataPlugin() : null,
45+
watchPackageDataPlugin(config.packageCache),
4446
preAliasPlugin(config),
4547
aliasPlugin({ entries: config.resolve.alias }),
4648
...prePlugins,

‎packages/vite/src/node/server/index.ts

-13
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import type { BindShortcutsOptions } from '../shortcuts'
4545
import { CLIENT_DIR, DEFAULT_DEV_PORT } from '../constants'
4646
import type { Logger } from '../logger'
4747
import { printServerUrls } from '../logger'
48-
import { invalidatePackageData } from '../packages'
4948
import { resolveChokidarOptions } from '../watch'
5049
import type { PluginContainer } from './pluginContainer'
5150
import { createPluginContainer } from './pluginContainer'
@@ -526,15 +525,6 @@ export async function _createServer(
526525
}
527526
}
528527

529-
const { packageCache } = config
530-
const setPackageData = packageCache.set.bind(packageCache)
531-
packageCache.set = (id, pkg) => {
532-
if (id.endsWith('.json')) {
533-
watcher.add(id)
534-
}
535-
return setPackageData(id, pkg)
536-
}
537-
538528
const onHMRUpdate = async (file: string, configOnly: boolean) => {
539529
if (serverConfig.hmr !== false) {
540530
try {
@@ -556,9 +546,6 @@ export async function _createServer(
556546

557547
watcher.on('change', async (file) => {
558548
file = normalizePath(file)
559-
if (file.endsWith('/package.json')) {
560-
return invalidatePackageData(packageCache, file)
561-
}
562549
// invalidate module graph cache on file change
563550
moduleGraph.onFileChange(file)
564551

0 commit comments

Comments
 (0)
Please sign in to comment.