|
1 | 1 | 'use strict'; |
2 | 2 |
|
| 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 | + |
3 | 8 | /** |
4 | 9 | * @module |
5 | 10 | * @author Oleg Dutchenko <[email protected]> |
6 | | - * @version 1.0.0 |
| 11 | + * @version 1.0.2 |
7 | 12 | */ |
8 | 13 |
|
9 | 14 | // ---------------------------------------- |
|
17 | 22 | * @return {RegExp} |
18 | 23 | */ |
19 | 24 | function babelLoaderExcludeNodeModulesExcept (exceptionList) { |
| 25 | + var normalizedExceptionList |
| 26 | + , alternationGroup |
| 27 | + , negativeLookahead |
| 28 | + |
20 | 29 | 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'); |
22 | 48 | } |
23 | | - return /node_modules/i; |
24 | 49 | } |
25 | 50 |
|
26 | 51 | // ---------------------------------------- |
|
0 commit comments