Fix HMR when adding/renaming assets

Summary:
The HMR logic used to try to calculate the dependencies of every new added (or modified) file, including assets. This resulted in a TransformError.

This commit adds a check that stops the HMR bundling once it finds out that the updated file is an asset

Reviewed By: cpojer

Differential Revision: D5697391

fbshipit-source-id: faf7ccad76ac4922b70ed1c7ce8ce32b03c4e8ee
This commit is contained in:
Rafael Oleza 2017-08-24 07:26:48 -07:00 committed by Facebook Github Bot
parent b2331471d5
commit 679309987b
3 changed files with 48 additions and 4 deletions

View File

@ -224,10 +224,11 @@ class DependencyGraph extends EventEmitter {
this._haste.end();
}
/**
* Returns the module object for the given path.
*/
getModuleForPath(entryFile: string) {
if (this._moduleResolver.isAssetFile(entryFile)) {
return this._moduleCache.getAssetModule(entryFile);
}
return this._moduleCache.getModule(entryFile);
}

View File

@ -364,12 +364,16 @@ class ModuleResolver<TModule: Moduleish, TPackage: Packageish> {
return failedFor({file: fileResult.candidates, dir: dirResult.candidates});
}
isAssetFile(filename: string): boolean {
return this._options.helpers.isAssetFile(filename);
}
_loadAsFile(
dirPath: string,
fileNameHint: string,
platform: string | null,
): Resolution<TModule, FileCandidates> {
if (this._options.helpers.isAssetFile(fileNameHint)) {
if (this.isAssetFile(fileNameHint)) {
return this._loadAsAssetFile(dirPath, fileNameHint, platform);
}
const {doesFileExist} = this._options;

View File

@ -6,6 +6,7 @@
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails oncall+javascript_tools
* @format
*/
@ -5661,6 +5662,44 @@ describe('DependencyGraph', function() {
});
});
describe('getModuleForPath()', () => {
let DependencyGraph;
let dependencyGraph;
beforeEach(async () => {
setMockFileSystem({
root: {
'index.js': ``,
imgs: {
'a.png': '',
},
},
});
DependencyGraph = require('../DependencyGraph');
dependencyGraph = await DependencyGraph.load({
...defaults,
roots: ['/root'],
});
});
afterEach(() => {
dependencyGraph.end();
});
it('returns correctly a JS module', async () => {
const module = dependencyGraph.getModuleForPath('/root/index.js');
expect(await module.getName()).toBe('/root/index.js');
expect(module.isAsset()).toBe(false);
});
it('returns correctly an asset module', async () => {
const module = dependencyGraph.getModuleForPath('/root/imgs/a.png');
expect(await module.getName()).toBe('/root/imgs/a.png');
expect(module.isAsset()).toBe(true);
});
});
/**
* When running a test on the dependency graph, watch mode is enabled by
* default, so we must end the watcher to ensure the test does not hang up