Skip to content

Commit

Permalink
fix: support dynamic import with template string (#1405)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc authored Jul 12, 2024
1 parent ce4b647 commit d11e50f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crates/mako/src/build/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::visitors::dynamic_import_to_require::DynamicImportToRequire;
use crate::visitors::env_replacer::{build_env_map, EnvReplacer};
use crate::visitors::fix_helper_inject_position::FixHelperInjectPosition;
use crate::visitors::fix_symbol_conflict::FixSymbolConflict;
use crate::visitors::import_template_to_string_literal::ImportTemplateToStringLiteral;
use crate::visitors::new_url_assets::NewUrlAssets;
use crate::visitors::provide::Provide;
use crate::visitors::react::react;
Expand Down Expand Up @@ -141,6 +142,7 @@ impl Transform {
}));
// TODO: move ContextModuleVisitor out of plugin
visitors.push(Box::new(ContextModuleVisitor { unresolved_mark }));
visitors.push(Box::new(ImportTemplateToStringLiteral {}));
// DynamicImportToRequire must be after ContextModuleVisitor
// since ContextModuleVisitor will add extra dynamic imports
if context.config.dynamic_import_to_require {
Expand Down
40 changes: 40 additions & 0 deletions crates/mako/src/visitors/import_template_to_string_literal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use swc_core::ecma::ast::{CallExpr, Callee, Expr, Lit};
use swc_core::ecma::visit::VisitMut;

pub struct ImportTemplateToStringLiteral {}

impl VisitMut for ImportTemplateToStringLiteral {
fn visit_mut_call_expr(&mut self, n: &mut CallExpr) {
if matches!(n.callee, Callee::Import(_)) && n.args.len() == 1 {
if let box Expr::Tpl(tpl) = &n.args[0].expr {
if tpl.exprs.is_empty() && tpl.quasis.len() == 1 {
let s: String = tpl.quasis[0].raw.clone().to_string();
n.args[0].expr = Expr::Lit(Lit::Str(s.into())).into();
}
}
}
}
}

#[cfg(test)]
mod tests {
use swc_core::common::GLOBALS;
use swc_core::ecma::visit::VisitMutWith;

use crate::ast::tests::TestUtils;

#[test]
fn test_normal() {
assert_eq!(run(r#"import(`a`)"#), r#"import("a");"#);
}

fn run(js_code: &str) -> String {
let mut test_utils = TestUtils::gen_js_ast(js_code);
let ast = test_utils.ast.js_mut();
GLOBALS.set(&test_utils.context.meta.script.globals, || {
let mut visitor = super::ImportTemplateToStringLiteral {};
ast.ast.visit_mut_with(&mut visitor);
});
test_utils.js_ast_to_code()
}
}
1 change: 1 addition & 0 deletions crates/mako/src/visitors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub(crate) mod dynamic_import_to_require;
pub(crate) mod env_replacer;
pub(crate) mod fix_helper_inject_position;
pub(crate) mod fix_symbol_conflict;
pub(crate) mod import_template_to_string_literal;
pub(crate) mod mako_require;
pub(crate) mod meta_url_replacer;
pub(crate) mod new_url_assets;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const {
parseBuildResult,
} = require("../../../scripts/test-utils");
const assert = require("assert");
const { files } = parseBuildResult(__dirname);
const content = files["index.js"];

assert(content.includes(`"foo.js": "foo_js-async.js"`), "template string replace works");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('foo');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import(`./foo`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"entry": {
"index": "./index.js"
},
"optimization": {
"skipModules": false,
"concatenateModules": false
},
"platform": "node",
"chunkLoadingGlobal": "dynamic_import_async_module"
}

0 comments on commit d11e50f

Please sign in to comment.