From 7d418d8e3e98fa38c15260d9dd13b2e6315c200b Mon Sep 17 00:00:00 2001 From: David Aurelio Date: Mon, 15 Aug 2016 08:30:59 -0700 Subject: [PATCH] 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 --- react-packager/src/Bundler/index.js | 4 ++++ react-packager/src/Resolver/index.js | 6 +++++- .../src/Server/__tests__/Server-test.js | 6 ++++++ react-packager/src/Server/index.js | 16 ++++++++++++++++ react-packager/src/node-haste/index.js | 4 ++++ 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/react-packager/src/Bundler/index.js b/react-packager/src/Bundler/index.js index 27c42430..135f8529 100644 --- a/react-packager/src/Bundler/index.js +++ b/react-packager/src/Bundler/index.js @@ -693,6 +693,10 @@ class Bundler { return Promise.resolve(extraOptions) .then(extraOptions => Object.assign(options, extraOptions)); } + + getResolver() { + return this._resolver; + } } function getPathRelativeToRoot(roots, absPath) { diff --git a/react-packager/src/Resolver/index.js b/react-packager/src/Resolver/index.js index 799dece2..673d9cc9 100644 --- a/react-packager/src/Resolver/index.js +++ b/react-packager/src/Resolver/index.js @@ -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(''); diff --git a/react-packager/src/Server/__tests__/Server-test.js b/react-packager/src/Server/__tests__/Server-test.js index 640da7eb..8f53c12e 100644 --- a/react-packager/src/Server/__tests__/Server-test.js +++ b/react-packager/src/Server/__tests__/Server-test.js @@ -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); diff --git a/react-packager/src/Server/index.js b/react-packager/src/Server/index.js index 72f35165..d9805792 100644 --- a/react-packager/src/Server/index.js +++ b/react-packager/src/Server/index.js @@ -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( diff --git a/react-packager/src/node-haste/index.js b/react-packager/src/node-haste/index.js index 5ddf93ee..79cf6b9d 100644 --- a/react-packager/src/node-haste/index.js +++ b/react-packager/src/node-haste/index.js @@ -304,6 +304,10 @@ class DependencyGraph { createPolyfill(options) { return this._moduleCache.createPolyfill(options); } + + getHasteMap() { + return this._hasteMap; + } } Object.assign(DependencyGraph, {