Skip to content

Commit

Permalink
fix(babel-plugin): handle special chars in file names (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Mar 19, 2019
1 parent 3767f28 commit 4da39ff
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 30 deletions.
39 changes: 39 additions & 0 deletions packages/babel-plugin/src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,45 @@ exports[`plugin simple import should transform path into "chunk-friendly" name 1
});"
`;
exports[`plugin simple import should work with custom name 1`] = `
"loadable({
chunkName() {
return \`foo\`.replace(/[^a-zA-Z0-9_$()=\\\\-^°]+/g, \\"-\\");
},
isReady(props) {
if (typeof __webpack_modules__ !== 'undefined') {
return !!__webpack_modules__[this.resolve(props)];
}
return false;
},
requireAsync: () => import(
/* webpackChunkName: \\"foo\\" */
\`./foo*\`),
requireSync(props) {
const id = this.resolve(props);
if (typeof __webpack_require__ !== 'undefined') {
return __webpack_require__(id);
}
return eval('module.require')(id);
},
resolve() {
if (require.resolveWeak) {
return require.resolveWeak(\`./foo*\`);
}
return require('path').resolve(__dirname, \`./foo*\`);
}
});"
`;
exports[`plugin simple import should work with template literal 1`] = `
"loadable({
chunkName() {
Expand Down
8 changes: 8 additions & 0 deletions packages/babel-plugin/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ describe('plugin', () => {
expect(result).toMatchSnapshot()
})

it('should work with custom name', () => {
const result = testPlugin(`
loadable(() => import(\`./foo*\`))
`)

expect(result).toMatchSnapshot()
})

it('should transform path into "chunk-friendly" name', () => {
const result = testPlugin(`
loadable(() => import('../foo/bar'))
Expand Down
62 changes: 32 additions & 30 deletions packages/babel-plugin/src/properties/chunkName.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import vm from 'vm'
import { getImportArg } from '../util'

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

function readWebpackCommentValues(str) {
try {
Expand Down Expand Up @@ -41,29 +44,30 @@ function getRawChunkNameFromCommments(importArg) {
}

function moduleToChunk(str) {
return str ? str.replace(/^[./]+|(\.js$)/g, '').replace(WEBPACK_PATH_NAME_NORMALIZE_REPLACE_REGEX, '-') : ''
if (typeof str !== 'string') return ''
return str
.replace(JS_PATH_REGEXP, '')
.replace(WEBPACK_PATH_NAME_NORMALIZE_REPLACE_REGEX, '-')
.replace(WEBPACK_MATCH_PADDED_HYPHENS_REPLACE_REGEX, '')
}

function replaceQuasiValue(str) {
return str ? str.replace(WEBPACK_PATH_NAME_NORMALIZE_REPLACE_REGEX, '-') : str
function replaceQuasi(str, stripLeftHyphen) {
if (!str) return ''
const result = str.replace(WEBPACK_PATH_NAME_NORMALIZE_REPLACE_REGEX, '-')
if (!stripLeftHyphen) return result
return result.replace(MATCH_LEFT_HYPHENS_REPLACE_REGEX, '')
}

export default function chunkNameProperty({ types: t }) {
function transformQuasi(quasi, index) {
if (index === 0) {
return t.templateElement(
{
raw: moduleToChunk(quasi.value.raw),
cooked: moduleToChunk(quasi.value.cooked),
},
quasi.tail,
)
}

function transformQuasi(quasi, first, single) {
return t.templateElement(
{
raw: replaceQuasiValue(quasi.value.raw),
cooked: replaceQuasiValue(quasi.value.cooked),
raw: single
? moduleToChunk(quasi.value.raw)
: replaceQuasi(quasi.value.raw, first),
cooked: single
? moduleToChunk(quasi.value.cooked)
: replaceQuasi(quasi.value.cooked, first),
},
quasi.tail,
)
Expand All @@ -73,7 +77,13 @@ export default function chunkNameProperty({ types: t }) {
const importArg = getImportArg(callPath)
if (importArg.isTemplateLiteral()) {
return t.templateLiteral(
importArg.node.quasis.map(transformQuasi),
importArg.node.quasis.map((quasi, index) =>
transformQuasi(
quasi,
index === 0,
importArg.node.quasis.length === 1,
),
),
importArg.node.expressions,
)
}
Expand Down Expand Up @@ -112,16 +122,10 @@ export default function chunkNameProperty({ types: t }) {
}

function sanitizeChunkNameTemplateLiteral(node) {
return t.callExpression(
t.memberExpression(
node,
t.identifier('replace')
),
[
t.regExpLiteral(WEBPACK_PATH_NAME_NORMALIZE_REPLACE_REGEX.source, 'g'),
t.stringLiteral('-')
]
)
return t.callExpression(t.memberExpression(node, t.identifier('replace')), [
t.regExpLiteral(WEBPACK_PATH_NAME_NORMALIZE_REPLACE_REGEX.source, 'g'),
t.stringLiteral('-'),
])
}

function replaceChunkName(callPath) {
Expand All @@ -147,8 +151,6 @@ export default function chunkNameProperty({ types: t }) {
return chunkNameNode
}



return ({ callPath, funcPath }) => {
const chunkName = replaceChunkName(callPath)

Expand Down

0 comments on commit 4da39ff

Please sign in to comment.