Skip to content

Commit 9b9da3a

Browse files
jchipJoel Chen
and
Joel Chen
authored
feat(node-resolve): pass original importee to secondary resolve (#1557)
* 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 <[email protected]>
1 parent 786e543 commit 9b9da3a

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

packages/node-resolve/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,25 @@ this.resolve(importee, importer, {
234234
});
235235
```
236236

237+
## Resolve Options
238+
239+
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:
240+
241+
```js
242+
this.resolve(resolved.id, importer, {
243+
custom: {
244+
'node-resolve': {
245+
resolved, // the object with information from node.js resolve
246+
importee // the original import id
247+
}
248+
}
249+
});
250+
```
251+
252+
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`.
253+
254+
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`.
255+
237256
## Meta
238257

239258
[CONTRIBUTING](/.github/CONTRIBUTING.md)

packages/node-resolve/src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export function nodeResolve(opts = {}) {
301301
// `moduleSideEffects` information.
302302
const resolvedResolved = await this.resolve(resolved.id, importer, {
303303
...resolveOptions,
304-
custom: { ...custom, 'node-resolve': { ...custom['node-resolve'], resolved } }
304+
custom: { ...custom, 'node-resolve': { ...custom['node-resolve'], resolved, importee } }
305305
});
306306
if (resolvedResolved) {
307307
// Handle plugins that manually make the result external

packages/node-resolve/test/test.mjs

+12-6
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ test('marks a module as external if the resolved version is external', async (t)
538538
});
539539
});
540540

541-
test('passes on "isEntry" flag', async (t) => {
541+
test('passes on "isEntry" flag and original importee', async (t) => {
542542
const resolveOptions = [];
543543
await rollup({
544544
input: 'entry/main.js',
@@ -561,6 +561,7 @@ test('passes on "isEntry" flag', async (t) => {
561561
}
562562
]
563563
});
564+
564565
t.deepEqual(resolveOptions, [
565566
['other.js', 'main.js', { assertions: {}, custom: {}, isEntry: true }],
566567
['main.js', void 0, { assertions: {}, custom: {}, isEntry: true }],
@@ -574,7 +575,8 @@ test('passes on "isEntry" flag', async (t) => {
574575
resolved: {
575576
id: join(DIRNAME, 'fixtures', 'entry', 'other.js'),
576577
moduleSideEffects: null
577-
}
578+
},
579+
importee: './other.js'
578580
}
579581
},
580582
isEntry: true
@@ -590,7 +592,8 @@ test('passes on "isEntry" flag', async (t) => {
590592
resolved: {
591593
id: join(DIRNAME, 'fixtures', 'entry', 'main.js'),
592594
moduleSideEffects: null
593-
}
595+
},
596+
importee: 'entry/main.js'
594597
}
595598
},
596599
isEntry: true
@@ -607,7 +610,8 @@ test('passes on "isEntry" flag', async (t) => {
607610
resolved: {
608611
id: join(DIRNAME, 'fixtures', 'entry', 'dep.js'),
609612
moduleSideEffects: null
610-
}
613+
},
614+
importee: './dep.js'
611615
}
612616
},
613617
isEntry: false
@@ -651,7 +655,8 @@ test('passes on custom options', async (t) => {
651655
resolved: {
652656
id: join(DIRNAME, 'fixtures', 'entry', 'main.js'),
653657
moduleSideEffects: null
654-
}
658+
},
659+
importee: 'entry/main.js'
655660
}
656661
},
657662
isEntry: false
@@ -668,7 +673,8 @@ test('passes on custom options', async (t) => {
668673
resolved: {
669674
id: join(DIRNAME, 'fixtures', 'entry', 'other.js'),
670675
moduleSideEffects: null
671-
}
676+
},
677+
importee: 'entry/other.js'
672678
}
673679
},
674680
isEntry: true

0 commit comments

Comments
 (0)