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

Commit

Permalink
Allows to specify an array of packages that will be the only ones imp…
Browse files Browse the repository at this point in the history
…orted.
  • Loading branch information
mehdi authored and arantes555 committed Mar 13, 2018
1 parent 625c9fe commit b6dcc5d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default function nodeResolve ( options = {} ) {
const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
const customResolveOptions = options.customResolveOptions || {};
const jail = options.jail;
const only = options.only;
const browserMapCache = {};

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

if (only && !only.some(pattern => id.match(pattern))) return null;

return new Promise( ( fulfil, reject ) => {
let disregardResult = false;
let packageBrowserField = false;
Expand Down
1 change: 1 addition & 0 deletions test/node_modules/@scoped/bar/index.js

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

7 changes: 7 additions & 0 deletions test/samples/only/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import foo from '@scoped/foo';
import bar from '@scoped/bar';
import test from 'test';

console.log( foo );
console.log( bar );
console.log( test );
26 changes: 26 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,32 @@ describe( 'rollup-plugin-node-resolve', function () {
});
});

it( '"only" option allows to specify the only packages to resolve', () => {
return rollup.rollup({
input: 'samples/only/main.js',
plugins: [
nodeResolve({
only: [ 'test' ]
})
]
}).then(bundle => {
assert.deepEqual( bundle.imports.sort(), [ '@scoped/bar', '@scoped/foo' ] );
});
});

it( '"only" option works with a regex', () => {
return rollup.rollup({
input: 'samples/only/main.js',
plugins: [
nodeResolve({
only: [ /@scoped\/.*/ ]
})
]
}).then(bundle => {
assert.deepEqual( bundle.imports.sort(), [ 'test' ] );
});
});

it( 'allows custom options', () => {
return rollup.rollup({
input: 'samples/custom-resolve-options/main.js',
Expand Down

0 comments on commit b6dcc5d

Please sign in to comment.