Skip to content

Commit

Permalink
fix(node-resolve): handle nested entry modules with the resolveOnly o…
Browse files Browse the repository at this point in the history
…ption (#430)
  • Loading branch information
lukastaegert authored Jun 1, 2020
1 parent eb73b0d commit 7c38cb2
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/node-resolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@babel/preset-env": "^7.9.0",
"@rollup/plugin-json": "^4.0.1",
"es5-ext": "^0.10.53",
"rollup": "^2.0.0",
"rollup": "^2.12.0",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.1.0",
"source-map": "^0.7.3",
Expand Down
3 changes: 1 addition & 2 deletions packages/node-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,12 @@ export function nodeResolve(opts = {}) {
isRelativeImport = true;
}

const input = normalizeInput(rollupOptions.input);
if (
!isRelativeImport &&
resolveOnly.length &&
!resolveOnly.some((pattern) => pattern.test(id))
) {
if (input.includes(id)) {
if (normalizeInput(rollupOptions.input).includes(importee)) {
return null;
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion packages/node-resolve/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export function normalizeInput(input) {
}

// otherwise it's a string
return input;
return [input];
}

// Resolve module specifiers in order. Promise resolves to the first module that resolves
Expand Down
10 changes: 10 additions & 0 deletions packages/node-resolve/test/fixtures/nested/only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import foo from '@scoped/foo';
import bar from '@scoped/bar';
import test from 'test';

import local from '../only-local';

console.log(foo);
console.log(bar);
console.log(test);
console.log(local);
20 changes: 20 additions & 0 deletions packages/node-resolve/test/only.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ test('specify the only packages to resolve', async (t) => {
t.assert(Object.keys(modules).includes(resolve('only-local.js')));
});

test('handles nested entry modules', async (t) => {
const warnings = [];
const bundle = await rollup({
input: ['nested/only.js'],
onwarn: (warning) => warnings.push(warning),
plugins: [
nodeResolve({
resolveOnly: ['test']
})
]
});
const imports = await getImports(bundle);
const modules = await getResolvedModules(bundle);

t.is(warnings.length, 0);
t.snapshot(warnings);
t.deepEqual(imports, ['@scoped/foo', '@scoped/bar']);
t.assert(Object.keys(modules).includes(resolve('only-local.js')));
});

test('regex', async (t) => {
const warnings = [];
const bundle = await rollup({
Expand Down
6 changes: 6 additions & 0 deletions packages/node-resolve/test/snapshots/only.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ Generated by [AVA](https://ava.li).
> Snapshot 1
[]

## handles nested entry modules

> Snapshot 1
[]
Binary file modified packages/node-resolve/test/snapshots/only.js.snap
Binary file not shown.
58 changes: 49 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7c38cb2

Please sign in to comment.