Skip to content

Commit

Permalink
fix issues on win32 and over-matching
Browse files Browse the repository at this point in the history
  • Loading branch information
AprilArcus committed Aug 26, 2019
1 parent 2adf85a commit 68fd811
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
31 changes: 28 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use strict';

var path = require('path')
, escapeStringRegexp = require('escape-string-regexp')
, ESCAPED_NODE_MODULES = escapeStringRegexp('node_modules')
, ESCAPED_PATH_SEP = escapeStringRegexp(path.sep);

/**
* @module
* @author Oleg Dutchenko <[email protected]>
* @version 1.0.0
* @version 1.0.2
*/

// ----------------------------------------
Expand All @@ -17,10 +22,30 @@
* @return {RegExp}
*/
function babelLoaderExcludeNodeModulesExcept (exceptionList) {
var normalizedExceptionList
, alternationGroup
, negativeLookahead

if (Array.isArray(exceptionList) && exceptionList.length) {
return new RegExp(`node_modules[\\/|\\\\](?!(${exceptionList.join('|')})).*`, 'i');
// Module names can contain path separators, e.g. "@types/react".
// Assume POSIX input and normalize for the current platform.
normalizedExceptionList = exceptionList.map(function (moduleName) {
// We'll handle trailing path separators when we build the
// negative lookahead, so remove them if present.
if (moduleName[moduleName.length - 1] === path.posix.sep) {
moduleName = moduleName.slice(0, -1);
}
return moduleName.split(path.posix.sep).join(path.sep);
});
alternationGroup = '(' + normalizedExceptionList.map(escapeStringRegexp).join('|') + ')';
// If the exception list includes e.g. "react", we don't want to
// accidentally make an exception for "react-dom", so make sure to
// include a trailing path separator inside the negative lookahead.
negativeLookahead = '(?!' + alternationGroup + ESCAPED_PATH_SEP + ')';
return new RegExp(ESCAPED_NODE_MODULES + ESCAPED_PATH_SEP + negativeLookahead, 'i');
} else {
return new RegExp(ESCAPED_NODE_MODULES, 'i');
}
return /node_modules/i;
}

// ----------------------------------------
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-loader-exclude-node-modules-except",
"version": "1.0.1",
"version": "1.0.2",
"description": "Creating a regular expression for excluding node_modules from babel transpiling except for individual modules",
"main": "index.js",
"scripts": {
Expand All @@ -22,5 +22,8 @@
"bugs": {
"url": "https://github.com/WezomAgency/babel-loader-exclude-node-modules-except/issues"
},
"homepage": "https://github.com/WezomAgency/babel-loader-exclude-node-modules-except#readme"
"homepage": "https://github.com/WezomAgency/babel-loader-exclude-node-modules-except#readme",
"dependencies": {
"escape-string-regexp": "^2.0.0"
}
}

0 comments on commit 68fd811

Please sign in to comment.