diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-08-14 05:01:11 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-08-14 05:02:09 +0200 |
commit | 363723fc84f7b8477592e0105aeb331ec9a017af (patch) | |
tree | 29f92724f34131bac64d6a318dd7e30612e631c7 /node_modules/webpack/lib/RequestShortener.js | |
parent | 5634e77ad96bfe1818f6b6ee70b7379652e5487f (diff) |
node_modules
Diffstat (limited to 'node_modules/webpack/lib/RequestShortener.js')
-rw-r--r-- | node_modules/webpack/lib/RequestShortener.js | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/node_modules/webpack/lib/RequestShortener.js b/node_modules/webpack/lib/RequestShortener.js index 369e44334..0dd27a7d2 100644 --- a/node_modules/webpack/lib/RequestShortener.js +++ b/node_modules/webpack/lib/RequestShortener.js @@ -5,40 +5,48 @@ "use strict";
const path = require("path");
+const NORMALIZE_SLASH_DIRECTION_REGEXP = /\\/g;
+const PATH_CHARS_REGEXP = /[-[\]{}()*+?.,\\^$|#\s]/g;
+const SEPARATOR_REGEXP = /[/\\]$/;
+const FRONT_OR_BACK_BANG_REGEXP = /^!|!$/g;
+const INDEX_JS_REGEXP = /\/index.js(!|\?|\(query\))/g;
+
+const normalizeBackSlashDirection = (request) => {
+ return request.replace(NORMALIZE_SLASH_DIRECTION_REGEXP, "/");
+};
+
+const createRegExpForPath = (path) => {
+ const regexpTypePartial = path.replace(PATH_CHARS_REGEXP, "\\$&");
+ return new RegExp(`(^|!)${regexpTypePartial}`, "g");
+};
class RequestShortener {
constructor(directory) {
- directory = directory.replace(/\\/g, "/");
- if(/[\/\\]$/.test(directory)) directory = directory.substr(0, directory.length - 1);
+ directory = normalizeBackSlashDirection(directory);
+ if(SEPARATOR_REGEXP.test(directory)) directory = directory.substr(0, directory.length - 1);
if(directory) {
- const currentDirectoryRegExpString = directory.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
- this.currentDirectoryRegExp = new RegExp("^" + currentDirectoryRegExpString + "|(!)" + currentDirectoryRegExpString, "g");
+ this.currentDirectoryRegExp = createRegExpForPath(directory);
}
const dirname = path.dirname(directory);
- const endsWithSeperator = /[\/\\]$/.test(dirname);
+ const endsWithSeperator = SEPARATOR_REGEXP.test(dirname);
const parentDirectory = endsWithSeperator ? dirname.substr(0, dirname.length - 1) : dirname;
if(parentDirectory && parentDirectory !== directory) {
- const parentDirectoryRegExpString = parentDirectory.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
- this.parentDirectoryRegExp = new RegExp("^" + parentDirectoryRegExpString + "|(!)" + parentDirectoryRegExpString, "g");
+ this.parentDirectoryRegExp = createRegExpForPath(parentDirectory);
}
if(__dirname.length >= 2) {
- const buildins = path.join(__dirname, "..").replace(/\\/g, "/");
+ const buildins = normalizeBackSlashDirection(path.join(__dirname, ".."));
const buildinsAsModule = this.currentDirectoryRegExp && this.currentDirectoryRegExp.test(buildins);
this.buildinsAsModule = buildinsAsModule;
- const buildinsRegExpString = buildins.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
- this.buildinsRegExp = new RegExp("^" + buildinsRegExpString + "|(!)" + buildinsRegExpString, "g");
+ this.buildinsRegExp = createRegExpForPath(buildins);
}
-
- this.nodeModulesRegExp = /\/node_modules\//g;
- this.indexJsRegExp = /\/index.js(!|\?|\(query\))/g;
}
shorten(request) {
if(!request) return request;
- request = request.replace(/\\/g, "/");
+ request = normalizeBackSlashDirection(request);
if(this.buildinsAsModule && this.buildinsRegExp)
request = request.replace(this.buildinsRegExp, "!(webpack)");
if(this.currentDirectoryRegExp)
@@ -47,9 +55,8 @@ class RequestShortener { request = request.replace(this.parentDirectoryRegExp, "!..");
if(!this.buildinsAsModule && this.buildinsRegExp)
request = request.replace(this.buildinsRegExp, "!(webpack)");
- request = request.replace(this.nodeModulesRegExp, "/~/");
- request = request.replace(this.indexJsRegExp, "$1");
- return request.replace(/^!|!$/, "");
+ request = request.replace(INDEX_JS_REGEXP, "$1");
+ return request.replace(FRONT_OR_BACK_BANG_REGEXP, "");
}
}
|