mirror of
https://github.com/status-im/metro.git
synced 2025-02-08 09:14:39 +00:00
Handle correctly multiple requires to the same file with different forms
Summary: The traverse dependencies logic was using the absolute path of the resolved dependency to group the module dependencies, so for example if there's a module like: ``` const a = require('./a'); const a1 = require('./a.js'); ``` The traversal dependencies logic was just outputting a single dependency for that module. Since we're transforming each `require()` call to replace the relative path by a dependencyMap, the code from above was transformed to: ``` const a = require(_dependencyMap[0]); const a1 = require(_dependencyMap[1]); ``` But since the traverse dependencies logic could only find a single dependency, `_dependencyMap[1]` was undefined, causing a runtime error. This fixes https://github.com/facebook/metro/issues/152 (more info in the task) Reviewed By: jeanlauliac Differential Revision: D7258093 fbshipit-source-id: 65c42b87e589430ecc96b906230dd7c4c55c2146
This commit is contained in:
parent
6a587db117
commit
233b00e836
@ -366,6 +366,28 @@ describe('edge cases', () => {
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should create two entries when requiring the same file in different forms', async () => {
|
||||||
|
const edges = new Map();
|
||||||
|
await initialTraverseDependencies('/bundle', dependencyGraph, {}, edges);
|
||||||
|
|
||||||
|
// We're adding a new reference from bundle to foo.
|
||||||
|
Actions.addDependency('/bundle', '/foo', 0, 'foo.js');
|
||||||
|
|
||||||
|
expect(
|
||||||
|
getPaths(
|
||||||
|
await traverseDependencies([...files], dependencyGraph, {}, edges),
|
||||||
|
),
|
||||||
|
).toEqual({
|
||||||
|
added: new Set(['/bundle']),
|
||||||
|
deleted: new Set(),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect([...edges.get(entryModule.path).dependencies]).toEqual([
|
||||||
|
['foo.js', '/foo'],
|
||||||
|
['foo', '/foo'],
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
it('should traverse the dependency tree in a deterministic order', async () => {
|
it('should traverse the dependency tree in a deterministic order', async () => {
|
||||||
// Mocks the shallow dependency call, always resolving the module in
|
// Mocks the shallow dependency call, always resolving the module in
|
||||||
// `slowPath` after the module in `fastPath`.
|
// `slowPath` after the module in `fastPath`.
|
||||||
|
@ -199,7 +199,7 @@ async function processEdge(
|
|||||||
onDependencyAdd: () => mixed,
|
onDependencyAdd: () => mixed,
|
||||||
onDependencyAdded: () => mixed,
|
onDependencyAdded: () => mixed,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const previousDependencies = new Set(edge.dependencies.values());
|
const previousDependencies = edge.dependencies;
|
||||||
|
|
||||||
const result = await dependencyGraph
|
const result = await dependencyGraph
|
||||||
.getModuleForPath(edge.path)
|
.getModuleForPath(edge.path)
|
||||||
@ -222,12 +222,12 @@ async function processEdge(
|
|||||||
edge.output.source = result.source;
|
edge.output.source = result.source;
|
||||||
edge.dependencies = new Map();
|
edge.dependencies = new Map();
|
||||||
|
|
||||||
currentDependencies.forEach((relativePath, absolutePath) => {
|
currentDependencies.forEach((absolutePath, relativePath) => {
|
||||||
edge.dependencies.set(relativePath, absolutePath);
|
edge.dependencies.set(relativePath, absolutePath);
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const absolutePath of previousDependencies.values()) {
|
for (const [relativePath, absolutePath] of previousDependencies) {
|
||||||
if (!currentDependencies.has(absolutePath)) {
|
if (!currentDependencies.has(relativePath)) {
|
||||||
removeDependency(edge, absolutePath, edges, delta);
|
removeDependency(edge, absolutePath, edges, delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -236,22 +236,24 @@ async function processEdge(
|
|||||||
// added and removed dependency, to get all the modules that have to be added
|
// added and removed dependency, to get all the modules that have to be added
|
||||||
// and removed from the dependency graph.
|
// and removed from the dependency graph.
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
Array.from(currentDependencies.keys()).map(async absolutePath => {
|
Array.from(currentDependencies.entries()).map(
|
||||||
if (previousDependencies.has(absolutePath)) {
|
async ([relativePath, absolutePath]) => {
|
||||||
return;
|
if (previousDependencies.has(relativePath)) {
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await addDependency(
|
await addDependency(
|
||||||
edge,
|
edge,
|
||||||
absolutePath,
|
absolutePath,
|
||||||
dependencyGraph,
|
dependencyGraph,
|
||||||
transformOptions,
|
transformOptions,
|
||||||
edges,
|
edges,
|
||||||
delta,
|
delta,
|
||||||
onDependencyAdd,
|
onDependencyAdd,
|
||||||
onDependencyAdded,
|
onDependencyAdded,
|
||||||
);
|
);
|
||||||
}),
|
},
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -370,12 +372,12 @@ function resolveDependencies(
|
|||||||
|
|
||||||
return new Map(
|
return new Map(
|
||||||
dependencies.map(relativePath => [
|
dependencies.map(relativePath => [
|
||||||
|
relativePath,
|
||||||
dependencyGraph.resolveDependency(
|
dependencyGraph.resolveDependency(
|
||||||
parentModule,
|
parentModule,
|
||||||
relativePath,
|
relativePath,
|
||||||
transformOptions.platform,
|
transformOptions.platform,
|
||||||
).path,
|
).path,
|
||||||
relativePath,
|
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user