diff --git a/packages/node-resolve/README.md b/packages/node-resolve/README.md index 566d4fe71..b4943a7eb 100755 --- a/packages/node-resolve/README.md +++ b/packages/node-resolve/README.md @@ -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) diff --git a/packages/node-resolve/src/index.js b/packages/node-resolve/src/index.js index 559ae1516..462a6a907 100644 --- a/packages/node-resolve/src/index.js +++ b/packages/node-resolve/src/index.js @@ -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 diff --git a/packages/node-resolve/test/test.mjs b/packages/node-resolve/test/test.mjs index ff6fbb8b2..dbe574347 100755 --- a/packages/node-resolve/test/test.mjs +++ b/packages/node-resolve/test/test.mjs @@ -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', @@ -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 }], @@ -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 @@ -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 @@ -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 @@ -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 @@ -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