From 085dd97fa015b39584dc87fd3135324b7690e942 Mon Sep 17 00:00:00 2001 From: Rafael Oleza Date: Tue, 21 Nov 2017 05:22:29 -0800 Subject: [PATCH] Change getModuleId() signature to directly accept a path Reviewed By: mjesun Differential Revision: D6373180 fbshipit-source-id: d6b49431adf85e7587cf0fe858d9dc9b9afeee06 --- .../src/DeltaBundler/DeltaTransformer.js | 22 +++++++++---------- .../src/Resolver/__tests__/Resolver-test.js | 12 +++++----- packages/metro-bundler/src/Resolver/index.js | 8 +++---- .../src/lib/createModuleIdFactory.js | 10 ++++----- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/packages/metro-bundler/src/DeltaBundler/DeltaTransformer.js b/packages/metro-bundler/src/DeltaBundler/DeltaTransformer.js index f6198313..ecc9b595 100644 --- a/packages/metro-bundler/src/DeltaBundler/DeltaTransformer.js +++ b/packages/metro-bundler/src/DeltaBundler/DeltaTransformer.js @@ -76,7 +76,7 @@ class DeltaTransformer extends EventEmitter { _resolver: Resolver; _getPolyfills: ({platform: ?string}) => $ReadOnlyArray; _polyfillModuleNames: $ReadOnlyArray; - _getModuleId: ({path: string}) => number; + _getModuleId: (path: string) => number; _deltaCalculator: DeltaCalculator; _bundleOptions: BundleOptions; _currentBuildPromise: ?Promise; @@ -175,9 +175,9 @@ class DeltaTransformer extends EventEmitter { const output = Object.create(null); for (const [path, {inverseDependencies}] of dependencyEdges.entries()) { - output[this._getModuleId({path})] = Array.from( + output[this._getModuleId(path)] = Array.from( inverseDependencies, - ).map(dep => this._getModuleId({path: dep})); + ).map(dep => this._getModuleId(dep)); } return output; @@ -241,7 +241,7 @@ class DeltaTransformer extends EventEmitter { // Precalculate all module ids sequentially. We do this to be sure that the // mapping between module -> moduleId is deterministic between runs. const modules = Array.from(modified.values()); - modules.forEach(module => this._getModuleId(module)); + modules.forEach(module => this._getModuleId(module.path)); // Get the transformed source code of each modified/added module. const modifiedDelta = await this._transformModules( @@ -251,7 +251,7 @@ class DeltaTransformer extends EventEmitter { ); deleted.forEach(id => { - modifiedDelta.set(this._getModuleId({path: id}), null); + modifiedDelta.set(this._getModuleId(id), null); }); // Return the source code that gets appended to all the modules. This @@ -345,19 +345,17 @@ class DeltaTransformer extends EventEmitter { // 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 = dependencyGraph.getAbsolutePath( + const entryPointModulePath = dependencyGraph.getAbsolutePath( this._bundleOptions.entryFile, ); - const entryPointModule = dependencyGraph.getModuleForPath(absPath); // First, get the modules correspondant to all the module names defined in // the `runBeforeMainModule` config variable. Then, append the entry point // module so the last thing that gets required is the entry point. const append = new Map( this._bundleOptions.runBeforeMainModule - .map(path => dependencyGraph.getModuleForPath(path)) - .concat(entryPointModule) - .filter(module => dependencyEdges.has(module.path)) + .concat(entryPointModulePath) + .filter(path => dependencyEdges.has(path)) .map(this._getModuleId) .map(moduleId => { const code = `require(${JSON.stringify(moduleId)});`; @@ -381,7 +379,7 @@ class DeltaTransformer extends EventEmitter { if (this._bundleOptions.sourceMapUrl) { const code = '//# sourceMappingURL=' + this._bundleOptions.sourceMapUrl; - const id = this._getModuleId({path: '/sourcemap.js'}); + const id = this._getModuleId('/sourcemap.js'); append.set(id, { code, @@ -452,7 +450,7 @@ class DeltaTransformer extends EventEmitter { ) : wrapped; - const id = this._getModuleId(module); + const id = this._getModuleId(module.path); return [ id, diff --git a/packages/metro-bundler/src/Resolver/__tests__/Resolver-test.js b/packages/metro-bundler/src/Resolver/__tests__/Resolver-test.js index 88639be0..ca6bcc51 100644 --- a/packages/metro-bundler/src/Resolver/__tests__/Resolver-test.js +++ b/packages/metro-bundler/src/Resolver/__tests__/Resolver-test.js @@ -147,7 +147,7 @@ describe('Resolver', function() { .map(([importId, module]) => [ importId, padRight( - resolutionResponse.getModuleId(module), + resolutionResponse.getModuleId(module.path), importId.length + 2, ), ]), @@ -186,7 +186,7 @@ describe('Resolver', function() { "require( 'z' )", 'require( "a")', 'require("b" )', - `}, ${resolutionResponse.getModuleId(module)});`, + `}, ${resolutionResponse.getModuleId(module.path)});`, ].join('\n'), ); }); @@ -213,7 +213,9 @@ describe('Resolver', function() { [ '__d(/* test module */function(global, require, module, exports) {' + code, - `}, ${resolutionResponse.getModuleId(module)}, null, "test module");`, + `}, ${resolutionResponse.getModuleId( + module.path, + )}, null, "test module");`, ].join('\n'), ); }); @@ -295,7 +297,7 @@ describe('Resolver', function() { [ `__d(/* ${id} */function(global, require, module, exports) {`, `module.exports = ${code}\n}, ${resolutionResponse.getModuleId( - module, + module.path, )});`, ].join(''), ); @@ -359,7 +361,7 @@ describe('Resolver', function() { return id; } - return ({path}) => knownIds.get(path) || createId(path); + return path => knownIds.get(path) || createId(path); } function padRight(value, width) { diff --git a/packages/metro-bundler/src/Resolver/index.js b/packages/metro-bundler/src/Resolver/index.js index 204863ba..9d207fca 100644 --- a/packages/metro-bundler/src/Resolver/index.js +++ b/packages/metro-bundler/src/Resolver/index.js @@ -121,7 +121,7 @@ class Resolver { resolveRequires( module: Module, - getModuleId: ({path: string}) => number, + getModuleId: (path: string) => number, code: string, dependencyPairs: Map, dependencyOffsets: Array = [], @@ -131,7 +131,7 @@ class Resolver { // here, we build a map of all require strings (relative and absolute) // to the canonical ID of the module they reference for (const [name, path] of dependencyPairs) { - resolvedDeps[name] = getModuleId({path}); + resolvedDeps[name] = getModuleId(path); } // if we have a canonical ID for the module imported here, @@ -164,7 +164,7 @@ class Resolver { dev = true, }: { module: Module, - getModuleId: ({path: string}) => number, + getModuleId: (path: string) => number, dependencyPairs: Map, dependencyOffsets: Array, name: string, @@ -179,7 +179,7 @@ class Resolver { if (module.isPolyfill()) { code = definePolyfillCode(code); } else { - const moduleId = getModuleId(module); + const moduleId = getModuleId(module.path); code = this.resolveRequires( module, diff --git a/packages/metro-bundler/src/lib/createModuleIdFactory.js b/packages/metro-bundler/src/lib/createModuleIdFactory.js index 82cf7d65..76c5dbb7 100644 --- a/packages/metro-bundler/src/lib/createModuleIdFactory.js +++ b/packages/metro-bundler/src/lib/createModuleIdFactory.js @@ -11,15 +11,15 @@ 'use strict'; -function createModuleIdFactory(): ({path: string}) => number { +function createModuleIdFactory(): (path: string) => number { const fileToIdMap = new Map(); let nextId = 0; - return ({path: modulePath}) => { - if (!fileToIdMap.has(modulePath)) { - fileToIdMap.set(modulePath, nextId); + return (path: string) => { + if (!fileToIdMap.has(path)) { + fileToIdMap.set(path, nextId); nextId += 1; } - return fileToIdMap.get(modulePath); + return fileToIdMap.get(path); }; }