From 334ecfa2f18cc3d29b57f81a780f1c0767ccf6f2 Mon Sep 17 00:00:00 2001 From: sainthkh Date: Sat, 2 Feb 2019 09:14:58 +0900 Subject: [PATCH] Add new files to the main bundle tree. And remove them when they're removed (#667) --- packages/core/parcel-bundler/src/Bundler.js | 36 ++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/core/parcel-bundler/src/Bundler.js b/packages/core/parcel-bundler/src/Bundler.js index 98238faa5a2..df254ec46f4 100644 --- a/packages/core/parcel-bundler/src/Bundler.js +++ b/packages/core/parcel-bundler/src/Bundler.js @@ -19,7 +19,7 @@ const installPackage = require('./utils/installPackage'); const bundleReport = require('./utils/bundleReport'); const prettifyTime = require('./utils/prettifyTime'); const getRootDir = require('./utils/getRootDir'); -const {glob} = require('./utils/glob'); +const {glob, isGlob} = require('./utils/glob'); /** * The Bundler is the main entry point. It resolves and loads assets, @@ -59,6 +59,7 @@ class Bundler extends EventEmitter { this.pending = false; this.loadedAssets = new Map(); this.watchedAssets = new Map(); + this.watchedGlobs = entryFiles.filter(entry => isGlob(entry)); this.farm = null; this.watcher = null; @@ -372,7 +373,12 @@ class Bundler extends EventEmitter { if (process.env.NODE_ENV === 'test' && !this.watcher.ready) { await new Promise(resolve => this.watcher.once('ready', resolve)); } + this.watchedGlobs.forEach(glob => { + this.watcher.add(glob); + }); + this.watcher.on('add', this.onAdd.bind(this)); this.watcher.on('change', this.onChange.bind(this)); + this.watcher.on('unlink', this.onUnlink.bind(this)); } if (this.options.hmr) { @@ -758,7 +764,24 @@ class Bundler extends EventEmitter { } } + async onAdd(path) { + path = Path.join(process.cwd(), path); + + let asset = this.parser.getAsset(path, this.options); + this.loadedAssets.set(path, asset); + + this.entryAssets.add(asset); + + await this.watch(path, asset); + this.onChange(path); + } + async onChange(path) { + // The path to the newly-added items are not absolute. + if (!Path.isAbsolute(path)) { + path = Path.resolve(process.cwd(), path); + } + let assets = this.watchedAssets.get(path); if (!assets || !assets.size) { return; @@ -779,6 +802,17 @@ class Bundler extends EventEmitter { }, 100); } + async onUnlink(path) { + // The path to the newly-added items are not absolute. + if (!Path.isAbsolute(path)) { + path = Path.resolve(process.cwd(), path); + } + + let asset = this.getLoadedAsset(path); + this.entryAssets.delete(asset); + this.unloadAsset(asset); + } + middleware() { this.bundle(); return Server.middleware(this);