Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit b6dcc5d

Browse files
mehdiarantes555
authored andcommitted
Allows to specify an array of packages that will be the only ones imported.
1 parent 625c9fe commit b6dcc5d

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export default function nodeResolve ( options = {} ) {
4444
const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
4545
const customResolveOptions = options.customResolveOptions || {};
4646
const jail = options.jail;
47+
const only = options.only;
4748
const browserMapCache = {};
4849

4950
const onwarn = options.onwarn || CONSOLE_WARN;
@@ -99,6 +100,8 @@ export default function nodeResolve ( options = {} ) {
99100
id = resolve( importer, '..', importee );
100101
}
101102

103+
if (only && !only.some(pattern => id.match(pattern))) return null;
104+
102105
return new Promise( ( fulfil, reject ) => {
103106
let disregardResult = false;
104107
let packageBrowserField = false;

test/node_modules/@scoped/bar/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/samples/only/main.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import foo from '@scoped/foo';
2+
import bar from '@scoped/bar';
3+
import test from 'test';
4+
5+
console.log( foo );
6+
console.log( bar );
7+
console.log( test );

test/test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,32 @@ describe( 'rollup-plugin-node-resolve', function () {
552552
});
553553
});
554554

555+
it( '"only" option allows to specify the only packages to resolve', () => {
556+
return rollup.rollup({
557+
input: 'samples/only/main.js',
558+
plugins: [
559+
nodeResolve({
560+
only: [ 'test' ]
561+
})
562+
]
563+
}).then(bundle => {
564+
assert.deepEqual( bundle.imports.sort(), [ '@scoped/bar', '@scoped/foo' ] );
565+
});
566+
});
567+
568+
it( '"only" option works with a regex', () => {
569+
return rollup.rollup({
570+
input: 'samples/only/main.js',
571+
plugins: [
572+
nodeResolve({
573+
only: [ /@scoped\/.*/ ]
574+
})
575+
]
576+
}).then(bundle => {
577+
assert.deepEqual( bundle.imports.sort(), [ 'test' ] );
578+
});
579+
});
580+
555581
it( 'allows custom options', () => {
556582
return rollup.rollup({
557583
input: 'samples/custom-resolve-options/main.js',

0 commit comments

Comments
 (0)