Use getModuleForPath() method from Resolver instead of Bundler

Reviewed By: davidaurelio

Differential Revision: D5813534

fbshipit-source-id: dbd005c2f9b478be58f33bd30df16e94159509da
This commit is contained in:
Rafael Oleza 2017-09-12 07:04:28 -07:00 committed by Facebook Github Bot
parent d9ff4fa1dc
commit bd51bc3c2f
3 changed files with 33 additions and 53 deletions

View File

@ -60,15 +60,6 @@ class DeltaCalculator extends EventEmitter {
.on('change', this._handleMultipleFileChanges);
}
static async create(
bundler: Bundler,
options: BundleOptions,
): Promise<DeltaCalculator> {
const resolver = await bundler.getResolver();
return new DeltaCalculator(bundler, resolver, options);
}
/**
* Stops listening for file changes and clears all the caches.
*/
@ -204,12 +195,10 @@ class DeltaCalculator extends EventEmitter {
// Build the modules from the files that have been modified.
const modified = new Map(
await Promise.all(
modifiedArray.map(async file => {
const module = await this._bundler.getModuleForPath(file);
return [file, module];
}),
),
modifiedArray.map(file => {
const module = this._resolver.getModuleForPath(file);
return [file, module];
}),
);
const filesWithChangedDependencies = await Promise.all(
@ -238,7 +227,7 @@ class DeltaCalculator extends EventEmitter {
}
async _hasChangedDependencies(file: string) {
const module = await this._bundler.getModuleForPath(file);
const module = this._resolver.getModuleForPath(file);
if (!this._dependencies.has(module.path)) {
return false;

View File

@ -66,6 +66,7 @@ type Options = {|
*/
class DeltaTransformer extends EventEmitter {
_bundler: Bundler;
_resolver: Resolver;
_getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>;
_polyfillModuleNames: $ReadOnlyArray<string>;
_getModuleId: ({path: string}) => number;
@ -75,6 +76,7 @@ class DeltaTransformer extends EventEmitter {
constructor(
bundler: Bundler,
resolver: Resolver,
deltaCalculator: DeltaCalculator,
options: Options,
bundleOptions: BundleOptions,
@ -82,6 +84,7 @@ class DeltaTransformer extends EventEmitter {
super();
this._bundler = bundler;
this._resolver = resolver;
this._deltaCalculator = deltaCalculator;
this._getPolyfills = options.getPolyfills;
this._polyfillModuleNames = options.polyfillModuleNames;
@ -96,13 +99,17 @@ class DeltaTransformer extends EventEmitter {
options: Options,
bundleOptions: BundleOptions,
): Promise<DeltaTransformer> {
const deltaCalculator = await DeltaCalculator.create(
const resolver = await bundler.getResolver();
const deltaCalculator = new DeltaCalculator(
bundler,
resolver,
bundleOptions,
);
return new DeltaTransformer(
bundler,
resolver,
deltaCalculator,
options,
bundleOptions,
@ -151,12 +158,10 @@ class DeltaTransformer extends EventEmitter {
const transformerOptions = this._deltaCalculator.getTransformerOptions();
const dependencyPairs = this._deltaCalculator.getDependencyPairs();
const resolver = await this._bundler.getResolver();
// Get the transformed source code of each modified/added module.
const modifiedDelta = await this._transformModules(
Array.from(modified.values()),
resolver,
transformerOptions,
dependencyPairs,
);
@ -198,8 +203,6 @@ class DeltaTransformer extends EventEmitter {
transformOptions: JSTransformerOptions,
dependencyPairs: Map<string, $ReadOnlyArray<[string, Module]>>,
): Promise<DeltaEntries> {
const resolver = await this._bundler.getResolver();
// Get all the polyfills from the relevant option params (the
// `getPolyfills()` method and the `polyfillModuleNames` variable).
const polyfillModuleNames = this._getPolyfills({
@ -208,13 +211,13 @@ class DeltaTransformer extends EventEmitter {
// The module system dependencies are scripts that need to be included at
// the very beginning of the bundle (before any polyfill).
const moduleSystemDeps = resolver.getModuleSystemDependencies({
const moduleSystemDeps = this._resolver.getModuleSystemDependencies({
dev: this._bundleOptions.dev,
});
const modules = moduleSystemDeps.concat(
polyfillModuleNames.map((polyfillModuleName, idx) =>
resolver.getDependencyGraph().createPolyfill({
this._resolver.getDependencyGraph().createPolyfill({
file: polyfillModuleName,
id: polyfillModuleName,
dependencies: [],
@ -224,7 +227,6 @@ class DeltaTransformer extends EventEmitter {
return await this._transformModules(
modules,
resolver,
transformOptions,
dependencyPairs,
);
@ -234,15 +236,13 @@ class DeltaTransformer extends EventEmitter {
dependencyPairs: Map<string, $ReadOnlyArray<[string, Module]>>,
modulesByName: Map<string, Module>,
): Promise<DeltaEntries> {
const resolver = await this._bundler.getResolver();
// Get the absolute path of the entry file, in order to be able to get the
// actual correspondant module (and its moduleId) to be able to add the
// correct require(); call at the very end of the bundle.
const absPath = resolver
const absPath = this._resolver
.getDependencyGraph()
.getAbsolutePath(this._bundleOptions.entryFile);
const entryPointModule = await this._bundler.getModuleForPath(absPath);
const entryPointModule = this._resolver.getModuleForPath(absPath);
// First, get the modules correspondant to all the module names defined in
// the `runBeforeMainModule` config variable. Then, append the entry point
@ -291,19 +291,13 @@ class DeltaTransformer extends EventEmitter {
async _transformModules(
modules: Array<Module>,
resolver: Resolver,
transformOptions: JSTransformerOptions,
dependencyPairs: Map<string, $ReadOnlyArray<[string, Module]>>,
): Promise<DeltaEntries> {
return new Map(
await Promise.all(
modules.map(module =>
this._transformModule(
module,
resolver,
transformOptions,
dependencyPairs,
),
this._transformModule(module, transformOptions, dependencyPairs),
),
),
);
@ -311,7 +305,6 @@ class DeltaTransformer extends EventEmitter {
async _transformModule(
module: Module,
resolver: Resolver,
transformOptions: JSTransformerOptions,
dependencyPairs: Map<string, $ReadOnlyArray<[string, Module]>>,
): Promise<[number, ?DeltaEntry]> {
@ -323,7 +316,7 @@ class DeltaTransformer extends EventEmitter {
const dependencyPairsForModule = dependencyPairs.get(module.path) || [];
const wrapped = this._bundleOptions.wrapModules
? await resolver.wrapModule({
? await this._resolver.wrapModule({
module,
getModuleId: this._getModuleId,
dependencyPairs: dependencyPairsForModule,
@ -335,7 +328,7 @@ class DeltaTransformer extends EventEmitter {
dev: this._bundleOptions.dev,
})
: {
code: resolver.resolveRequires(
code: this._resolver.resolveRequires(
module,
this._getModuleId,
metadata.code,

View File

@ -13,9 +13,11 @@
'use strict';
jest.mock('../../Bundler');
jest.mock('../../Resolver');
const Bundler = require('../../Bundler');
const {EventEmitter} = require('events');
const Resolver = require('../../Resolver');
const DeltaCalculator = require('../DeltaCalculator');
@ -71,16 +73,14 @@ describe('DeltaCalculator', () => {
fileWatcher = new EventEmitter();
Bundler.prototype.getResolver.mockReturnValue(
Promise.resolve({
getDependencyGraph() {
return {
getWatcher() {
return fileWatcher;
},
};
},
}),
Resolver.prototype.getDependencyGraph.mockReturnValue({
getWatcher() {
return fileWatcher;
},
});
Resolver.prototype.getModuleForPath.mockImplementation(
path => mockedDependencies.filter(dep => dep.path === path)[0],
);
Bundler.prototype.getDependencies.mockImplementation(async () => {
@ -94,10 +94,6 @@ describe('DeltaCalculator', () => {
};
});
Bundler.prototype.getModuleForPath.mockImplementation(async path => {
return mockedDependencies.filter(dep => dep.path === path)[0];
});
Bundler.prototype.getShallowDependencies.mockImplementation(
async module => {
const deps = mockedDependencyTree.get(module);
@ -105,7 +101,9 @@ describe('DeltaCalculator', () => {
},
);
deltaCalculator = await DeltaCalculator.create(bundlerMock, options);
const resolverMock = new Resolver();
deltaCalculator = new DeltaCalculator(bundlerMock, resolverMock, options);
});
it('should start listening for file changes after being initialized', async () => {