Skip to content

Commit

Permalink
Clear bundles for potential dependency resolution changes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
davidaurelio authored and Facebook Github Bot 5 committed Aug 15, 2016
1 parent 0fb2ccf commit 8240339
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packager/react-packager/src/Bundler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,10 @@ class Bundler {
return Promise.resolve(extraOptions)
.then(extraOptions => Object.assign(options, extraOptions));
}

getResolver() {
return this._resolver;
}
}

function getPathRelativeToRoot(roots, absPath) {
Expand Down
6 changes: 5 additions & 1 deletion packager/react-packager/src/Resolver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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('');
Expand Down
6 changes: 6 additions & 0 deletions packager/react-packager/src/Server/__tests__/Server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions packager/react-packager/src/Server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ const dependencyOpts = declareOpts({
});

const bundleDeps = new WeakMap();
const NODE_MODULES = `${path.sep}node_modules${path.sep}`;

class Server {
constructor(options) {
Expand Down Expand Up @@ -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)) {
Expand All @@ -244,6 +255,7 @@ class Server {
});
}
} else {
debug('Clearing bundles due to non-JS change');
this._clearBundles();
}
this._informChangeWatchers();
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions packager/react-packager/src/node-haste/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ class DependencyGraph {
createPolyfill(options) {
return this._moduleCache.createPolyfill(options);
}

getHasteMap() {
return this._hasteMap;
}
}

Object.assign(DependencyGraph, {
Expand Down

0 comments on commit 8240339

Please sign in to comment.