Skip to content

Commit

Permalink
Revert "fix:dynamic import as namespace import (#1171)" (#1208)
Browse files Browse the repository at this point in the history
This reverts commit 5ad6f5f.
  • Loading branch information
stormslowly authored May 27, 2024
1 parent f60b28d commit 6e69d85
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 132 deletions.
4 changes: 3 additions & 1 deletion crates/mako/src/generate/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ pub fn transform_js_generate(transform_js_param: TransformJsParam) -> Result<()>
let mut meta_url_replacer = MetaUrlReplacer {};
ast.ast.visit_mut_with(&mut meta_url_replacer);

let mut dynamic_import = DynamicImport::new(context.clone(), dep_map);
let mut dynamic_import = DynamicImport {
context: context.clone(),
};
ast.ast.visit_mut_with(&mut dynamic_import);

// replace require to __mako_require__
Expand Down
4 changes: 3 additions & 1 deletion crates/mako/src/plugins/bundless_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ fn transform_js_generate(
};
ast.ast.visit_mut_with(&mut dep_replacer);

let mut dynamic_import = DynamicImport::new(context.clone(), dep_map);
let mut dynamic_import = DynamicImport {
context: context.clone(),
};
ast.ast.visit_mut_with(&mut dynamic_import);

ast.ast
Expand Down
2 changes: 1 addition & 1 deletion crates/mako/src/plugins/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl VisitMut for ContextModuleVisitor {
.as_callee();
// TODO: allow use await in args
// eg: import(`./i18n${await xxx()}`)
expr.args = vec![member_expr!(DUMMY_SP, m.default)
expr.args = vec![quote_ident!("m")
.as_call(DUMMY_SP, expr.args.clone())
.as_expr()
.to_owned()
Expand Down
100 changes: 18 additions & 82 deletions crates/mako/src/visitors/dynamic_import.rs
Original file line number Diff line number Diff line change
@@ -1,74 +1,20 @@
use std::sync::Arc;

use mako_core::swc_common::DUMMY_SP;
use mako_core::swc_ecma_ast::{ArrayLit, Expr, ExprOrSpread, Lit, MemberExpr};
use mako_core::swc_ecma_ast::{ArrayLit, Expr, ExprOrSpread, Lit};
use mako_core::swc_ecma_visit::{VisitMut, VisitMutWith};
use swc_core::ecma::ast::{Ident, Module, Stmt, VarDeclKind};
use swc_core::ecma::utils::{
member_expr, private_ident, quote_ident, quote_str, ExprFactory, IsDirective,
};

use crate::ast::utils::{is_dynamic_import, member_call, member_prop, promise_all, require_ensure};
use crate::ast::utils::{
id, is_dynamic_import, member_call, member_prop, promise_all, require_ensure,
};
use crate::compiler::Context;
use crate::generate::chunk::ChunkId;
use crate::visitors::dep_replacer::DependenciesToReplace;

pub struct DynamicImport<'a> {
pub struct DynamicImport {
pub context: Arc<Context>,
interop: Ident,
changed: bool,
dep_to_replace: &'a DependenciesToReplace,
}

impl<'a> DynamicImport<'a> {
pub fn new(context: Arc<Context>, dep_map: &'a DependenciesToReplace) -> Self {
let interop = private_ident!("interop");

Self {
context,
interop,
changed: false,
dep_to_replace: dep_map,
}
}
}

impl<'a> VisitMut for DynamicImport<'a> {
fn visit_mut_module(&mut self, n: &mut Module) {
n.visit_mut_children_with(self);

if self.changed {
let insert_at = n
.body
.iter()
.position(|module_item| {
!module_item
.as_stmt()
.map_or(false, |stmt| stmt.is_directive())
})
.unwrap();

let (id, _) = self
.dep_to_replace
.resolved
.get("@swc/helpers/_/_interop_require_wildcard")
.unwrap();

let require_interop = quote_ident!("__mako_require__")
.as_call(DUMMY_SP, vec![quote_str!(id.clone()).as_arg()]);

let stmt: Stmt = Expr::Member(MemberExpr {
span: DUMMY_SP,
obj: require_interop.into(),
prop: quote_ident!("_").into(),
})
.into_var_decl(VarDeclKind::Var, self.interop.clone().into())
.into();

n.body.insert(insert_at, stmt.into());
}
}

impl VisitMut for DynamicImport {
fn visit_mut_expr(&mut self, expr: &mut Expr) {
if let Expr::Call(call_expr) = expr {
if is_dynamic_import(call_expr) {
Expand Down Expand Up @@ -113,7 +59,6 @@ impl<'a> VisitMut for DynamicImport<'a> {
chunk_ids
};

self.changed = true;
// build new expr
// e.g.
// Promise.all([ require.ensure("id") ]).then(require.bind(require, "id"))
Expand All @@ -135,24 +80,26 @@ impl<'a> VisitMut for DynamicImport<'a> {
elems: to_ensure_elems,
})),
});

let require_call = member_expr!(DUMMY_SP, __mako_require__.dr).as_call(
DUMMY_SP,
let require_call = member_call(
Expr::Ident(id("__mako_require__")),
member_prop("bind"),
vec![
self.interop.clone().as_arg(),
ExprOrSpread {
spread: None,
expr: Box::new(Expr::Ident(id("__mako_require__"))),
},
ExprOrSpread {
spread: None,
expr: Box::new(Expr::Lit(Lit::Str(resolved_source.into()))),
},
],
);

member_call(
load_promise,
member_prop("then"),
vec![ExprOrSpread {
spread: None,
expr: require_call.into(),
expr: Box::new(require_call),
}],
)
};
Expand All @@ -165,26 +112,22 @@ impl<'a> VisitMut for DynamicImport<'a> {

#[cfg(test)]
mod tests {
use std::collections::HashMap;

use mako_core::swc_common::GLOBALS;
use mako_core::swc_ecma_visit::VisitMutWith;

use super::DynamicImport;
use crate::ast::tests::TestUtils;
use crate::generate::chunk::{Chunk, ChunkType};
use crate::visitors::dep_replacer::DependenciesToReplace;

// TODO: add nested chunk test
#[test]
fn test_dynamic_import() {
assert_eq!(
run(r#"import("foo");"#),
r#"
var interop = __mako_require__("hashed_helper")._;
Promise.all([
__mako_require__.ensure("foo")
]).then(__mako_require__.dr(interop, "foo"));
]).then(__mako_require__.bind(__mako_require__, "foo"));
"#
.trim()
);
Expand All @@ -199,17 +142,10 @@ Promise.all([
cg.add_chunk(foo);
}
let ast = test_utils.ast.js_mut();

let dep_to_replace = DependenciesToReplace {
resolved: maplit::hashmap! {
"@swc/helpers/_/_interop_require_wildcard".to_string() =>
("hashed_helper".to_string(), "dummy".into())
},
missing: HashMap::new(),
};

GLOBALS.set(&test_utils.context.meta.script.globals, || {
let mut visitor = DynamicImport::new(test_utils.context.clone(), &dep_to_replace);
let mut visitor = DynamicImport {
context: test_utils.context.clone(),
};
ast.ast.visit_mut_with(&mut visitor);
});
let code = test_utils.js_ast_to_code();
Expand Down
5 changes: 0 additions & 5 deletions crates/mako/templates/app_runtime.stpl
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ function createRuntime(makoModules, entryModuleId, global) {
});
};
requireModule.d = Object.defineProperty.bind(Object);
requireModule.dr = function(interop, request) {
return function(){
return interop(requireModule(request));
}
};

<% if has_dynamic_chunks || has_hmr { %>
/* mako/runtime/ensure chunk */
Expand Down
4 changes: 2 additions & 2 deletions e2e/fixtures/javascript.require-dynamic/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ assert.match(

assert.match(
asyncContent,
moduleReg("src/i18n\\?context&glob=\\*\\*/\\*.json&async", "'./zh-CN.json': ()=>Promise.all([\n.*__mako_require__.ensure(\"src/i18n/zh-CN.json\")\n.*]).then(__mako_require__.dr(interop, \"src/i18n/zh-CN.json\"))", true),
moduleReg("src/i18n\\?context&glob=\\*\\*/\\*.json&async", "'./zh-CN.json': ()=>Promise.all([\n.*__mako_require__.ensure(\"src/i18n/zh-CN.json\")\n.*]).then(__mako_require__.bind(__mako_require__, \"src/i18n/zh-CN.json\"))", true),
"should generate context module with correct map in async chunk",
);

Expand All @@ -49,7 +49,7 @@ assert.match(

assert.match(
asyncContent,
moduleReg("src/i18n\\?context&glob=\\*\\*/\\*.json&async", "'./en-US.json': ()=>Promise.all([\n.*__mako_require__.ensure(\"src/i18n/en-US.json\")\n.*]).then(__mako_require__.dr(interop, \"src/i18n/en-US.json\"))", true),
moduleReg("src/i18n\\?context&glob=\\*\\*/\\*.json&async", "'./en-US.json': ()=>Promise.all([\n.*__mako_require__.ensure(\"src/i18n/en-US.json\")\n.*]).then(__mako_require__.bind(__mako_require__, \"src/i18n/en-US.json\"))", true),
"should generate context module with correct map in async chunk",
);

Expand Down
8 changes: 3 additions & 5 deletions e2e/fixtures/javascript.require-dynamic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ function loadLang(lang) {

function loadLangExt(lang, ext) {
// nested dynamic require + with then callback
return import(`./i18n/${lang}.${(require(`./ext/${ext}`)).default}`)

.then(m => m);
return import(`./i18n/${lang}.${(require(`./ext/${ext}`)).default}`).then(m => m);
}

function loadFile(file) {
Expand All @@ -17,7 +15,7 @@ function loadFile2(file) {
return require('./fake.js/' + file);
}

loadLang('zh-CN').then(console.log);
loadLangExt('zh-CN', 'json').then(console.log);
console.log(loadLang('zh-CN'));
console.log(loadLangExt('zh-CN', 'json'));
console.log(loadFile('/zh-CN.json'));
console.log(loadFile2('a.js'));
16 changes: 0 additions & 16 deletions e2e/fixtures/runtime.dynamic_import_interop/expect.js

This file was deleted.

5 changes: 0 additions & 5 deletions e2e/fixtures/runtime.dynamic_import_interop/index.js

This file was deleted.

10 changes: 0 additions & 10 deletions e2e/fixtures/runtime.dynamic_import_interop/mako.config.json

This file was deleted.

4 changes: 0 additions & 4 deletions e2e/fixtures/runtime.dynamic_import_interop/src/cjs.js

This file was deleted.

4 changes: 4 additions & 0 deletions packages/mako/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

/* auto-generated by NAPI-RS */

export interface TransformOutput {
code: string;
map?: string;
}
export interface JsHooks {
load?: (filePath: string) => Promise<{ content: string; type: 'css' | 'js' }>;
generateEnd?: (data: {
Expand Down

0 comments on commit 6e69d85

Please sign in to comment.