Skip to content

Commit

Permalink
fix(babel-plugin): fix bug when using + concatenation instead of a te…
Browse files Browse the repository at this point in the history
…mplate literal (#425)
  • Loading branch information
mbrowne authored and gregberge committed Sep 19, 2019
1 parent d4428c6 commit d98dd27
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 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 @@ -527,6 +527,45 @@ exports[`plugin simple import should work with * in name 1`] = `
});"
`;
exports[`plugin simple import should work with + concatenation 1`] = `
"loadable({
chunkName() {
return \\"\\";
},
isReady(props) {
if (typeof __webpack_modules__ !== 'undefined') {
return !!__webpack_modules__[this.resolve(props)];
}
return false;
},
requireAsync: () => import(
/* webpackChunkName: \\"\\" */
'./Mod' + 'A'),
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('./Mod' + 'A');
}
return eval('require.resolve')('./Mod' + 'A');
}
});"
`;
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 + concatenation', () => {
const result = testPlugin(`
loadable(() => import('./Mod' + 'A'))
`)

expect(result).toMatchSnapshot()
})

it('should work with * in name', () => {
const result = testPlugin(`
loadable(() => import(\`./foo*\`))
Expand Down
7 changes: 7 additions & 0 deletions packages/babel-plugin/src/properties/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export default function resolveProperty({ types: t, template }) {
importArg.node.expressions,
)
}
if (importArg.isBinaryExpression()) {
return t.BinaryExpression(
importArg.node.operator,
importArg.node.left,
importArg.node.right,
)
}
return t.stringLiteral(importArg.node.value)
}

Expand Down

0 comments on commit d98dd27

Please sign in to comment.