(packageCache: PackageCache)
| 252 | } |
| 253 | |
| 254 | export 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, |
no test coverage detected