Skip to content

Commit 68fd811

Browse files
committed
fix issues on win32 and over-matching
1 parent 2adf85a commit 68fd811

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

index.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
'use strict';
22

3+
var path = require('path')
4+
, escapeStringRegexp = require('escape-string-regexp')
5+
, ESCAPED_NODE_MODULES = escapeStringRegexp('node_modules')
6+
, ESCAPED_PATH_SEP = escapeStringRegexp(path.sep);
7+
38
/**
49
* @module
510
* @author Oleg Dutchenko <[email protected]>
6-
* @version 1.0.0
11+
* @version 1.0.2
712
*/
813

914
// ----------------------------------------
@@ -17,10 +22,30 @@
1722
* @return {RegExp}
1823
*/
1924
function babelLoaderExcludeNodeModulesExcept (exceptionList) {
25+
var normalizedExceptionList
26+
, alternationGroup
27+
, negativeLookahead
28+
2029
if (Array.isArray(exceptionList) && exceptionList.length) {
21-
return new RegExp(`node_modules[\\/|\\\\](?!(${exceptionList.join('|')})).*`, 'i');
30+
// Module names can contain path separators, e.g. "@types/react".
31+
// Assume POSIX input and normalize for the current platform.
32+
normalizedExceptionList = exceptionList.map(function (moduleName) {
33+
// We'll handle trailing path separators when we build the
34+
// negative lookahead, so remove them if present.
35+
if (moduleName[moduleName.length - 1] === path.posix.sep) {
36+
moduleName = moduleName.slice(0, -1);
37+
}
38+
return moduleName.split(path.posix.sep).join(path.sep);
39+
});
40+
alternationGroup = '(' + normalizedExceptionList.map(escapeStringRegexp).join('|') + ')';
41+
// If the exception list includes e.g. "react", we don't want to
42+
// accidentally make an exception for "react-dom", so make sure to
43+
// include a trailing path separator inside the negative lookahead.
44+
negativeLookahead = '(?!' + alternationGroup + ESCAPED_PATH_SEP + ')';
45+
return new RegExp(ESCAPED_NODE_MODULES + ESCAPED_PATH_SEP + negativeLookahead, 'i');
46+
} else {
47+
return new RegExp(ESCAPED_NODE_MODULES, 'i');
2248
}
23-
return /node_modules/i;
2449
}
2550

2651
// ----------------------------------------

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "babel-loader-exclude-node-modules-except",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Creating a regular expression for excluding node_modules from babel transpiling except for individual modules",
55
"main": "index.js",
66
"scripts": {
@@ -22,5 +22,8 @@
2222
"bugs": {
2323
"url": "https://github.com/WezomAgency/babel-loader-exclude-node-modules-except/issues"
2424
},
25-
"homepage": "https://github.com/WezomAgency/babel-loader-exclude-node-modules-except#readme"
25+
"homepage": "https://github.com/WezomAgency/babel-loader-exclude-node-modules-except#readme",
26+
"dependencies": {
27+
"escape-string-regexp": "^2.0.0"
28+
}
2629
}

0 commit comments

Comments
 (0)