mirror of https://github.com/status-im/metro.git
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:
parent
a6059b7ca8
commit
7d418d8e3e
|
@ -693,6 +693,10 @@ class Bundler {
|
||||||
return Promise.resolve(extraOptions)
|
return Promise.resolve(extraOptions)
|
||||||
.then(extraOptions => Object.assign(options, extraOptions));
|
.then(extraOptions => Object.assign(options, extraOptions));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getResolver() {
|
||||||
|
return this._resolver;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPathRelativeToRoot(roots, absPath) {
|
function getPathRelativeToRoot(roots, absPath) {
|
||||||
|
|
|
@ -264,6 +264,10 @@ class Resolver {
|
||||||
minifyModule({path, code, map}) {
|
minifyModule({path, code, map}) {
|
||||||
return this._minifyCode(path, code, map);
|
return this._minifyCode(path, code, map);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getDependecyGraph() {
|
||||||
|
return this._depGraph;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function defineModuleCode(moduleName, code, verboseName = '', dev = true) {
|
function defineModuleCode(moduleName, code, verboseName = '', dev = true) {
|
||||||
|
@ -280,7 +284,7 @@ function defineModuleCode(moduleName, code, verboseName = '', dev = true) {
|
||||||
|
|
||||||
function definePolyfillCode(code,) {
|
function definePolyfillCode(code,) {
|
||||||
return [
|
return [
|
||||||
`(function(global) {`,
|
'(function(global) {',
|
||||||
code,
|
code,
|
||||||
`\n})(typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : this);`,
|
`\n})(typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : this);`,
|
||||||
].join('');
|
].join('');
|
||||||
|
|
|
@ -82,6 +82,12 @@ describe('processRequest', () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
Bundler.prototype.invalidateFile = invalidatorFunc;
|
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);
|
server = new Server(options);
|
||||||
requestHandler = server.processRequest.bind(server);
|
requestHandler = server.processRequest.bind(server);
|
||||||
|
|
|
@ -180,6 +180,7 @@ const dependencyOpts = declareOpts({
|
||||||
});
|
});
|
||||||
|
|
||||||
const bundleDeps = new WeakMap();
|
const bundleDeps = new WeakMap();
|
||||||
|
const NODE_MODULES = `${path.sep}node_modules${path.sep}`;
|
||||||
|
|
||||||
class Server {
|
class Server {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
|
@ -230,6 +231,16 @@ class Server {
|
||||||
|
|
||||||
this._fileWatcher.on('all', this._onFileChange.bind(this));
|
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 => {
|
this._debouncedFileChangeHandler = debounceAndBatch(filePaths => {
|
||||||
// only clear bundles for non-JS changes
|
// only clear bundles for non-JS changes
|
||||||
if (filePaths.every(RegExp.prototype.test, /\.js(?:on)?$/i)) {
|
if (filePaths.every(RegExp.prototype.test, /\.js(?:on)?$/i)) {
|
||||||
|
@ -244,6 +255,7 @@ class Server {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
debug('Clearing bundles due to non-JS change');
|
||||||
this._clearBundles();
|
this._clearBundles();
|
||||||
}
|
}
|
||||||
this._informChangeWatchers();
|
this._informChangeWatchers();
|
||||||
|
@ -362,6 +374,10 @@ class Server {
|
||||||
this._clearBundles();
|
this._clearBundles();
|
||||||
this._hmrFileChangeListener(absPath, this._bundler.stat(absPath));
|
this._hmrFileChangeListener(absPath, this._bundler.stat(absPath));
|
||||||
return;
|
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(
|
Promise.all(
|
||||||
|
|
|
@ -304,6 +304,10 @@ class DependencyGraph {
|
||||||
createPolyfill(options) {
|
createPolyfill(options) {
|
||||||
return this._moduleCache.createPolyfill(options);
|
return this._moduleCache.createPolyfill(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getHasteMap() {
|
||||||
|
return this._hasteMap;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.assign(DependencyGraph, {
|
Object.assign(DependencyGraph, {
|
||||||
|
|
Loading…
Reference in New Issue