From d98dd2778a54bbd1918f9532abc01bc6375559d8 Mon Sep 17 00:00:00 2001 From: Matt Browne Date: Thu, 19 Sep 2019 02:42:42 -0400 Subject: [PATCH] fix(babel-plugin): fix bug when using + concatenation instead of a template literal (#425) --- .../src/__snapshots__/index.test.js.snap | 39 +++++++++++++++++++ packages/babel-plugin/src/index.test.js | 8 ++++ .../babel-plugin/src/properties/resolve.js | 7 ++++ 3 files changed, 54 insertions(+) diff --git a/packages/babel-plugin/src/__snapshots__/index.test.js.snap b/packages/babel-plugin/src/__snapshots__/index.test.js.snap index 9da516a9e..d094b5ed7 100644 --- a/packages/babel-plugin/src/__snapshots__/index.test.js.snap +++ b/packages/babel-plugin/src/__snapshots__/index.test.js.snap @@ -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() { diff --git a/packages/babel-plugin/src/index.test.js b/packages/babel-plugin/src/index.test.js index 28c26caa4..1bab8750e 100644 --- a/packages/babel-plugin/src/index.test.js +++ b/packages/babel-plugin/src/index.test.js @@ -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*\`)) diff --git a/packages/babel-plugin/src/properties/resolve.js b/packages/babel-plugin/src/properties/resolve.js index c18a5a645..8c952d0df 100644 --- a/packages/babel-plugin/src/properties/resolve.js +++ b/packages/babel-plugin/src/properties/resolve.js @@ -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) }