Clear bundles for potential dependency resolution changes

Summary:
This clears the packager server cache for potential changes of module resolutions.
We will make this a lot better in the future, but for now this crude mechanism will have to do.

Reviewed By: cpojer

Differential Revision: D3713143

fbshipit-source-id: 7c81f40e8ec71404c3369211b29f63928d6634b9
This commit is contained in:
David Aurelio
2016-08-15 08:30:59 -07:00
committed by Facebook Github Bot 5
parent 0fb2ccfcc3
commit 8240339dca
5 changed files with 35 additions and 1 deletions

View File

@@ -82,6 +82,12 @@ describe('processRequest', () => {
};
Bundler.prototype.invalidateFile = invalidatorFunc;
Bundler.prototype.getResolver =
jest.fn().mockReturnValue({
getDependecyGraph: jest.fn().mockReturnValue({
getHasteMap: jest.fn().mockReturnValue({on: jest.fn()}),
}),
});
server = new Server(options);
requestHandler = server.processRequest.bind(server);

View File

@@ -180,6 +180,7 @@ const dependencyOpts = declareOpts({
});
const bundleDeps = new WeakMap();
const NODE_MODULES = `${path.sep}node_modules${path.sep}`;
class Server {
constructor(options) {
@@ -230,6 +231,16 @@ class Server {
this._fileWatcher.on('all', this._onFileChange.bind(this));
// changes to the haste map can affect resolution of files in the bundle
this._bundler
.getResolver()
.getDependecyGraph()
.getHasteMap()
.on('change', () => {
debug('Clearing bundle cache due to haste map change');
this._clearBundles();
});
this._debouncedFileChangeHandler = debounceAndBatch(filePaths => {
// only clear bundles for non-JS changes
if (filePaths.every(RegExp.prototype.test, /\.js(?:on)?$/i)) {
@@ -244,6 +255,7 @@ class Server {
});
}
} else {
debug('Clearing bundles due to non-JS change');
this._clearBundles();
}
this._informChangeWatchers();
@@ -362,6 +374,10 @@ class Server {
this._clearBundles();
this._hmrFileChangeListener(absPath, this._bundler.stat(absPath));
return;
} else if (type !== 'change' && absPath.indexOf(NODE_MODULES) !== -1) {
// node module resolution can be affected by added or removed files
debug('Clearing bundles due to potential node_modules resolution change');
this._clearBundles();
}
Promise.all(