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

Commit 4387b8a

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

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ export default {
5353
// Lock the module search in this path (like a chroot). Module defined
5454
// outside this path will be marked as external
5555
jail: '/my/jail/path', // Default: '/'
56+
57+
// Set to an array of strings and/or regexps to lock the module search
58+
// to modules that match at least one entry. Modules not matching any
59+
// entry will be marked as external
60+
only: ['some_module', /^@some_scope\/.*$/], // Default: null
5661

5762
// If true, inspect resolved files to check that they are
5863
// ES2015 modules

src/index.js

+3
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

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/samples/only/main.js

+7
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

+26
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)