Skip to content

Commit

Permalink
Add new files to the main bundle tree. And remove them when they're r…
Browse files Browse the repository at this point in the history
…emoved (parcel-bundler#667)
  • Loading branch information
sainthkh committed Feb 2, 2019
1 parent 791b4b8 commit 334ecfa
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion packages/core/parcel-bundler/src/Bundler.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 334ecfa

Please sign in to comment.