From a78588f5dd2c7f712262696ce5765ae8a4ace70d Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Fri, 7 Apr 2023 03:21:33 +0800 Subject: [PATCH] fix: update package cache watcher (#12772) --- packages/vite/src/node/build.ts | 3 +-- packages/vite/src/node/packages.ts | 31 +++++++++++++++---------- packages/vite/src/node/plugins/index.ts | 2 ++ packages/vite/src/node/server/index.ts | 13 ----------- 4 files changed, 22 insertions(+), 27 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 3cfea1943da8c8..62201970672e21 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -51,7 +51,7 @@ import { initDepsOptimizer, } from './optimizer' import { loadFallbackPlugin } from './plugins/loadFallback' -import { findNearestPackageData, watchPackageDataPlugin } from './packages' +import { findNearestPackageData } from './packages' import type { PackageCache } from './packages' import { ensureWatchPlugin } from './plugins/ensureWatch' import { ESBUILD_MODULES_TARGET, VERSION } from './constants' @@ -436,7 +436,6 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{ pre: [ completeSystemWrapPlugin(), ...(options.watch ? [ensureWatchPlugin()] : []), - watchPackageDataPlugin(config), ...(usePluginCommonjs ? [commonjsPlugin(options.commonjsOptions)] : []), dataURIPlugin(), ...(( diff --git a/packages/vite/src/node/packages.ts b/packages/vite/src/node/packages.ts index 75c73c585d2c80..b72032d2ec1aa2 100644 --- a/packages/vite/src/node/packages.ts +++ b/packages/vite/src/node/packages.ts @@ -1,8 +1,7 @@ import fs from 'node:fs' import path from 'node:path' import { createRequire } from 'node:module' -import { createFilter, safeRealpathSync } from './utils' -import type { ResolvedConfig } from './config' +import { createFilter, isInNodeModules, safeRealpathSync } from './utils' import type { Plugin } from './plugin' // eslint-disable-next-line @typescript-eslint/consistent-type-imports @@ -37,11 +36,10 @@ export interface PackageData { } } -export function invalidatePackageData( +function invalidatePackageData( packageCache: PackageCache, pkgPath: string, ): void { - packageCache.delete(pkgPath) const pkgDir = path.dirname(pkgPath) packageCache.forEach((pkg, cacheKey) => { if (pkg.dir === pkgDir) { @@ -217,17 +215,21 @@ export function loadPackageData(pkgPath: string): PackageData { return pkg } -export function watchPackageDataPlugin(config: ResolvedConfig): Plugin { +export function watchPackageDataPlugin(packageCache: PackageCache): Plugin { + // a list of files to watch before the plugin is ready const watchQueue = new Set() - let watchFile = (id: string) => { + const watchedDirs = new Set() + + const watchFileStub = (id: string) => { watchQueue.add(id) } + let watchFile = watchFileStub - const { packageCache } = config const setPackageData = packageCache.set.bind(packageCache) packageCache.set = (id, pkg) => { - if (id.endsWith('.json')) { - watchFile(id) + if (!isInNodeModules(pkg.dir) && !watchedDirs.has(pkg.dir)) { + watchedDirs.add(pkg.dir) + watchFile(path.join(pkg.dir, 'package.json')) } return setPackageData(id, pkg) } @@ -235,16 +237,21 @@ export function watchPackageDataPlugin(config: ResolvedConfig): Plugin { return { name: 'vite:watch-package-data', buildStart() { - watchFile = this.addWatchFile + watchFile = this.addWatchFile.bind(this) watchQueue.forEach(watchFile) watchQueue.clear() }, buildEnd() { - watchFile = (id) => watchQueue.add(id) + watchFile = watchFileStub }, watchChange(id) { if (id.endsWith('/package.json')) { - invalidatePackageData(packageCache, id) + invalidatePackageData(packageCache, path.normalize(id)) + } + }, + handleHotUpdate({ file }) { + if (file.endsWith('/package.json')) { + invalidatePackageData(packageCache, path.normalize(file)) } }, } diff --git a/packages/vite/src/node/plugins/index.ts b/packages/vite/src/node/plugins/index.ts index 928d2db7f3225b..1205befbc69b4b 100644 --- a/packages/vite/src/node/plugins/index.ts +++ b/packages/vite/src/node/plugins/index.ts @@ -4,6 +4,7 @@ import { isDepsOptimizerEnabled } from '../config' import type { HookHandler, Plugin } from '../plugin' import { getDepsOptimizer } from '../optimizer' import { shouldExternalizeForSSR } from '../ssr/ssrExternal' +import { watchPackageDataPlugin } from '../packages' import { jsonPlugin } from './json' import { resolvePlugin } from './resolve' import { optimizedDepsBuildPlugin, optimizedDepsPlugin } from './optimizedDeps' @@ -41,6 +42,7 @@ export async function resolvePlugins( return [ isWatch ? ensureWatchPlugin() : null, isBuild ? metadataPlugin() : null, + watchPackageDataPlugin(config.packageCache), preAliasPlugin(config), aliasPlugin({ entries: config.resolve.alias }), ...prePlugins, diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index e43e81cdde17a0..d9c4fcec8a7a2d 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -45,7 +45,6 @@ import type { BindShortcutsOptions } from '../shortcuts' import { CLIENT_DIR, DEFAULT_DEV_PORT } from '../constants' import type { Logger } from '../logger' import { printServerUrls } from '../logger' -import { invalidatePackageData } from '../packages' import { resolveChokidarOptions } from '../watch' import type { PluginContainer } from './pluginContainer' import { createPluginContainer } from './pluginContainer' @@ -526,15 +525,6 @@ export async function _createServer( } } - const { packageCache } = config - const setPackageData = packageCache.set.bind(packageCache) - packageCache.set = (id, pkg) => { - if (id.endsWith('.json')) { - watcher.add(id) - } - return setPackageData(id, pkg) - } - const onHMRUpdate = async (file: string, configOnly: boolean) => { if (serverConfig.hmr !== false) { try { @@ -556,9 +546,6 @@ export async function _createServer( 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)