@@ -10,6 +10,12 @@ const ConcatSource = require("webpack-sources").ConcatSource;
1010const START_LOWERCASE_ALPHABET_CODE = "a" . charCodeAt ( 0 ) ;
1111const START_UPPERCASE_ALPHABET_CODE = "A" . charCodeAt ( 0 ) ;
1212const DELTA_A_TO_Z = "z" . charCodeAt ( 0 ) - START_LOWERCASE_ALPHABET_CODE + 1 ;
13+ const FUNCTION_CONTENT_REGEX = / ^ f u n c t i o n \s ? \( \) \s ? \{ \n ? | \n ? \} $ / g;
14+ const INDENT_MULTILINE_REGEX = / ^ \t / mg;
15+ const IDENTIFIER_NAME_REPLACE_REGEX = / ^ [ ^ a - z A - Z $ _ ] / ;
16+ const IDENTIFIER_ALPHA_NUMERIC_NAME_REPLACE_REGEX = / [ ^ a - z A - Z 0 - 9 $ _ ] / g;
17+ const PATH_NAME_NORMALIZE_REPLACE_REGEX = / [ ^ a - z A - Z 0 - 9 _ ! § $ ( ) = \- ^ ° ] + / g;
18+ const MATCH_PADDED_HYPHENS_REPLACE_REGEX = / ^ - | - $ / g;
1319
1420module . exports = class Template extends Tapable {
1521 constructor ( outputOptions ) {
@@ -18,17 +24,17 @@ module.exports = class Template extends Tapable {
1824 }
1925
2026 static getFunctionContent ( fn ) {
21- return fn . toString ( ) . replace ( / ^ f u n c t i o n \s ? \( \) \s ? \{ \n ? | \n ? \} $ / g , "" ) . replace ( / ^ \t / mg , "" ) ;
27+ return fn . toString ( ) . replace ( FUNCTION_CONTENT_REGEX , "" ) . replace ( INDENT_MULTILINE_REGEX , "" ) ;
2228 }
2329
2430 static toIdentifier ( str ) {
2531 if ( typeof str !== "string" ) return "" ;
26- return str . replace ( / ^ [ ^ a - z A - Z $ _ ] / , "_" ) . replace ( / [ ^ a - z A - Z 0 - 9 $ _ ] / g , "_" ) ;
32+ return str . replace ( IDENTIFIER_NAME_REPLACE_REGEX , "_" ) . replace ( IDENTIFIER_ALPHA_NUMERIC_NAME_REPLACE_REGEX , "_" ) ;
2733 }
2834
2935 static toPath ( str ) {
3036 if ( typeof str !== "string" ) return "" ;
31- return str . replace ( / [ ^ a - z A - Z 0 - 9 _ ! § $ ( ) = \- ^ ° ] + / g , "-" ) . replace ( / ^ - | - $ / , "" ) ;
37+ return str . replace ( PATH_NAME_NORMALIZE_REPLACE_REGEX , "-" ) . replace ( MATCH_PADDED_HYPHENS_REPLACE_REGEX , "" ) ;
3238 }
3339
3440 // map number to a single character a-z, A-Z or <_ + number> if number is too big
@@ -144,23 +150,27 @@ module.exports = class Template extends Tapable {
144150 } else {
145151 // Render an object
146152 source . add ( "{\n" ) ;
147- allModules . sort ( function ( a , b ) {
148- var aId = a . id + "" ;
149- var bId = b . id + "" ;
150- if ( aId < bId ) return - 1 ;
151- if ( aId > bId ) return 1 ;
152- return 0 ;
153- } ) . forEach ( function ( module , idx ) {
154- if ( idx !== 0 ) source . add ( ",\n" ) ;
155- source . add ( "\n/***/ " + JSON . stringify ( module . id ) + ":\n" ) ;
156- source . add ( module . source ) ;
157- } ) ;
153+ allModules
154+ . sort ( stringifyIdSortPredicate )
155+ . forEach ( function ( module , idx ) {
156+ if ( idx !== 0 ) source . add ( ",\n" ) ;
157+ source . add ( `\n/***/ ${ JSON . stringify ( module . id ) } :\n` ) ;
158+ source . add ( module . source ) ;
159+ } ) ;
158160 source . add ( "\n\n" + prefix + "}" ) ;
159161 }
160162 return source ;
161163 }
162164} ;
163165
166+ function stringifyIdSortPredicate ( a , b ) {
167+ var aId = a . id + "" ;
168+ var bId = b . id + "" ;
169+ if ( aId < bId ) return - 1 ;
170+ if ( aId > bId ) return 1 ;
171+ return 0 ;
172+ }
173+
164174function moduleIdIsNumber ( module ) {
165175 return typeof module . id === "number" ;
166176}
0 commit comments