Skip to content

Commit ef11e11

Browse files
marcmrfgregberge
authored andcommitted
fix(server): fix chunkName resolving (#219)
1 parent cfab31f commit ef11e11

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

packages/babel-plugin/src/__snapshots__/index.test.js.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`plugin aggressive import should work with destructuration 1`] = `
55
chunkName({
66
foo
77
}) {
8-
return \`\${foo}\`;
8+
return \`\${foo}\`.replace(/[^a-zA-Z0-9_!§$()=\\\\-^°]+/g, \\"-\\");
99
},
1010
1111
isReady(props) {
@@ -48,7 +48,7 @@ exports[`plugin aggressive import should work with destructuration 1`] = `
4848
exports[`plugin aggressive import with "webpackChunkName" should replace it 1`] = `
4949
"loadable({
5050
chunkName(props) {
51-
return \`\${props.foo}\`;
51+
return \`\${props.foo}\`.replace(/[^a-zA-Z0-9_!§$()=\\\\-^°]+/g, \\"-\\");
5252
},
5353
5454
isReady(props) {
@@ -87,7 +87,7 @@ exports[`plugin aggressive import with "webpackChunkName" should replace it 1`]
8787
exports[`plugin aggressive import without "webpackChunkName" should support complex request 1`] = `
8888
"loadable({
8989
chunkName(props) {
90-
return \`dir-\${props.foo}-test\`;
90+
return \`dir-\${props.foo}-test\`.replace(/[^a-zA-Z0-9_!§$()=\\\\-^°]+/g, \\"-\\");
9191
},
9292
9393
isReady(props) {
@@ -128,7 +128,7 @@ exports[`plugin aggressive import without "webpackChunkName" should support dest
128128
chunkName({
129129
foo
130130
}) {
131-
return \`dir-\${foo}-test\`;
131+
return \`dir-\${foo}-test\`.replace(/[^a-zA-Z0-9_!§$()=\\\\-^°]+/g, \\"-\\");
132132
},
133133
134134
isReady(props) {
@@ -171,7 +171,7 @@ exports[`plugin aggressive import without "webpackChunkName" should support dest
171171
exports[`plugin aggressive import without "webpackChunkName" should support simple request 1`] = `
172172
"loadable({
173173
chunkName(props) {
174-
return \`\${props.foo}\`;
174+
return \`\${props.foo}\`.replace(/[^a-zA-Z0-9_!§$()=\\\\-^°]+/g, \\"-\\");
175175
},
176176
177177
isReady(props) {
@@ -327,7 +327,7 @@ exports[`plugin simple import should transform path into "chunk-friendly" name 1
327327
exports[`plugin simple import should work with template literal 1`] = `
328328
"loadable({
329329
chunkName() {
330-
return \`ModA\`;
330+
return \`ModA\`.replace(/[^a-zA-Z0-9_$()=\\\\-^°]+/g, \\"-\\");
331331
},
332332
333333
isReady(props) {

packages/babel-plugin/src/properties/chunkName.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import vm from 'vm'
22
import { getImportArg } from '../util'
33

44
const WEBPACK_CHUNK_NAME_REGEXP = /webpackChunkName/
5+
// https://github.com/webpack/webpack/blob/master/lib/Template.js
6+
const WEBPACK_PATH_NAME_NORMALIZE_REPLACE_REGEX = /[^a-zA-Z0-9_!§$()=\-^°]+/g
57

68
function readWebpackCommentValues(str) {
79
try {
@@ -39,11 +41,11 @@ function getRawChunkNameFromCommments(importArg) {
3941
}
4042

4143
function moduleToChunk(str) {
42-
return str ? str.replace(/^[./]+|(\.js$)/g, '').replace(/\//, '-') : ''
44+
return str ? str.replace(/^[./]+|(\.js$)/g, '').replace(WEBPACK_PATH_NAME_NORMALIZE_REPLACE_REGEX, '-') : ''
4345
}
4446

4547
function replaceQuasiValue(str) {
46-
return str ? str.replace(/\//g, '-') : str
48+
return str ? str.replace(WEBPACK_PATH_NAME_NORMALIZE_REPLACE_REGEX, '-') : str
4749
}
4850

4951
export default function chunkNameProperty({ types: t }) {
@@ -109,6 +111,19 @@ export default function chunkNameProperty({ types: t }) {
109111
return `${v1}[request]${v2}`
110112
}
111113

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+
112127
function replaceChunkName(callPath) {
113128
const agressiveImport = isAgressiveImport(callPath)
114129
const values = getExistingChunkNameComment(callPath)
@@ -118,14 +133,22 @@ export default function chunkNameProperty({ types: t }) {
118133
return t.stringLiteral(values.webpackChunkName)
119134
}
120135

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+
125146
addOrReplaceChunkNameComment(callPath, { webpackChunkName })
126147
return chunkNameNode
127148
}
128149

150+
151+
129152
return ({ callPath, funcPath }) => {
130153
const chunkName = replaceChunkName(callPath)
131154

0 commit comments

Comments
 (0)