MCPcopy
hub / github.com/vitejs/vite / watchPackageDataPlugin

Function watchPackageDataPlugin

packages/vite/src/node/packages.ts:254–289  ·  view source on GitHub ↗
(packageCache: PackageCache)

Source from the content-addressed store, hash-verified

252}
253
254export function watchPackageDataPlugin(packageCache: PackageCache): Plugin {
255 // a list of files to watch before the plugin is ready
256 const watchQueue = new Set<string>()
257 const watchedDirs = new Set<string>()
258
259 const watchFileStub = (id: string) => {
260 watchQueue.add(id)
261 }
262 let watchFile = watchFileStub
263
264 const setPackageData = packageCache.set.bind(packageCache)
265 packageCache.set = (id, pkg) => {
266 if (!isInNodeModules(pkg.dir) && !watchedDirs.has(pkg.dir)) {
267 watchedDirs.add(pkg.dir)
268 watchFile(path.join(pkg.dir, 'package.json'))
269 }
270 return setPackageData(id, pkg)
271 }
272
273 return {
274 name: 'vite:watch-package-data',
275 buildStart() {
276 watchFile = this.addWatchFile.bind(this)
277 watchQueue.forEach(watchFile)
278 watchQueue.clear()
279 },
280 buildEnd() {
281 watchFile = watchFileStub
282 },
283 watchChange(id) {
284 if (id.endsWith('/package.json')) {
285 invalidatePackageData(packageCache, path.normalize(id))
286 }
287 },
288 }
289}
290
291/**
292 * Get cached `resolvePackageData` value based on `basedir`. When one is found,

Callers 1

resolvePluginsFunction · 0.90

Calls 3

isInNodeModulesFunction · 0.90
hasMethod · 0.80
addMethod · 0.80

Tested by

no test coverage detected