Skip to content

Commit

Permalink
feat(node-resolve): pass original importee to secondary resolve (#1557)
Browse files Browse the repository at this point in the history
* feat(node-resolve): pass original importee to secondary resolve

* reverse snapshot updates

* reverse test.mjs snapshot updates

* fix styles

* update README

* add info about moduleSideEffects in resolved object

---------

Co-authored-by: Joel Chen <joel_chen@intuit.com>
  • Loading branch information
jchip and Joel Chen committed Aug 17, 2023
1 parent 786e543 commit 9b9da3a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
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

0 comments on commit 9b9da3a

Please sign in to comment.