|
1 | | -'use strict'; |
2 | | - |
3 | | -/** |
4 | | - * @module |
5 | | - * @author Oleg Dutchenko <[email protected]> |
6 | | - * @version 1.0.3 |
7 | | - * @contributors |
8 | | - * - April Arcus {@link https://github.com/AprilArcus} |
9 | | - */ |
10 | | - |
11 | | -var path = require('path') |
12 | | - , escapeStringRegexp = require('escape-string-regexp') |
13 | | - , ESCAPED_NODE_MODULES = escapeStringRegexp('node_modules') |
14 | | - , ESCAPED_PATH_SEP = escapeStringRegexp(path.sep); |
15 | | - |
16 | | -// ---------------------------------------- |
17 | | -// Public |
18 | | -// ---------------------------------------- |
| 1 | +const path = require('path'); |
| 2 | +const escapeStringRegexp = require('escape-string-regexp'); |
| 3 | +const ESCAPED_NODE_MODULES = escapeStringRegexp('node_modules'); |
| 4 | +const ESCAPED_PATH_SEP = escapeStringRegexp(path.sep); |
19 | 5 |
|
20 | 6 | /** |
21 | 7 | * Creating a regular expression for excluding node modules |
22 | 8 | * from babel transpiling except for individual modules |
23 | 9 | * @param {string[]} [exceptionList] - exclude all modules except this list |
24 | 10 | * @return {RegExp} |
25 | 11 | */ |
26 | | -function babelLoaderExcludeNodeModulesExcept (exceptionList) { |
27 | | - var normalizedExceptionList |
28 | | - , alternationGroup |
29 | | - , negativeLookahead |
30 | | - |
| 12 | +function babelLoaderExcludeNodeModulesExcept(exceptionList) { |
31 | 13 | if (Array.isArray(exceptionList) && exceptionList.length) { |
32 | 14 | // Module names can contain path separators, e.g. "@types/react". |
33 | 15 | // Assume POSIX input and normalize for the current platform. |
34 | | - normalizedExceptionList = exceptionList.map(function (moduleName) { |
| 16 | + const normalizedExceptionList = exceptionList.map(function (moduleName) { |
35 | 17 | // We'll handle trailing path separators when we build the |
36 | 18 | // negative lookahead, so remove them if present. |
37 | 19 | if (moduleName[moduleName.length - 1] === path.posix.sep) { |
38 | 20 | moduleName = moduleName.slice(0, -1); |
39 | 21 | } |
40 | 22 | return moduleName.split(path.posix.sep).join(path.sep); |
41 | 23 | }); |
42 | | - alternationGroup = '(' + normalizedExceptionList.map(escapeStringRegexp).join('|') + ')'; |
| 24 | + |
| 25 | + const alternationGroup = |
| 26 | + '(' + normalizedExceptionList.map(escapeStringRegexp).join('|') + ')'; |
| 27 | + |
43 | 28 | // If the exception list includes e.g. "react", we don't want to |
44 | 29 | // accidentally make an exception for "react-dom", so make sure to |
45 | 30 | // include a trailing path separator inside the negative lookahead. |
46 | | - negativeLookahead = '(?!' + alternationGroup + ESCAPED_PATH_SEP + ')'; |
47 | | - return new RegExp(ESCAPED_NODE_MODULES + ESCAPED_PATH_SEP + negativeLookahead, 'i'); |
| 31 | + const negativeLookahead = '(?!' + alternationGroup + ESCAPED_PATH_SEP + ')'; |
| 32 | + |
| 33 | + return new RegExp( |
| 34 | + ESCAPED_NODE_MODULES + ESCAPED_PATH_SEP + negativeLookahead, |
| 35 | + 'i' |
| 36 | + ); |
48 | 37 | } else { |
49 | 38 | return new RegExp(ESCAPED_NODE_MODULES, 'i'); |
50 | 39 | } |
51 | 40 | } |
52 | 41 |
|
53 | | -// ---------------------------------------- |
54 | | -// Exports |
55 | | -// ---------------------------------------- |
56 | | - |
57 | 42 | module.exports = babelLoaderExcludeNodeModulesExcept; |
0 commit comments