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

Commit

Permalink
Merge pull request #83 from arantes555/master
Browse files Browse the repository at this point in the history
Adding an "only" option
  • Loading branch information
keithamus committed Mar 17, 2018
2 parents 625c9fe + e608e77 commit 3927778
Show file tree
Hide file tree
Showing 5 changed files with 47 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
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
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 3927778

Please sign in to comment.