From 070810b7b5d3183d8f469adbe2e4d745bf6cc3ae Mon Sep 17 00:00:00 2001 From: mehdi Date: Tue, 4 Oct 2016 18:53:44 -0700 Subject: [PATCH 1/2] Allows to specify an array of packages that will be the only ones imported. --- README.md | 5 +++++ src/index.js | 3 +++ test/node_modules/@scoped/bar/index.js | 1 + test/samples/only/main.js | 7 +++++++ test/test.js | 26 ++++++++++++++++++++++++++ 5 files changed, 42 insertions(+) create mode 100644 test/node_modules/@scoped/bar/index.js create mode 100644 test/samples/only/main.js 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..a91ea2c 100644 --- a/src/index.js +++ b/src/index.js @@ -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; @@ -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; 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', From e608e7797ba8498e66f4f585c85bbe9e595ef7fb Mon Sep 17 00:00:00 2001 From: mehdi Date: Fri, 16 Mar 2018 09:57:11 +0100 Subject: [PATCH 2/2] Converting all "only" options to regexps and using Regexp.test for efficiency --- src/index.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index a91ea2c..60cbe13 100644 --- a/src/index.js +++ b/src/index.js @@ -44,7 +44,12 @@ export default function nodeResolve ( options = {} ) { const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true; const customResolveOptions = options.customResolveOptions || {}; const jail = options.jail; - const only = options.only; + 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; @@ -100,7 +105,7 @@ export default function nodeResolve ( options = {} ) { id = resolve( importer, '..', importee ); } - if (only && !only.some(pattern => id.match(pattern))) return null; + if (only && !only.some(pattern => pattern.test(id))) return null; return new Promise( ( fulfil, reject ) => { let disregardResult = false;