Skip to content

Commit

Permalink
fix: update package cache watcher (#12772)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Apr 6, 2023
1 parent 2fdec3c commit a78588f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
3 changes: 1 addition & 2 deletions packages/vite/src/node/build.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -436,7 +436,6 @@ export async function resolveBuildPlugins(config: ResolvedConfig): Promise<{
pre: [
completeSystemWrapPlugin(),
...(options.watch ? [ensureWatchPlugin()] : []),
watchPackageDataPlugin(config),
...(usePluginCommonjs ? [commonjsPlugin(options.commonjsOptions)] : []),
dataURIPlugin(),
...((
Expand Down
31 changes: 19 additions & 12 deletions 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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -217,34 +215,43 @@ 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<string>()
let watchFile = (id: string) => {
const watchedDirs = new Set<string>()

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)
}

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))
}
},
}
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/node/plugins/index.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 0 additions & 13 deletions packages/vite/src/node/server/index.ts
Expand Up @@ -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'
Expand Down Expand Up @@ -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 {
Expand All @@ -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)

Expand Down

0 comments on commit a78588f

Please sign in to comment.