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 4387b8a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export default {
// Lock the module search in this path (like a chroot). Module defined
// outside this path will be marked as external
jail: '/my/jail/path', // Default: '/'

// Set to an array of strings and/or regexps to lock the module search
// to modules that match at least one entry. Modules not matching any
// entry will be marked as external
only: ['some_module', /^@some_scope\/.*$/], // Default: null

// If true, inspect resolved files to check that they are
// ES2015 modules
Expand Down
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 4387b8a

Please sign in to comment.