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 a6059b7ca8
commit 7d418d8e3e
5 changed files with 35 additions and 1 deletions

View File

@ -693,6 +693,10 @@ class Bundler {
return Promise.resolve(extraOptions)
.then(extraOptions => Object.assign(options, extraOptions));
}
getResolver() {
return this._resolver;
}
}
function getPathRelativeToRoot(roots, absPath) {

View File

@ -264,6 +264,10 @@ class Resolver {
minifyModule({path, code, map}) {
return this._minifyCode(path, code, map);
}
getDependecyGraph() {
return this._depGraph;
}
}
function defineModuleCode(moduleName, code, verboseName = '', dev = true) {
@ -280,7 +284,7 @@ function defineModuleCode(moduleName, code, verboseName = '', dev = true) {
function definePolyfillCode(code,) {
return [
`(function(global) {`,
'(function(global) {',
code,
`\n})(typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : this);`,
].join('');

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(

View File

@ -304,6 +304,10 @@ class DependencyGraph {
createPolyfill(options) {
return this._moduleCache.createPolyfill(options);
}
getHasteMap() {
return this._hasteMap;
}
}
Object.assign(DependencyGraph, {