Skip to content

Commit 7e757cd

Browse files
authored
Merge pull request webpack#5194 from webpack/feature/hoist_regex_literals
feat(perf): Hoist RegExp literals in RequestShortener
2 parents 2879fb3 + 70b7d2f commit 7e757cd

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

lib/RequestShortener.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,48 @@
55
"use strict";
66

77
const path = require("path");
8+
const NORMALIZE_SLASH_DIRECTION_REGEXP = /\\/g;
9+
const PATH_CHARS_REGEXP = /[-[\]{}()*+?.,\\^$|#\s]/g;
10+
const SEPARATOR_REGEXP = /[/\\]$/;
11+
const FRONT_OR_BACK_BANG_REGEXP = /^!|!$/g;
12+
const INDEX_JS_REGEXP = /\/index.js(!|\?|\(query\))/g;
13+
14+
const normalizeBackSlashDirection = (request) => {
15+
return request.replace(NORMALIZE_SLASH_DIRECTION_REGEXP, "/");
16+
};
17+
18+
const createRegExpForPath = (path) => {
19+
const regexpTypePartial = path.replace(PATH_CHARS_REGEXP, "\\$&");
20+
return new RegExp(`(^|!)${regexpTypePartial}`, "g");
21+
};
822

923
class RequestShortener {
1024
constructor(directory) {
11-
directory = directory.replace(/\\/g, "/");
12-
if(/[\/\\]$/.test(directory)) directory = directory.substr(0, directory.length - 1);
25+
directory = normalizeBackSlashDirection(directory);
26+
if(SEPARATOR_REGEXP.test(directory)) directory = directory.substr(0, directory.length - 1);
1327

1428
if(directory) {
15-
const currentDirectoryRegExpString = directory.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
16-
this.currentDirectoryRegExp = new RegExp("^" + currentDirectoryRegExpString + "|(!)" + currentDirectoryRegExpString, "g");
29+
this.currentDirectoryRegExp = createRegExpForPath(directory);
1730
}
1831

1932
const dirname = path.dirname(directory);
20-
const endsWithSeperator = /[\/\\]$/.test(dirname);
33+
const endsWithSeperator = SEPARATOR_REGEXP.test(dirname);
2134
const parentDirectory = endsWithSeperator ? dirname.substr(0, dirname.length - 1) : dirname;
2235
if(parentDirectory && parentDirectory !== directory) {
23-
const parentDirectoryRegExpString = parentDirectory.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
24-
this.parentDirectoryRegExp = new RegExp("^" + parentDirectoryRegExpString + "|(!)" + parentDirectoryRegExpString, "g");
36+
this.parentDirectoryRegExp = createRegExpForPath(parentDirectory);
2537
}
2638

2739
if(__dirname.length >= 2) {
28-
const buildins = path.join(__dirname, "..").replace(/\\/g, "/");
40+
const buildins = normalizeBackSlashDirection(path.join(__dirname, ".."));
2941
const buildinsAsModule = this.currentDirectoryRegExp && this.currentDirectoryRegExp.test(buildins);
3042
this.buildinsAsModule = buildinsAsModule;
31-
const buildinsRegExpString = buildins.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
32-
this.buildinsRegExp = new RegExp("^" + buildinsRegExpString + "|(!)" + buildinsRegExpString, "g");
43+
this.buildinsRegExp = createRegExpForPath(buildins);
3344
}
34-
35-
this.indexJsRegExp = /\/index.js(!|\?|\(query\))/g;
3645
}
3746

3847
shorten(request) {
3948
if(!request) return request;
40-
request = request.replace(/\\/g, "/");
49+
request = normalizeBackSlashDirection(request);
4150
if(this.buildinsAsModule && this.buildinsRegExp)
4251
request = request.replace(this.buildinsRegExp, "!(webpack)");
4352
if(this.currentDirectoryRegExp)
@@ -46,8 +55,8 @@ class RequestShortener {
4655
request = request.replace(this.parentDirectoryRegExp, "!..");
4756
if(!this.buildinsAsModule && this.buildinsRegExp)
4857
request = request.replace(this.buildinsRegExp, "!(webpack)");
49-
request = request.replace(this.indexJsRegExp, "$1");
50-
return request.replace(/^!|!$/, "");
58+
request = request.replace(INDEX_JS_REGEXP, "$1");
59+
return request.replace(FRONT_OR_BACK_BANG_REGEXP, "");
5160
}
5261
}
5362

0 commit comments

Comments
 (0)