Skip to content

Commit 9b49c1e

Browse files
Allow wildcards for matching except names
1 parent a4fbef1 commit 9b49c1e

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

__specs__/index.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,45 @@ describe('Should create a correct regular expression for excluding node_modules'
4242
});
4343
});
4444

45+
describe('Should be able to convert wildcards in parameters to matching pattern in the regular expression', () => {
46+
/**
47+
* @type {TestCase[]}
48+
*/
49+
const testCases = [
50+
{
51+
description: 'Wildcard occurs in the only argument',
52+
params: ['react-*'],
53+
expected: X
54+
? /node_modules\\(?!(react\-(?:.*))\\)/i
55+
: /node_modules\/(?!(react\-(?:.*))\/)/i
56+
},
57+
{
58+
description: 'Wildcard occurs in the second argument out of three',
59+
params: ['abc', 'mno*', 'xvz'],
60+
expected: X
61+
? /node_modules\\(?!(abc|mno(?:.*)|xvz)\\)/i
62+
: /node_modules\/(?!(abc|mno(?:.*)|xvz)\/)/i
63+
},
64+
{
65+
description: 'Wildcard used for scoped packages',
66+
params: ['@awesomecorp/*'],
67+
expected: X
68+
? /node_modules\\(?!(@awesomecorp\\(?:.*))\\)/i
69+
: /node_modules\/(?!(@awesomecorp\/(?:.*))\/)/i
70+
}
71+
];
72+
73+
testCases.forEach(function ({ params, expected, description }, i) {
74+
test(`Test case #${i + 1}: ${description}`, function () {
75+
const result = babelLoaderExcludeNodeModulesExcept(params);
76+
expect(result).toEqual(expected);
77+
});
78+
});
79+
});
80+
4581
/**
4682
* @typedef {Object} TestCase
4783
* @property {string[]} params
4884
* @property {RegExp} expected
85+
* @property {string} [description]
4986
*/

index.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,29 @@ function babelLoaderExcludeNodeModulesExcept(exceptionList) {
2323
});
2424

2525
const alternationGroup =
26-
'(' + normalizedExceptionList.map(escapeStringRegexp).join('|') + ')';
26+
'(' +
27+
normalizedExceptionList
28+
.map((item) => {
29+
// Breaking down string by wildcards. For every
30+
// occurrance between wildcards build the portion
31+
// with escapeStringRegexp.
32+
// For the wildcards, replace them by a non-capturing
33+
// group matching everything.
34+
const match = item.match(/([^*]*)/g);
35+
return match
36+
.map((m, i) => {
37+
if (m.length > 0) {
38+
return escapeStringRegexp(m);
39+
} else if (i !== match.length - 1 && m.length === 0) {
40+
return '(?:.*)';
41+
} else {
42+
return '';
43+
}
44+
})
45+
.join('');
46+
})
47+
.join('|') +
48+
')';
2749

2850
// If the exception list includes e.g. "react", we don't want to
2951
// accidentally make an exception for "react-dom", so make sure to

0 commit comments

Comments
 (0)