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: chunk runtime tpl cannot compatible with legacy devices #1227

Merged
merged 3 commits into from
May 31, 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
2 changes: 1 addition & 1 deletion crates/mako/src/generate/chunk_pot/str_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub(super) fn render_normal_js_chunk(
let (content_buf, source_map_buf) = {
let pot = chunk_pot;
let chunk_prefix_code = format!(
r#"(globalThis['{}'] = globalThis['{}'] || []).push([
r#"((typeof globalThis !== 'undefined' ? globalThis : self)['{}'] = (typeof globalThis !== 'undefined' ? globalThis : self)['{}'] || []).push([
['{}'],"#,
context.config.output.chunk_loading_global,
context.config.output.chunk_loading_global,
Expand Down
23 changes: 18 additions & 5 deletions crates/mako/src/generate/chunk_pot/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use std::sync::Arc;
use mako_core::anyhow::{anyhow, Result};
use mako_core::md5;
use mako_core::sailfish::TemplateOnce;
use mako_core::swc_atoms::js_word;
use mako_core::swc_common::DUMMY_SP;
use mako_core::swc_ecma_ast::{
ArrayLit, AssignOp, BinaryOp, BlockStmt, Expr, ExprOrSpread, FnExpr, Function, Ident,
KeyValueProp, Module as SwcModule, ObjectLit, Prop, PropOrSpread,
ArrayLit, AssignOp, BinaryOp, BlockStmt, CondExpr, Expr, ExprOrSpread, FnExpr, Function, Ident,
KeyValueProp, Module as SwcModule, ObjectLit, Prop, PropOrSpread, UnaryExpr, UnaryOp,
};
use mako_core::swc_ecma_codegen::text_writer::JsWriter;
use mako_core::swc_ecma_codegen::{Config as JsCodegenConfig, Emitter};
Expand Down Expand Up @@ -210,9 +211,21 @@ pub(crate) fn pot_to_chunk_module(

let module_object = pot_to_module_object(pot, context)?;

// (globalThis['makoChunk_global'] = globalThis['makoChunk_global'] || []).push([["module_id"], { module object }])
let chunk_global_expr =
quote_ident!("globalThis").computed_member::<Expr>(global.clone().into());
// ((typeof globalThis !== 'undefined' ? globalThis : self)['makoChunk_global'] = (typeof globalThis !== 'undefined' ? globalThis : self)['makoChunk_global'] || []).push([["module_id"], { module object }])
let chunk_global_expr = CondExpr {
span: DUMMY_SP,
test: UnaryExpr {
span: DUMMY_SP,
op: UnaryOp::TypeOf,
arg: quote_ident!("globalThis").into(),
}
.make_bin::<Expr>(BinaryOp::NotEqEq, js_word!("undefined").into())
.into(),
cons: quote_ident!("globalThis").into(),
alt: quote_ident!("self").into(),
}
.wrap_with_paren()
.computed_member::<Expr>(global.clone().into());
let chunk_global_obj = chunk_global_expr
.clone()
.make_bin::<Expr>(
Expand Down
2 changes: 1 addition & 1 deletion e2e/fixtures/generate.chunks-hash-replace/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ assert(
);

assert(
entryDistContent.includes('"src/lazy.ts":"src_lazy_ts-async.0c40bab1.js"'),
entryDistContent.includes('"src/lazy.ts":"src_lazy_ts-async.d869db5c.js"'),
"should correctly replace async js chunk hash"
);

Expand Down
Loading