Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: let async imports bundled into separate chunks #940

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions crates/mako/src/plugins/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
));
}
Expand Down Expand Up @@ -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,
};
Expand All @@ -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,
Expand Down
24 changes: 21 additions & 3 deletions e2e/fixtures/javascript.require-dynamic/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);

Expand Down
3 changes: 3 additions & 0 deletions e2e/fixtures/javascript.require-dynamic/src/i18n/en-US.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"lang": "English"
}
4 changes: 3 additions & 1 deletion e2e/fixtures/javascript.require-dynamic/src/i18n/zh-CN.json
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{}
{
"lang": "中文"
}
Loading