Change signature of Resolver.wrapModule() to not depend on ResolutionResponse

Reviewed By: mjesun

Differential Revision: D5757102

fbshipit-source-id: 1cb514612ec431e48d669607c5332f26d19f53a0
This commit is contained in:
Rafael Oleza 2017-09-04 13:42:33 -07:00 committed by Facebook Github Bot
parent ebb14a56c6
commit 70a0916dd2
6 changed files with 75 additions and 35 deletions

View File

@ -87,7 +87,7 @@ class Bundle extends BundleBase {
* refactored. * refactored.
*/ */
resolver: {wrapModule: (options: any) => Promise<{code: any, map: any}>}, resolver: {wrapModule: (options: any) => Promise<{code: any, map: any}>},
resolutionResponse: mixed, resolutionResponse: any,
module: mixed, module: mixed,
/* $FlowFixMe: erroneous change of signature. */ /* $FlowFixMe: erroneous change of signature. */
moduleTransport: ModuleTransport, moduleTransport: ModuleTransport,
@ -96,12 +96,15 @@ class Bundle extends BundleBase {
const index = super.addModule(moduleTransport); const index = super.addModule(moduleTransport);
return resolver return resolver
.wrapModule({ .wrapModule({
resolutionResponse,
module, module,
getModuleId: resolutionResponse.getModuleId,
dependencyPairs: resolutionResponse.getResolvedDependencyPairs(module),
name: moduleTransport.name, name: moduleTransport.name,
code: moduleTransport.code, code: moduleTransport.code,
map: moduleTransport.map, map: moduleTransport.map,
meta: moduleTransport.meta, dependencyOffsets: moduleTransport.meta
? moduleTransport.meta.dependencyOffsets
: undefined,
minify: this._minify, minify: this._minify,
dev: this._dev, dev: this._dev,
}) })

View File

@ -50,9 +50,11 @@ class HMRBundle extends BundleBase {
moduleTransport: ModuleTransport, moduleTransport: ModuleTransport,
) { ) {
const code = resolver.resolveRequires( const code = resolver.resolveRequires(
response,
module, module,
/* $FlowFixMe: `getModuleId` is monkey-patched so may not exist */
response.getModuleId,
moduleTransport.code, moduleTransport.code,
response.getResolvedDependencyPairs(module),
/* $FlowFixMe: may not exist */ /* $FlowFixMe: may not exist */
moduleTransport.meta.dependencyOffsets, moduleTransport.meta.dependencyOffsets,
); );

View File

@ -6,6 +6,7 @@
* LICENSE file in the root directory of this source tree. An additional grant * 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. * of patent rights can be found in the PATENTS file in the same directory.
* *
* @emails oncall+javascript_tools
* @format * @format
*/ */
@ -15,6 +16,11 @@ const Bundle = require('../Bundle');
const ModuleTransport = require('../../lib/ModuleTransport'); const ModuleTransport = require('../../lib/ModuleTransport');
const crypto = require('crypto'); const crypto = require('crypto');
const resolutionResponse = {
getModuleId() {},
getResolvedDependencyPairs() {},
};
describe('Bundle', () => { describe('Bundle', () => {
var bundle; var bundle;
@ -137,7 +143,12 @@ describe('Bundle', () => {
const promise = Promise.all( const promise = Promise.all(
moduleTransports.map(m => moduleTransports.map(m =>
bundle.addModule(resolver, null, {isPolyfill: () => false}, m), bundle.addModule(
resolver,
resolutionResponse,
{isPolyfill: () => false},
m,
),
), ),
).then(() => { ).then(() => {
expect(bundle.getModules()).toEqual(moduleTransports); expect(bundle.getModules()).toEqual(moduleTransports);
@ -471,7 +482,7 @@ function addModule({
}) { }) {
return bundle.addModule( return bundle.addModule(
resolverFor(code, map), resolverFor(code, map),
null, resolutionResponse,
{isPolyfill: () => polyfill}, {isPolyfill: () => polyfill},
createModuleTransport({ createModuleTransport({
code, code,

View File

@ -246,11 +246,14 @@ describe('Resolver', function() {
return depResolver return depResolver
.wrapModule({ .wrapModule({
resolutionResponse,
module: module, module: module,
getModuleId: resolutionResponse.getModuleId,
dependencyPairs: resolutionResponse.getResolvedDependencyPairs(
module,
),
name: 'test module', name: 'test module',
code, code,
meta: {dependencyOffsets}, dependencyOffsets,
dev: false, dev: false,
}) })
.then(({code: processedCode}) => { .then(({code: processedCode}) => {
@ -286,7 +289,10 @@ describe('Resolver', function() {
}); });
return depResolver return depResolver
.wrapModule({ .wrapModule({
resolutionResponse, getModuleId: resolutionResponse.getModuleId,
dependencyPairs: resolutionResponse.getResolvedDependencyPairs(
module,
),
code, code,
module, module,
name: 'test module', name: 'test module',
@ -315,7 +321,10 @@ describe('Resolver', function() {
const inputMap = {version: 3, mappings: 'ARBITRARY'}; const inputMap = {version: 3, mappings: 'ARBITRARY'};
return depResolver return depResolver
.wrapModule({ .wrapModule({
resolutionResponse, getModuleId: resolutionResponse.getModuleId,
dependencyPairs: resolutionResponse.getResolvedDependencyPairs(
module,
),
module, module,
name: 'test module', name: 'test module',
code: 'arbitrary(code)', code: 'arbitrary(code)',
@ -368,7 +377,16 @@ describe('Resolver', function() {
it('should prefix JSON files with `module.exports=`', () => { it('should prefix JSON files with `module.exports=`', () => {
expect.assertions(1); expect.assertions(1);
return depResolver return depResolver
.wrapModule({resolutionResponse, module, name: id, code, dev: false}) .wrapModule({
getModuleId: resolutionResponse.getModuleId,
dependencyPairs: resolutionResponse.getResolvedDependencyPairs(
module,
),
module,
name: id,
code,
dev: false,
})
.then(({code: processedCode}) => .then(({code: processedCode}) =>
expect(processedCode).toEqual( expect(processedCode).toEqual(
[ [
@ -413,7 +431,10 @@ describe('Resolver', function() {
)});`; )});`;
return depResolver return depResolver
.wrapModule({ .wrapModule({
resolutionResponse, getModuleId: resolutionResponse.getModuleId,
dependencyPairs: resolutionResponse.getResolvedDependencyPairs(
module,
),
module, module,
name: id, name: id,
code, code,
@ -439,7 +460,10 @@ describe('Resolver', function() {
); );
return depResolver return depResolver
.wrapModule({ .wrapModule({
resolutionResponse, getModuleId: resolutionResponse.getModuleId,
dependencyPairs: resolutionResponse.getResolvedDependencyPairs(
module,
),
module, module,
name: id, name: id,
code, code,

View File

@ -179,22 +179,20 @@ class Resolver {
); );
} }
resolveRequires<T: ContainsTransformerOptions>( resolveRequires(
resolutionResponse: ResolutionResponse<Module, T>,
module: Module, module: Module,
getModuleId: ({path: string}) => number,
code: string, code: string,
dependencyPairs: $ReadOnlyArray<[string, Module]>,
dependencyOffsets: Array<number> = [], dependencyOffsets: Array<number> = [],
): string { ): string {
const resolvedDeps = Object.create(null); const resolvedDeps = Object.create(null);
// here, we build a map of all require strings (relative and absolute) // here, we build a map of all require strings (relative and absolute)
// to the canonical ID of the module they reference // to the canonical ID of the module they reference
resolutionResponse dependencyPairs.forEach(([name, module], key) => {
.getResolvedDependencyPairs(module) if (module) {
.forEach(([depName, depModule]) => { resolvedDeps[name] = getModuleId(module);
if (depModule) {
/* $FlowFixMe: `getModuleId` is monkey-patched so may not exist */
resolvedDeps[depName] = resolutionResponse.getModuleId(depModule);
} }
}); });
@ -217,27 +215,27 @@ class Resolver {
.join(''); .join('');
} }
wrapModule<T: ContainsTransformerOptions>({ wrapModule({
resolutionResponse,
module, module,
getModuleId,
dependencyPairs,
dependencyOffsets,
name, name,
map, map,
code, code,
meta = {},
dev = true, dev = true,
minify = false, minify = false,
}: { }: {
resolutionResponse: ResolutionResponse<Module, T>,
module: Module, module: Module,
getModuleId: ({path: string}) => number,
dependencyPairs: $ReadOnlyArray<[string, Module]>,
dependencyOffsets: Array<number>,
name: string, name: string,
map: ?MappingsMap, map: ?MappingsMap,
code: string, code: string,
meta?: {
dependencyOffsets?: Array<number>,
},
dev?: boolean, dev?: boolean,
minify?: boolean, minify?: boolean,
}) { }): Promise<{code: string, map: ?MappingsMap}> {
if (module.isJSON()) { if (module.isJSON()) {
code = `module.exports = ${code}`; code = `module.exports = ${code}`;
} }
@ -245,13 +243,14 @@ class Resolver {
if (module.isPolyfill()) { if (module.isPolyfill()) {
code = definePolyfillCode(code); code = definePolyfillCode(code);
} else { } else {
/* $FlowFixMe: `getModuleId` is monkey-patched so may not exist */ const moduleId = getModuleId(module);
const moduleId = resolutionResponse.getModuleId(module);
code = this.resolveRequires( code = this.resolveRequires(
resolutionResponse,
module, module,
getModuleId,
code, code,
meta.dependencyOffsets, dependencyPairs,
dependencyOffsets,
); );
code = defineModuleCode(moduleId, code, name, dev); code = defineModuleCode(moduleId, code, name, dev);
} }

View File

@ -22,6 +22,7 @@ type Metadata = {
dependencies?: ?Array<string>, dependencies?: ?Array<string>,
dependencyPairs?: Array<[string, Module]>, dependencyPairs?: Array<[string, Module]>,
preloaded: ?boolean, preloaded: ?boolean,
dependencyOffsets?: ?Array<number>,
}; };
class ModuleTransport { class ModuleTransport {