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

Commit 3927778

Browse files
authored
Merge pull request #83 from arantes555/master
Adding an "only" option
2 parents 625c9fe + e608e77 commit 3927778

File tree

5 files changed

+47
-0
lines changed

5 files changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ 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 = Array.isArray(options.only)
48+
? options.only.map(o => o instanceof RegExp
49+
? o
50+
: new RegExp('^' + String(o).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&') + '$')
51+
)
52+
: null;
4753
const browserMapCache = {};
4854

4955
const onwarn = options.onwarn || CONSOLE_WARN;
@@ -99,6 +105,8 @@ export default function nodeResolve ( options = {} ) {
99105
id = resolve( importer, '..', importee );
100106
}
101107

108+
if (only && !only.some(pattern => pattern.test(id))) return null;
109+
102110
return new Promise( ( fulfil, reject ) => {
103111
let disregardResult = false;
104112
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)