-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support dynamic import with template string (#1405)
- Loading branch information
Showing
7 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
crates/mako/src/visitors/import_template_to_string_literal.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
e2e/fixtures/javascript.dynamic-import.template-string/expect.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('foo'); |
1 change: 1 addition & 0 deletions
1
e2e/fixtures/javascript.dynamic-import.template-string/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import(`./foo`) |
11 changes: 11 additions & 0 deletions
11
e2e/fixtures/javascript.dynamic-import.template-string/mako.config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |