mirror of https://github.com/status-im/metro.git
Change getModuleId() signature to directly accept a path
Reviewed By: mjesun Differential Revision: D6373180 fbshipit-source-id: d6b49431adf85e7587cf0fe858d9dc9b9afeee06
This commit is contained in:
parent
e58b7f1d73
commit
085dd97fa0
|
@ -76,7 +76,7 @@ class DeltaTransformer extends EventEmitter {
|
|||
_resolver: Resolver;
|
||||
_getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>;
|
||||
_polyfillModuleNames: $ReadOnlyArray<string>;
|
||||
_getModuleId: ({path: string}) => number;
|
||||
_getModuleId: (path: string) => number;
|
||||
_deltaCalculator: DeltaCalculator;
|
||||
_bundleOptions: BundleOptions;
|
||||
_currentBuildPromise: ?Promise<DeltaTransformResponse>;
|
||||
|
@ -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,
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -121,7 +121,7 @@ class Resolver {
|
|||
|
||||
resolveRequires(
|
||||
module: Module,
|
||||
getModuleId: ({path: string}) => number,
|
||||
getModuleId: (path: string) => number,
|
||||
code: string,
|
||||
dependencyPairs: Map<string, string>,
|
||||
dependencyOffsets: Array<number> = [],
|
||||
|
@ -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<string, string>,
|
||||
dependencyOffsets: Array<number>,
|
||||
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,
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue