@@ -2,6 +2,8 @@ import vm from 'vm'
2
2
import { getImportArg } from '../util'
3
3
4
4
const WEBPACK_CHUNK_NAME_REGEXP = / w e b p a c k C h u n k N a m e /
5
+ // https://github.com/webpack/webpack/blob/master/lib/Template.js
6
+ const WEBPACK_PATH_NAME_NORMALIZE_REPLACE_REGEX = / [ ^ a - z A - Z 0 - 9 _ ! § $ ( ) = \- ^ ° ] + / g
5
7
6
8
function readWebpackCommentValues ( str ) {
7
9
try {
@@ -39,11 +41,11 @@ function getRawChunkNameFromCommments(importArg) {
39
41
}
40
42
41
43
function moduleToChunk ( str ) {
42
- return str ? str . replace ( / ^ [ . / ] + | ( \. j s $ ) / g, '' ) . replace ( / \/ / , '-' ) : ''
44
+ return str ? str . replace ( / ^ [ . / ] + | ( \. j s $ ) / g, '' ) . replace ( WEBPACK_PATH_NAME_NORMALIZE_REPLACE_REGEX , '-' ) : ''
43
45
}
44
46
45
47
function replaceQuasiValue ( str ) {
46
- return str ? str . replace ( / \/ / g , '-' ) : str
48
+ return str ? str . replace ( WEBPACK_PATH_NAME_NORMALIZE_REPLACE_REGEX , '-' ) : str
47
49
}
48
50
49
51
export default function chunkNameProperty ( { types : t } ) {
@@ -109,6 +111,19 @@ export default function chunkNameProperty({ types: t }) {
109
111
return `${ v1 } [request]${ v2 } `
110
112
}
111
113
114
+ function sanitizeChunkNameTemplateLiteral ( node ) {
115
+ return t . callExpression (
116
+ t . memberExpression (
117
+ node ,
118
+ t . identifier ( 'replace' )
119
+ ) ,
120
+ [
121
+ t . regExpLiteral ( WEBPACK_PATH_NAME_NORMALIZE_REPLACE_REGEX . source , 'g' ) ,
122
+ t . stringLiteral ( '-' )
123
+ ]
124
+ )
125
+ }
126
+
112
127
function replaceChunkName ( callPath ) {
113
128
const agressiveImport = isAgressiveImport ( callPath )
114
129
const values = getExistingChunkNameComment ( callPath )
@@ -118,14 +133,22 @@ export default function chunkNameProperty({ types: t }) {
118
133
return t . stringLiteral ( values . webpackChunkName )
119
134
}
120
135
121
- const chunkNameNode = generateChunkNameNode ( callPath )
122
- const webpackChunkName = t . isTemplateLiteral ( chunkNameNode )
123
- ? chunkNameFromTemplateLiteral ( chunkNameNode )
124
- : chunkNameNode . value
136
+ let chunkNameNode = generateChunkNameNode ( callPath )
137
+ let webpackChunkName
138
+
139
+ if ( t . isTemplateLiteral ( chunkNameNode ) ) {
140
+ webpackChunkName = chunkNameFromTemplateLiteral ( chunkNameNode )
141
+ chunkNameNode = sanitizeChunkNameTemplateLiteral ( chunkNameNode )
142
+ } else {
143
+ webpackChunkName = chunkNameNode . value
144
+ }
145
+
125
146
addOrReplaceChunkNameComment ( callPath , { webpackChunkName } )
126
147
return chunkNameNode
127
148
}
128
149
150
+
151
+
129
152
return ( { callPath, funcPath } ) => {
130
153
const chunkName = replaceChunkName ( callPath )
131
154
0 commit comments