Skip to content

Commit

Permalink
Collect isESMImport and propagate through to ResolutionContext
Browse files Browse the repository at this point in the history
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
  • Loading branch information
robhogan committed Oct 21, 2024
1 parent add4c76 commit a390857
Show file tree
Hide file tree
Showing 19 changed files with 137 additions and 24 deletions.
1 change: 1 addition & 0 deletions packages/metro-resolver/src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export function createResolutionContext(
realPath: candidate.realPath,
};
},
isESMImport: false,
mainFields: ['browser', 'main'],
nodeModulesPaths: [],
preferNativePlatform: false,
Expand Down
11 changes: 11 additions & 0 deletions packages/metro-resolver/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ export type ResolutionContext = $ReadOnly<{
*/
dependency?: TransformResultDependency,

/**
* Whether the dependency to be resolved was declared with an ESM import,
* ("import x from 'y'" or "await import('z')"), or a CommonJS "require".
* Corresponds to the criteria Node.js uses to assert an "import"
* resolution condition, vs "require".
*
* Always equal to dependency.data.isESMImport where dependency is provided,
* but may be used for resolution.
*/
isESMImport: boolean,

/**
* Synchonously returns information about a given absolute path, including
* whether it exists, whether it is a file or directory, and its absolute
Expand Down
11 changes: 11 additions & 0 deletions packages/metro-resolver/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ export interface ResolutionContext {
*/
readonly dependency?: TransformResultDependency;

/**
* Whether the dependency to be resolved was declared with an ESM import,
* ("import x from 'y'" or "await import('z')"), or a CommonJS "require".
* Corresponds to the criteria Node.js uses to assert an "import"
* resolution condition, vs "require".
*
* Always equal to dependency.data.isESMImport where dependency is provided,
* but may be used for resolution.
*/
readonly isESMImport: boolean;

/**
* Synchonously returns information about a given absolute path, including
* whether it exists, whether it is a file or directory, and its absolute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ const fooModule: Module<> = {
'./bar',
{
absolutePath: '/root/bar',
data: {data: {asyncType: null, locs: [], key: './bar'}, name: './bar'},
data: {
data: {asyncType: null, isESMImport: false, locs: [], key: './bar'},
name: './bar',
},
},
],
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ function createModule(
dep,
{
absolutePath: `/root/${dep}.js`,
data: {data: {asyncType: null, locs: [], key: dep}, name: dep},
data: {
data: {asyncType: null, isESMImport: false, locs: [], key: dep},
name: dep,
},
},
]),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ const fooModule: Module<> = {
'./bar',
{
absolutePath: '/root/bar.js',
data: {data: {asyncType: null, locs: [], key: './bar'}, name: './bar'},
data: {
data: {asyncType: null, isESMImport: false, locs: [], key: './bar'},
name: './bar',
},
},
],
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,20 @@ beforeEach(() => {
'bar',
{
absolutePath: '/bar.js',
data: {data: {asyncType: null, locs: [], key: 'bar'}, name: 'bar'},
data: {
data: {asyncType: null, isESMImport: false, locs: [], key: 'bar'},
name: 'bar',
},
},
],
[
'baz',
{
absolutePath: '/baz.js',
data: {data: {asyncType: null, locs: [], key: 'baz'}, name: 'baz'},
data: {
data: {asyncType: null, isESMImport: false, locs: [], key: 'baz'},
name: 'baz',
},
},
],
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ describe('DeltaCalculator + require.context', () => {
absolutePath: '/ctx?ctx=xxx',
data: {
name: 'ctx',
data: {key: 'ctx?ctx=xxx', asyncType: null, locs: []},
data: {
key: 'ctx?ctx=xxx',
asyncType: null,
isESMImport: false,
locs: [],
},
},
},
],
Expand All @@ -109,7 +114,12 @@ describe('DeltaCalculator + require.context', () => {
absolutePath: '/ctx/foo',
data: {
name: 'foo',
data: {key: 'foo', asyncType: null, locs: []},
data: {
key: 'foo',
asyncType: null,
isESMImport: false,
locs: [],
},
},
},
],
Expand Down
28 changes: 24 additions & 4 deletions packages/metro/src/DeltaBundler/__tests__/DeltaCalculator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ describe.each(['linux', 'win32'])('DeltaCalculator (%s)', osPlatform => {
absolutePath: p('/foo'),
data: {
name: 'foo',
data: {key: 'foo', asyncType: null, locs: []},
data: {
key: 'foo',
asyncType: null,
isESMImport: false,
locs: [],
},
},
},
],
Expand All @@ -104,7 +109,12 @@ describe.each(['linux', 'win32'])('DeltaCalculator (%s)', osPlatform => {
absolutePath: p('/bar'),
data: {
name: 'bar',
data: {key: 'bar', asyncType: null, locs: []},
data: {
key: 'bar',
asyncType: null,
isESMImport: false,
locs: [],
},
},
},
],
Expand All @@ -114,7 +124,12 @@ describe.each(['linux', 'win32'])('DeltaCalculator (%s)', osPlatform => {
absolutePath: p('/baz'),
data: {
name: 'baz',
data: {key: 'baz', asyncType: null, locs: []},
data: {
key: 'baz',
asyncType: null,
isESMImport: false,
locs: [],
},
},
},
],
Expand All @@ -132,7 +147,12 @@ describe.each(['linux', 'win32'])('DeltaCalculator (%s)', osPlatform => {
absolutePath: p('/qux'),
data: {
name: 'qux',
data: {key: 'qux', asyncType: null, locs: []},
data: {
key: 'qux',
asyncType: null,
isESMImport: false,
locs: [],
},
},
},
],
Expand Down
2 changes: 2 additions & 0 deletions packages/metro/src/DeltaBundler/__tests__/Graph-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ beforeEach(async () => {
name: dep.name,
data: {
asyncType: null,
isESMImport: false,
// $FlowFixMe[missing-empty-array-annot]
locs: [],
// $FlowFixMe[incompatible-call]
Expand Down Expand Up @@ -3498,6 +3499,7 @@ describe('reorderGraph', () => {
data: {
data: {
asyncType: null,
isESMImport: false,
locs: [],
key: path.substr(1),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ TestGraph {
"data": Object {
"data": Object {
"asyncType": null,
"isESMImport": false,
"key": "LB7P4TKrvfdUdViBXGaVopqz7Os=",
"locs": Array [],
},
Expand Down Expand Up @@ -39,6 +40,7 @@ TestGraph {
"data": Object {
"data": Object {
"asyncType": null,
"isESMImport": false,
"key": "W+de6an7x9bzpev84O0W/hS4K8U=",
"locs": Array [],
},
Expand All @@ -50,6 +52,7 @@ TestGraph {
"data": Object {
"data": Object {
"asyncType": null,
"isESMImport": false,
"key": "x6e9Oz1JO0QPfIBBjUad2qqGFjI=",
"locs": Array [],
},
Expand Down Expand Up @@ -137,6 +140,7 @@ TestGraph {
"data": Object {
"data": Object {
"asyncType": null,
"isESMImport": false,
"key": "LB7P4TKrvfdUdViBXGaVopqz7Os=",
"locs": Array [],
},
Expand Down
11 changes: 9 additions & 2 deletions packages/metro/src/DeltaBundler/__tests__/buildSubgraph-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ import nullthrows from 'nullthrows';
const makeTransformDep = (
name: string,
asyncType: null | 'weak' | 'async' = null,
isESMImport: boolean = false,
contextParams?: RequireContextParams,
): TransformResultDependency => ({
name,
data: {key: 'key-' + name, asyncType, locs: [], contextParams},
data: {
key: 'key-' + name + (isESMImport ? '-import' : ''),
asyncType,
isESMImport,
locs: [],
contextParams,
},
});
class BadTransformError extends Error {}
Expand All @@ -40,7 +47,7 @@ describe('GraphTraversal', () => {
[
'/entryWithContext',
[
makeTransformDep('virtual', null, {
makeTransformDep('virtual', null, false, {
filter: {
pattern: 'contextMatch.*',
flags: 'i',
Expand Down
1 change: 1 addition & 0 deletions packages/metro/src/DeltaBundler/__tests__/resolver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function dep(name: string): TransformResultDependency {
name,
data: {
asyncType: null,
isESMImport: false,
key: name,
locs: [],
},
Expand Down
5 changes: 5 additions & 0 deletions packages/metro/src/DeltaBundler/types.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export type TransformResultDependency = $ReadOnly<{
* If not null, this dependency is due to a dynamic `import()` or `__prefetchImport()` call.
*/
asyncType: AsyncDependencyType | null,
/**
* True if the dependency is declared with a static "import x from 'y'" or
* an import() call.
*/
isESMImport: boolean,
/**
* The dependency is enclosed in a try/catch block.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/metro/src/HmrServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class HmrServer<TClient: Client> {
data: {
key: entryFile,
asyncType: null,
isESMImport: false,
locs: [],
},
},
Expand Down
Loading

0 comments on commit a390857

Please sign in to comment.