diff --git a/README.md b/README.md index 7b75736..c4d7ff6 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/index.js b/src/index.js index a5dcce3..60cbe13 100644 --- a/src/index.js +++ b/src/index.js @@ -44,6 +44,12 @@ export default function nodeResolve ( options = {} ) { const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true; const customResolveOptions = options.customResolveOptions || {}; const jail = options.jail; + const only = Array.isArray(options.only) + ? options.only.map(o => o instanceof RegExp + ? o + : new RegExp('^' + String(o).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&') + '$') + ) + : null; const browserMapCache = {}; const onwarn = options.onwarn || CONSOLE_WARN; @@ -99,6 +105,8 @@ export default function nodeResolve ( options = {} ) { id = resolve( importer, '..', importee ); } + if (only && !only.some(pattern => pattern.test(id))) return null; + return new Promise( ( fulfil, reject ) => { let disregardResult = false; let packageBrowserField = false; diff --git a/test/node_modules/@scoped/bar/index.js b/test/node_modules/@scoped/bar/index.js new file mode 100644 index 0000000..d742342 --- /dev/null +++ b/test/node_modules/@scoped/bar/index.js @@ -0,0 +1 @@ +export default 'BAR'; diff --git a/test/samples/only/main.js b/test/samples/only/main.js new file mode 100644 index 0000000..4245d8b --- /dev/null +++ b/test/samples/only/main.js @@ -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 ); diff --git a/test/test.js b/test/test.js index 20fa188..1613ec9 100644 --- a/test/test.js +++ b/test/test.js @@ -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',