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); .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. * 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. // Build the modules from the files that have been modified.
const modified = new Map( const modified = new Map(
await Promise.all( modifiedArray.map(file => {
modifiedArray.map(async file => { const module = this._resolver.getModuleForPath(file);
const module = await this._bundler.getModuleForPath(file); return [file, module];
return [file, module]; }),
}),
),
); );
const filesWithChangedDependencies = await Promise.all( const filesWithChangedDependencies = await Promise.all(
@ -238,7 +227,7 @@ class DeltaCalculator extends EventEmitter {
} }
async _hasChangedDependencies(file: string) { async _hasChangedDependencies(file: string) {
const module = await this._bundler.getModuleForPath(file); const module = this._resolver.getModuleForPath(file);
if (!this._dependencies.has(module.path)) { if (!this._dependencies.has(module.path)) {
return false; return false;

View File

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

View File

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