diff --git a/crates/mako/src/plugins/context_module.rs b/crates/mako/src/plugins/context_module.rs index aa44d56db..714bcf711 100644 --- a/crates/mako/src/plugins/context_module.rs +++ b/crates/mako/src/plugins/context_module.rs @@ -33,6 +33,7 @@ impl Plugin for ContextModulePlugin { ) { let glob_pattern = param.file.pathname.clone().join(glob_pattern); let paths = glob(glob_pattern.to_str().unwrap())?; + let mut key_values = vec![]; for path in paths { @@ -74,10 +75,14 @@ impl Plugin for ContextModulePlugin { } } + let is_async = param.file.has_param("async"); + for key in keys { + let load_by = if is_async { "import" } else { "require" }; key_values.push(format!( - "'{}': () => require('{}')", + "'{}': () => {}('{}')", key, + load_by, path.to_string_lossy() )); } @@ -143,9 +148,11 @@ impl VisitMut for ContextModuleVisitor { ) .map(|(prefix, suffix)| (prefix, format!("**/*{}", suffix.unwrap_or("".to_string()),))) { - let ctxt_call_expr = CallExpr { + let args_literals = format!("{}?context&glob={}", from, glob); + + let mut ctxt_call_expr = CallExpr { callee: expr.callee.clone(), - args: vec![quote_str!(format!("{}?context&glob={}", from, glob)).as_arg()], + args: vec![quote_str!(args_literals.clone()).as_arg()], span: DUMMY_SP, type_args: None, }; @@ -154,6 +161,10 @@ impl VisitMut for ContextModuleVisitor { // require('./i18n' + n) -> require('./i18n?context&glob=**/*')('.' + n) expr.callee = ctxt_call_expr.as_callee(); } else { + // mark async import in params + ctxt_call_expr.args = + vec![quote_str!(format!("{}&{}", args_literals, "async")).as_arg()]; + // import('./i18n' + n) -> import('./i18n?context&glob=**/*').then(m => m('.' + n)) expr.callee = member_expr!( @EXT, diff --git a/e2e/fixtures/javascript.require-dynamic/expect.js b/e2e/fixtures/javascript.require-dynamic/expect.js index 688fde5d7..03a76b9ce 100644 --- a/e2e/fixtures/javascript.require-dynamic/expect.js +++ b/e2e/fixtures/javascript.require-dynamic/expect.js @@ -37,19 +37,37 @@ assert.match( assert.match( asyncContent, - moduleReg("src/i18n\\?context&glob=\\*\\*/\\*.json", "'./zh-CN.json': ()=>__mako_require__(\"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", +); + +assert.match( + asyncContent, + moduleReg("src/i18n/zh-CN.json", "中文", true), + "should generate context module with correct map in async chunk", +); + +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__.bind(__mako_require__, \"src/i18n/en-US.json\"))", true), + "should generate context module with correct map in async chunk", +); + +assert.match( + asyncContent, + moduleReg("src/i18n/en-US.json", "English", true), "should generate context module with correct map in async chunk", ); assert.match( content, - moduleReg("src/index.ts", '__mako_require__.ensure("src/i18n\\?context&glob=\\*\\*/\\*.json")', true), + moduleReg("src/index.ts", '__mako_require__.ensure("src/i18n\\?context&glob=\\*\\*/\\*.json&async")', true), "should generate async require for import dynamic module", ); assert.match( content, - moduleReg("src/index.ts", 'ensure("src/i18n\\?context&glob=\\*\\*/\\*")', true), + moduleReg("src/index.ts", 'ensure("src/i18n\\?context&glob=\\*\\*/\\*&async")', true), "should generate async require for import dynamic module with then callback", ); diff --git a/e2e/fixtures/javascript.require-dynamic/src/i18n/en-US.json b/e2e/fixtures/javascript.require-dynamic/src/i18n/en-US.json new file mode 100644 index 000000000..ab6f82ba4 --- /dev/null +++ b/e2e/fixtures/javascript.require-dynamic/src/i18n/en-US.json @@ -0,0 +1,3 @@ +{ + "lang": "English" +} diff --git a/e2e/fixtures/javascript.require-dynamic/src/i18n/zh-CN.json b/e2e/fixtures/javascript.require-dynamic/src/i18n/zh-CN.json index 0967ef424..c08dceb12 100644 --- a/e2e/fixtures/javascript.require-dynamic/src/i18n/zh-CN.json +++ b/e2e/fixtures/javascript.require-dynamic/src/i18n/zh-CN.json @@ -1 +1,3 @@ -{} +{ + "lang": "中文" +}