55"use strict" ;
66
77const 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 = / \/ i n d e x .j s ( ! | \? | \( q u e r y \) ) / 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
923class 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 = / \/ i n d e x .j s ( ! | \? | \( q u e r y \) ) / 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