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

Commit e608e77

Browse files
committed
Converting all "only" options to regexps and using Regexp.test for efficiency
1 parent 070810b commit e608e77

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ export default function nodeResolve ( options = {} ) {
4444
const preferBuiltins = isPreferBuiltinsSet ? options.preferBuiltins : true;
4545
const customResolveOptions = options.customResolveOptions || {};
4646
const jail = options.jail;
47-
const only = options.only;
47+
const only = Array.isArray(options.only)
48+
? options.only.map(o => o instanceof RegExp
49+
? o
50+
: new RegExp('^' + String(o).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&') + '$')
51+
)
52+
: null;
4853
const browserMapCache = {};
4954

5055
const onwarn = options.onwarn || CONSOLE_WARN;
@@ -100,7 +105,7 @@ export default function nodeResolve ( options = {} ) {
100105
id = resolve( importer, '..', importee );
101106
}
102107

103-
if (only && !only.some(pattern => id.match(pattern))) return null;
108+
if (only && !only.some(pattern => pattern.test(id))) return null;
104109

105110
return new Promise( ( fulfil, reject ) => {
106111
let disregardResult = false;

0 commit comments

Comments
 (0)