Skip to content

Commit

Permalink
fix: add support for params in dynamic imports (#1325)
Browse files Browse the repository at this point in the history
* fix: add support for params in dynamic imports

* Create long-singers-provide.md

* Update dynamic-import.ts
  • Loading branch information
layershifter committed Aug 17, 2023
1 parent da191b5 commit b3ef8c1
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 37 deletions.
7 changes: 7 additions & 0 deletions .changeset/long-singers-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@linaria/babel-preset": patch
"@linaria/testkit": patch
"@linaria/utils": patch
---

fix: add support for params in dynamic imports
24 changes: 18 additions & 6 deletions packages/babel/src/plugins/dynamic-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,26 @@ export default function dynamicImport(babel: Core): PluginObj {
if (path.get('callee').isImport()) {
const moduleName = path.get('arguments.0') as NodePath;

if (!moduleName.isStringLiteral()) {
throw new Error('Dynamic import argument must be a string literal');
if (moduleName.isStringLiteral()) {
path.replaceWith(
t.callExpression(t.identifier('__linaria_dynamic_import'), [
t.stringLiteral(moduleName.node.value),
])
);
return;
}

path.replaceWith(
t.callExpression(t.identifier('__linaria_dynamic_import'), [
t.stringLiteral(moduleName.node.value),
])
if (moduleName.isTemplateLiteral()) {
path.replaceWith(
t.callExpression(t.identifier('__linaria_dynamic_import'), [
t.cloneNode(moduleName.node, true, true),
])
);
return;
}

throw new Error(
'Dynamic import argument must be a string or a template literal'
);
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// *

export function foo(locale, onImport) {
import(`./foo/${locale}`).then(onImport);
}
14 changes: 14 additions & 0 deletions packages/testkit/src/__snapshots__/prepareCode.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ exports[`prepareCode Testing transformation for dynamic-import: imports 1`] = `M

exports[`prepareCode Testing transformation for dynamic-import: metadata 1`] = `Object {}`;

exports[`prepareCode Testing transformation for dynamic-import-param: code 1`] = `
"\\"use strict\\";
exports.__esModule = true;
exports.foo = foo;
function foo(locale, onImport) {
__linaria_dynamic_import(\\"./foo/\\" + locale).then(onImport);
}"
`;

exports[`prepareCode Testing transformation for dynamic-import-param: imports 1`] = `Map {}`;

exports[`prepareCode Testing transformation for dynamic-import-param: metadata 1`] = `Object {}`;

exports[`prepareCode Testing transformation for for-debug: code 1`] = `
"'use strict';
Expand Down
1 change: 0 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"@babel/core": "^7.22.9",
"@babel/generator": "^7.22.9",
"@babel/plugin-proposal-export-namespace-from": "^7.18.9",
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/plugin-transform-modules-commonjs": "^7.22.5",
"@babel/template": "^7.22.5",
"@babel/traverse": "^7.22.8",
Expand Down
27 changes: 0 additions & 27 deletions packages/utils/src/options/dynamic-import-noop.ts

This file was deleted.

5 changes: 2 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b3ef8c1

Please sign in to comment.