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

Adding an "only" option #83

Merged
merged 2 commits into from
Mar 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For performance reasons it might also make sense to initially convert all entries to RegExp (in L.47 where it will only happen once) and then use pattern.test(id) instead (which will make all invocations faster, cf. e.g. https://stackoverflow.com/a/10940138)


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