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

View File

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

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
*/
@ -15,6 +16,11 @@ const Bundle = require('../Bundle');
const ModuleTransport = require('../../lib/ModuleTransport');
const crypto = require('crypto');
const resolutionResponse = {
getModuleId() {},
getResolvedDependencyPairs() {},
};
describe('Bundle', () => {
var bundle;
@ -137,7 +143,12 @@ describe('Bundle', () => {
const promise = Promise.all(
moduleTransports.map(m =>
bundle.addModule(resolver, null, {isPolyfill: () => false}, m),
bundle.addModule(
resolver,
resolutionResponse,
{isPolyfill: () => false},
m,
),
),
).then(() => {
expect(bundle.getModules()).toEqual(moduleTransports);
@ -471,7 +482,7 @@ function addModule({
}) {
return bundle.addModule(
resolverFor(code, map),
null,
resolutionResponse,
{isPolyfill: () => polyfill},
createModuleTransport({
code,

View File

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

View File

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

View File

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