Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node-resolve): pass original importee to secondary resolve #1557

Merged
merged 6 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/node-resolve/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,25 @@ this.resolve(importee, importer, {
});
```

## Resolve Options

After this plugin resolved an import id to its target file in `node_modules`, it will invoke `this.resolve` again with the resolved id. It will pass the following information in the resolve options:

```js
this.resolve(resolved.id, importer, {
custom: {
'node-resolve': {
resolved, // the object with information from node.js resolve
importee // the original import id
}
}
});
```

Your plugin can use the `importee` information to map an original import to its resolved file in `node_modules`, in a plugin hook such as `resolveId`.

The `resolved` object contains the resolved id, which is passed as the first parameter. It also has a property `moduleSideEffects`, which may contain the value from the npm `package.json` field `sideEffects` or `null`.

## Meta

[CONTRIBUTING](/.github/CONTRIBUTING.md)
Expand Down
2 changes: 1 addition & 1 deletion packages/node-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ export function nodeResolve(opts = {}) {
// `moduleSideEffects` information.
const resolvedResolved = await this.resolve(resolved.id, importer, {
...resolveOptions,
custom: { ...custom, 'node-resolve': { ...custom['node-resolve'], resolved } }
custom: { ...custom, 'node-resolve': { ...custom['node-resolve'], resolved, importee } }
});
if (resolvedResolved) {
// Handle plugins that manually make the result external
Expand Down
18 changes: 12 additions & 6 deletions packages/node-resolve/test/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ test('marks a module as external if the resolved version is external', async (t)
});
});

test('passes on "isEntry" flag', async (t) => {
test('passes on "isEntry" flag and original importee', async (t) => {
const resolveOptions = [];
await rollup({
input: 'entry/main.js',
Expand All @@ -561,6 +561,7 @@ test('passes on "isEntry" flag', async (t) => {
}
]
});

t.deepEqual(resolveOptions, [
['other.js', 'main.js', { assertions: {}, custom: {}, isEntry: true }],
['main.js', void 0, { assertions: {}, custom: {}, isEntry: true }],
Expand All @@ -574,7 +575,8 @@ test('passes on "isEntry" flag', async (t) => {
resolved: {
id: join(DIRNAME, 'fixtures', 'entry', 'other.js'),
moduleSideEffects: null
}
},
importee: './other.js'
}
},
isEntry: true
Expand All @@ -590,7 +592,8 @@ test('passes on "isEntry" flag', async (t) => {
resolved: {
id: join(DIRNAME, 'fixtures', 'entry', 'main.js'),
moduleSideEffects: null
}
},
importee: 'entry/main.js'
}
},
isEntry: true
Expand All @@ -607,7 +610,8 @@ test('passes on "isEntry" flag', async (t) => {
resolved: {
id: join(DIRNAME, 'fixtures', 'entry', 'dep.js'),
moduleSideEffects: null
}
},
importee: './dep.js'
}
},
isEntry: false
Expand Down Expand Up @@ -651,7 +655,8 @@ test('passes on custom options', async (t) => {
resolved: {
id: join(DIRNAME, 'fixtures', 'entry', 'main.js'),
moduleSideEffects: null
}
},
importee: 'entry/main.js'
}
},
isEntry: false
Expand All @@ -668,7 +673,8 @@ test('passes on custom options', async (t) => {
resolved: {
id: join(DIRNAME, 'fixtures', 'entry', 'other.js'),
moduleSideEffects: null
}
},
importee: 'entry/other.js'
}
},
isEntry: true
Expand Down
Loading