From f8ff46c1d7ea51fa7bf582695ac07511cd63bf9f Mon Sep 17 00:00:00 2001 From: pshu Date: Mon, 26 Aug 2024 14:21:26 +0800 Subject: [PATCH 01/11] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20ensure2=20repla?= =?UTF-8?q?ce=20in=20dynamic=20replace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/mako/src/visitors/dynamic_import.rs | 52 ++++++++++++++-------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/crates/mako/src/visitors/dynamic_import.rs b/crates/mako/src/visitors/dynamic_import.rs index 9af78bc09..b2a7a385c 100644 --- a/crates/mako/src/visitors/dynamic_import.rs +++ b/crates/mako/src/visitors/dynamic_import.rs @@ -76,6 +76,7 @@ impl<'a> VisitMut for DynamicImport<'a> { if call_expr.args.is_empty() { return; } + if let ExprOrSpread { expr: box Expr::Lit(Lit::Str(ref mut source)), .. @@ -83,7 +84,7 @@ impl<'a> VisitMut for DynamicImport<'a> { { // note: the source is replaced! let resolved_source = source.value.clone().to_string(); - let chunk_ids = { + let _chunk_ids = { let chunk_id: ChunkId = resolved_source.clone().into(); let chunk_graph = &self.context.chunk_graph.read().unwrap(); let chunk = chunk_graph.chunk(&chunk_id); @@ -119,24 +120,12 @@ impl<'a> VisitMut for DynamicImport<'a> { // e.g. // Promise.all([ require.ensure("id") ]).then(require.bind(require, "id")) // Promise.all([ require.ensure("d1"), require.ensure("id)]).then(require.bind(require, "id")) + // version 2 + // require.ensure2("id").then(require.bind(require,"id")) + *expr = { - let to_ensure_elems = chunk_ids - .iter() - .map(|c_id| { - Some(ExprOrSpread { - spread: None, - expr: Box::new(require_ensure(c_id.clone())), - }) - }) - .collect::>(); - let load_promise = promise_all(ExprOrSpread { - spread: None, - expr: ArrayLit { - span: DUMMY_SP, - elems: to_ensure_elems, - } - .into(), - }); + // let load_promise = self.make_load_promise(&chunk_ids); + let load_promise = self.make_load_promise_2(&resolved_source); let lazy_require_call = member_expr!(DUMMY_SP, __mako_require__.bind) .as_call( @@ -161,6 +150,33 @@ impl<'a> VisitMut for DynamicImport<'a> { } } +impl DynamicImport<'_> { + fn make_load_promise_2(&self, module_id: &str) -> Expr { + member_expr!(DUMMY_SP, __mako_require__.ensure2) + .as_call(DUMMY_SP, vec![quote_str!(module_id).as_arg()]) + } + + fn make_load_promise(&self, chunk_ids: &Vec) -> Expr { + let to_ensure_elems = chunk_ids + .iter() + .map(|c_id| { + Some(ExprOrSpread { + spread: None, + expr: Box::new(require_ensure(c_id.clone())), + }) + }) + .collect::>(); + promise_all(ExprOrSpread { + spread: None, + expr: ArrayLit { + span: DUMMY_SP, + elems: to_ensure_elems, + } + .into(), + }) + } +} + #[cfg(test)] mod tests { use std::collections::HashMap; From 2d42631c8d77d4dfcc78b7e14a03b6e96943a5b5 Mon Sep 17 00:00:00 2001 From: pshu Date: Wed, 28 Aug 2024 08:12:30 +0800 Subject: [PATCH 02/11] =?UTF-8?q?feat:=20=E2=9C=A8=20ensure=202=20runtime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/mako/src/compiler.rs | 1 + crates/mako/src/plugins.rs | 1 + crates/mako/src/plugins/ensure_2.rs | 64 +++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 crates/mako/src/plugins/ensure_2.rs diff --git a/crates/mako/src/compiler.rs b/crates/mako/src/compiler.rs index f26c535c7..e4fa16fd9 100644 --- a/crates/mako/src/compiler.rs +++ b/crates/mako/src/compiler.rs @@ -233,6 +233,7 @@ impl Compiler { Arc::new(plugins::runtime::MakoRuntime {}), Arc::new(plugins::invalid_webpack_syntax::InvalidWebpackSyntaxPlugin {}), Arc::new(plugins::hmr_runtime::HMRRuntimePlugin {}), + Arc::new(plugins::ensure_2::Ensure2 {}), Arc::new(plugins::wasm_runtime::WasmRuntimePlugin {}), Arc::new(plugins::async_runtime::AsyncRuntimePlugin {}), Arc::new(plugins::emotion::EmotionPlugin {}), diff --git a/crates/mako/src/plugins.rs b/crates/mako/src/plugins.rs index c1ddc18ee..7c2bb7e63 100644 --- a/crates/mako/src/plugins.rs +++ b/crates/mako/src/plugins.rs @@ -17,3 +17,4 @@ pub mod runtime; pub mod ssu; pub mod tree_shaking; pub mod wasm_runtime; +pub mod ensure_2; diff --git a/crates/mako/src/plugins/ensure_2.rs b/crates/mako/src/plugins/ensure_2.rs new file mode 100644 index 000000000..f4c3cb067 --- /dev/null +++ b/crates/mako/src/plugins/ensure_2.rs @@ -0,0 +1,64 @@ +use std::collections::BTreeMap; +use std::sync::Arc; + +use crate::compiler::Context; +use crate::generate::chunk::ChunkType; +use crate::plugin::Plugin; + +pub struct Ensure2 {} + +impl Plugin for Ensure2 { + fn name(&self) -> &str { + "dev_ensure2" + } + fn runtime_plugins(&self, _context: &Arc) -> anyhow::Result> { + let cg = _context.chunk_graph.read().unwrap(); + + let mut chunk_async_map: BTreeMap> = BTreeMap::new(); + + cg.get_chunks() + .into_iter() + .filter(|chunk| chunk.chunk_type == ChunkType::Async) + .for_each(|chunk| { + let chunk_ids = { + [ + cg.sync_dependencies_chunk(&chunk.id), + vec![chunk.id.clone()], + ] + .concat() + .iter() + .filter_map(|chunk_id| { + // skip empty chunk because it will not be generated + if cg.chunk(chunk_id).is_some_and(|c| !c.modules.is_empty()) { + Some(chunk_id.id.clone()) + } else { + None + } + }) + .collect::>() + }; + chunk_async_map.insert(chunk.id.id.clone(), chunk_ids); + }); + + // TODO: compress the map to reduce duplicated chunk ids + let ensure_map = serde_json::to_string(&chunk_async_map)?; + + let runtime = format!( + r#" +(function(){{ + let map = {ensure_map}; + requireModule.ensure2 = function(chunkId){{ + let toEnsure = map[chunkId]; + if (toEnsure) {{ + return Promise.all(toEnsure.map(function(c){{ return requireModule.ensure(c); }})) + }}else{{ + return Promise.resolve(); + }} + }}; +}})(); +"# + ); + + Ok(vec![runtime]) + } +} From d1ef84a3b737db051c97f63188d003605c6880fe Mon Sep 17 00:00:00 2001 From: pshu Date: Tue, 5 Nov 2024 15:41:07 +0800 Subject: [PATCH 03/11] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20experimental=20?= =?UTF-8?q?centralEnsure=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/mako/src/config/experimental.rs | 1 + crates/mako/src/config/mako.config.default.json | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/mako/src/config/experimental.rs b/crates/mako/src/config/experimental.rs index 7b65f40fa..1268e3678 100644 --- a/crates/mako/src/config/experimental.rs +++ b/crates/mako/src/config/experimental.rs @@ -10,6 +10,7 @@ pub struct ExperimentalConfig { pub magic_comment: bool, #[serde(deserialize_with = "deserialize_detect_loop")] pub detect_circular_dependence: Option, + pub central_ensure: bool, } #[derive(Deserialize, Serialize, Debug)] diff --git a/crates/mako/src/config/mako.config.default.json b/crates/mako/src/config/mako.config.default.json index d7c50c566..eaec1e1a1 100644 --- a/crates/mako/src/config/mako.config.default.json +++ b/crates/mako/src/config/mako.config.default.json @@ -71,7 +71,8 @@ "detectCircularDependence": { "ignores": ["node_modules"], "graphviz": false - } + }, + "centralEnsure": true }, "useDefineForClassFields": true, "emitDecoratorMetadata": false, From 7fe969d13faaaf87608406ed9e4cf3f9b72171f0 Mon Sep 17 00:00:00 2001 From: pshu Date: Tue, 5 Nov 2024 15:43:21 +0800 Subject: [PATCH 04/11] =?UTF-8?q?refactor:=20=F0=9F=8E=A8=20add=20struct?= =?UTF-8?q?=20ReslvedReplaceInfo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/mako/src/generate/transform.rs | 62 +++++----- crates/mako/src/plugins/bundless_compiler.rs | 12 +- crates/mako/src/visitors/dep_replacer.rs | 34 +++--- crates/mako/src/visitors/dynamic_import.rs | 115 ++++++++++--------- 4 files changed, 119 insertions(+), 104 deletions(-) diff --git a/crates/mako/src/generate/transform.rs b/crates/mako/src/generate/transform.rs index 972740311..102835672 100644 --- a/crates/mako/src/generate/transform.rs +++ b/crates/mako/src/generate/transform.rs @@ -27,7 +27,7 @@ use crate::utils::thread_pool; use crate::visitors::async_module::{mark_async, AsyncModule}; use crate::visitors::common_js::common_js; use crate::visitors::css_imports::CSSImports; -use crate::visitors::dep_replacer::{DepReplacer, DependenciesToReplace}; +use crate::visitors::dep_replacer::{DepReplacer, DependenciesToReplace, ResolvedReplaceInfo}; use crate::visitors::dynamic_import::DynamicImport; use crate::visitors::mako_require::MakoRequire; use crate::visitors::meta_url_replacer::MetaUrlReplacer; @@ -84,36 +84,27 @@ pub fn transform_modules_in_thread( thread_pool::spawn(move || { let module_graph = context.module_graph.read().unwrap(); let deps = module_graph.get_dependencies(&module_id); - let mut resolved_deps: HashMap = deps + let mut resolved_deps: HashMap = deps .into_iter() .map(|(id, dep)| { - ( - dep.source.clone(), - ( - match &dep.resolve_type { - ResolveType::Worker(import_options) => { - let chunk_id = match import_options.get_chunk_name() { - Some(chunk_name) => { - generate_module_id(chunk_name, &context) - } - None => id.generate(&context), - }; - let chunk_graph = context.chunk_graph.read().unwrap(); - chunk_graph.chunk(&chunk_id.into()).unwrap().filename() - } - ResolveType::DynamicImport(import_options) => { - match import_options.get_chunk_name() { - Some(chunk_name) => { - generate_module_id(chunk_name, &context) - } - None => id.generate(&context), - } - } - _ => id.generate(&context), - }, - id.id.clone(), - ), - ) + let to_replace_source = match &dep.resolve_type { + ResolveType::Worker(import_options) => { + let chunk_id = match import_options.get_chunk_name() { + Some(chunk_name) => generate_module_id(chunk_name, &context), + None => id.generate(&context), + }; + let chunk_graph = context.chunk_graph.read().unwrap(); + chunk_graph.chunk(&chunk_id.into()).unwrap().filename() + } + _ => id.generate(&context), + }; + + let resolve_replace_info = ResolvedReplaceInfo { + to_replace_source, + resolved_module_id: id.clone(), + }; + + (dep.source.clone(), resolve_replace_info) }) .collect(); insert_swc_helper_replace(&mut resolved_deps, &context); @@ -162,10 +153,19 @@ pub fn transform_modules_in_thread( Ok(()) } -fn insert_swc_helper_replace(map: &mut HashMap, context: &Arc) { +fn insert_swc_helper_replace( + map: &mut HashMap, + context: &Arc, +) { SWC_HELPERS.into_iter().for_each(|h| { let m_id: ModuleId = h.to_string().into(); - map.insert(m_id.id.clone(), (m_id.generate(context), h.to_string())); + map.insert( + m_id.id.clone(), + ResolvedReplaceInfo { + to_replace_source: m_id.generate(context), + resolved_module_id: m_id, + }, + ); }); } diff --git a/crates/mako/src/plugins/bundless_compiler.rs b/crates/mako/src/plugins/bundless_compiler.rs index 68c10be36..11f2a64d6 100644 --- a/crates/mako/src/plugins/bundless_compiler.rs +++ b/crates/mako/src/plugins/bundless_compiler.rs @@ -24,7 +24,7 @@ use crate::compiler::{Args, Context}; use crate::config::Config; use crate::module::{ModuleAst, ModuleId}; use crate::plugin::{Plugin, PluginTransformJsParam}; -use crate::visitors::dep_replacer::{DepReplacer, DependenciesToReplace}; +use crate::visitors::dep_replacer::{DepReplacer, DependenciesToReplace, ResolvedReplaceInfo}; use crate::visitors::dynamic_import::DynamicImport; pub struct BundlessCompiler { @@ -80,11 +80,17 @@ impl BundlessCompiler { } }; - Ok((dep.source.clone(), (replacement.clone(), replacement))) + Ok(( + dep.source.clone(), + ResolvedReplaceInfo { + to_replace_source: replacement, + resolved_module_id: id.clone(), + }, + )) }) .collect::>>(); - let resolved_deps: HashMap = + let resolved_deps: HashMap = resolved_deps?.into_iter().collect(); drop(module_graph); diff --git a/crates/mako/src/visitors/dep_replacer.rs b/crates/mako/src/visitors/dep_replacer.rs index 3e44434e1..193117760 100644 --- a/crates/mako/src/visitors/dep_replacer.rs +++ b/crates/mako/src/visitors/dep_replacer.rs @@ -23,14 +23,17 @@ pub struct DepReplacer<'a> { pub unresolved_mark: Mark, } -type ResolvedModuleId = String; -type ResolvedModulePath = String; +#[derive(Debug, Clone)] +pub struct ResolvedReplaceInfo { + pub to_replace_source: String, + pub resolved_module_id: ModuleId, +} #[derive(Debug, Clone)] pub struct DependenciesToReplace { // resolved stores the "source" maps to (generate_id, raw_module_id) // e.g. "react" => ("hashed_id", "/abs/to/react/index.js") - pub resolved: HashMap, + pub resolved: HashMap, pub missing: HashMap, } @@ -124,8 +127,9 @@ impl VisitMut for DepReplacer<'_> { // css // TODO: add testcases for this let is_replaceable_css = - if let Some((_, raw_id)) = self.to_replace.resolved.get(&source_string) { - let (path, _search, query, _) = parse_path(raw_id).unwrap(); + if let Some(replace_info) = self.to_replace.resolved.get(&source_string) { + let (path, _search, query, _) = + parse_path(&replace_info.resolved_module_id.id).unwrap(); // when inline_css is enabled // css is parsed as js modules self.context.config.inline_css.is_none() @@ -194,7 +198,7 @@ impl VisitMut for DepReplacer<'_> { impl DepReplacer<'_> { fn replace_source(&mut self, source: &mut Str) { if let Some(replacement) = self.to_replace.resolved.get(&source.value.to_string()) { - let module_id = replacement.0.clone(); + let module_id = replacement.to_replace_source.clone(); let span = source.span; *source = Str::from(module_id); source.span = span; @@ -247,7 +251,7 @@ mod tests { use swc_core::common::GLOBALS; use swc_core::ecma::visit::VisitMutWith; - use super::{DepReplacer, DependenciesToReplace, ResolvedModuleId, ResolvedModulePath}; + use super::{DepReplacer, DependenciesToReplace, ResolvedReplaceInfo}; use crate::ast::tests::TestUtils; use crate::module::{Dependency, ImportType, ModuleId, ResolveType}; @@ -339,17 +343,13 @@ try { ); } - fn build_resolved( - key: &str, - module_id: &str, - ) -> HashMap { + fn build_resolved(key: &str, module_id: &str) -> HashMap { hashmap! { key.to_string() => - ( - - module_id.to_string(), - "".to_string() - ) + ResolvedReplaceInfo { + to_replace_source: module_id.into(), + resolved_module_id: "".into(), + } } } @@ -367,7 +367,7 @@ try { fn run( js_code: &str, - resolved: HashMap, + resolved: HashMap, missing: HashMap, ) -> String { let mut test_utils = TestUtils::gen_js_ast(js_code); diff --git a/crates/mako/src/visitors/dynamic_import.rs b/crates/mako/src/visitors/dynamic_import.rs index ded70eba3..71de72cbe 100644 --- a/crates/mako/src/visitors/dynamic_import.rs +++ b/crates/mako/src/visitors/dynamic_import.rs @@ -13,7 +13,7 @@ use super::dep_replacer::miss_throw_stmt; use crate::ast::utils::{is_dynamic_import, promise_all, require_ensure}; use crate::ast::DUMMY_CTXT; use crate::compiler::Context; -use crate::generate::chunk::ChunkId; +use crate::module::ModuleId; use crate::visitors::dep_replacer::DependenciesToReplace; pub struct DynamicImport<'a> { @@ -47,14 +47,16 @@ impl<'a> VisitMut for DynamicImport<'a> { .position(|module_item| !module_item.directive_continue()) .unwrap(); - let (id, _) = self + let interop_replace = 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 require_interop = quote_ident!("__mako_require__").as_call( + DUMMY_SP, + vec![quote_str!(interop_replace.to_replace_source.clone()).as_arg()], + ); let stmt: Stmt = Expr::Member(MemberExpr { span: DUMMY_SP, @@ -74,7 +76,6 @@ impl<'a> VisitMut for DynamicImport<'a> { if call_expr.args.is_empty() { return; } - if let ExprOrSpread { expr: box Expr::Lit(Lit::Str(ref mut source)), .. @@ -106,60 +107,28 @@ impl<'a> VisitMut for DynamicImport<'a> { // it must be resolved, so unwrap is safe here. .unwrap(); - let resolved_source = &resolved_info.0; - let _chunk_ids = { - let chunk_id: ChunkId = resolved_source.clone().into(); - let chunk_graph = &self.context.chunk_graph.read().unwrap(); - let chunk = chunk_graph.chunk(&chunk_id); - let chunk_ids = match chunk { - Some(chunk) => { - [ - chunk_graph.sync_dependencies_chunk(&chunk.id), - vec![chunk.id.clone()], - ] - .concat() - .iter() - .filter_map(|chunk_id| { - // skip empty chunk because it will not be generated - if chunk_graph - .chunk(chunk_id) - .is_some_and(|c| !c.modules.is_empty()) - { - Some(chunk_id.id.clone()) - } else { - None - } - }) - .collect::>() - } - // None means the original chunk has been optimized to entry chunk - None => vec![], - }; - chunk_ids - }; - self.changed = true; - // build new expr - // e.g. - // Promise.all([ require.ensure("id") ]).then(require.bind(require, "id")) - // Promise.all([ require.ensure("d1"), require.ensure("id)]).then(require.bind(require, "id")) - - // version 2 - // require.ensure2("id").then(require.bind(require,"id")) + let generated_module_id = resolved_info.to_replace_source.clone(); *expr = { // let load_promise = self.make_load_promise(&chunk_ids); - let load_promise = self.make_load_promise_2(resolved_source); + + let load_promise = if self.context.args.watch + && self.context.config.experimental.central_ensure + { + self.central_ensure(&generated_module_id) + } else { + self.inline_ensure(&resolved_info.resolved_module_id, &self.context) + }; let lazy_require_call = member_expr!(DUMMY_CTXT, DUMMY_SP, __mako_require__.bind).as_call( DUMMY_SP, vec![ quote_ident!("__mako_require__").as_arg(), - quote_str!(resolved_source.clone()).as_arg(), + quote_str!(generated_module_id).as_arg(), ], ); - let dr_call = member_expr!(DUMMY_CTXT, DUMMY_SP, __mako_require__.dr) .as_call( DUMMY_SP, @@ -177,12 +146,47 @@ impl<'a> VisitMut for DynamicImport<'a> { } impl DynamicImport<'_> { - fn make_load_promise_2(&self, module_id: &str) -> Expr { + // require.ensure2("id").then(require.bind(require,"id")) + fn central_ensure(&self, module_id: &str) -> Expr { member_expr!(DUMMY_CTXT, DUMMY_SP, __mako_require__.ensure2) .as_call(DUMMY_SP, vec![quote_str!(module_id).as_arg()]) } - fn make_load_promise(&self, chunk_ids: &[String]) -> Expr { + // build the Promise.all([...]) part + // Promise.all([ require.ensure("id") ]).then(require.bind(require, "id")) + // Promise.all([ require.ensure("d1"), require.ensure("id)]).then(require.bind(require, "id")) + fn inline_ensure(&self, module_id: &ModuleId, context: &Arc) -> Expr { + let chunk_graph = context.chunk_graph.read().unwrap(); + + let chunk_ids = { + let chunk = chunk_graph.get_chunk_for_module(module_id); + let chunk_ids = match chunk { + Some(chunk) => { + [ + chunk_graph.sync_dependencies_chunk(&chunk.id), + vec![chunk.id.clone()], + ] + .concat() + .iter() + .filter_map(|chunk_id| { + // skip empty chunk because it will not be generated + if chunk_graph + .chunk(chunk_id) + .is_some_and(|c| !c.modules.is_empty()) + { + Some(chunk_id.id.clone()) + } else { + None + } + }) + .collect::>() + } + // None means the original chunk has been optimized to entry chunk + None => vec![], + }; + chunk_ids + }; + let to_ensure_elems = chunk_ids .iter() .map(|c_id| { @@ -213,7 +217,7 @@ mod tests { use super::DynamicImport; use crate::ast::tests::TestUtils; use crate::generate::chunk::{Chunk, ChunkType}; - use crate::visitors::dep_replacer::DependenciesToReplace; + use crate::visitors::dep_replacer::{DependenciesToReplace, ResolvedReplaceInfo}; // TODO: add nested chunk test #[test] @@ -242,9 +246,14 @@ Promise.all([ let dep_to_replace = DependenciesToReplace { resolved: maplit::hashmap! { - "@swc/helpers/_/_interop_require_wildcard".to_string() => - ("hashed_helper".to_string(), "dummy".into()), - "foo".to_string() => ("foo".to_string(), "foo".to_string()) + "@swc/helpers/_/_interop_require_wildcard".to_string() => ResolvedReplaceInfo { + to_replace_source: "hashed_helper".to_string(), + resolved_module_id:"dummy".into() + }, + "foo".to_string() => ResolvedReplaceInfo { + to_replace_source: "foo".into(), + resolved_module_id: "foo".into() + } }, missing: HashMap::new(), }; From 1abdb374c1f0c09f0dcd0d4d2b5f5247412ff1d7 Mon Sep 17 00:00:00 2001 From: pshu Date: Tue, 5 Nov 2024 15:44:54 +0800 Subject: [PATCH 05/11] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20central=20ensur?= =?UTF-8?q?e=20plugin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/mako/src/compiler.rs | 5 +- crates/mako/src/plugins.rs | 2 +- crates/mako/src/plugins/central_ensure.rs | 76 +++++++++++++++++++++++ crates/mako/src/plugins/ensure_2.rs | 64 ------------------- 4 files changed, 81 insertions(+), 66 deletions(-) create mode 100644 crates/mako/src/plugins/central_ensure.rs delete mode 100644 crates/mako/src/plugins/ensure_2.rs diff --git a/crates/mako/src/compiler.rs b/crates/mako/src/compiler.rs index 878e0c540..fc124a87e 100644 --- a/crates/mako/src/compiler.rs +++ b/crates/mako/src/compiler.rs @@ -248,7 +248,6 @@ impl Compiler { Arc::new(plugins::runtime::MakoRuntime {}), Arc::new(plugins::invalid_webpack_syntax::InvalidWebpackSyntaxPlugin {}), Arc::new(plugins::hmr_runtime::HMRRuntimePlugin {}), - Arc::new(plugins::ensure_2::Ensure2 {}), Arc::new(plugins::wasm_runtime::WasmRuntimePlugin {}), Arc::new(plugins::async_runtime::AsyncRuntimePlugin {}), Arc::new(plugins::emotion::EmotionPlugin {}), @@ -299,6 +298,10 @@ impl Compiler { plugins.push(Arc::new(plugins::ssu::SUPlus::new())); } + if args.watch && config.experimental.central_ensure { + plugins.push(Arc::new(plugins::central_ensure::CentralChunkEnsure {})); + } + if let Some(minifish_config) = &config._minifish { let inject = if let Some(inject) = &minifish_config.inject { let mut map = HashMap::new(); diff --git a/crates/mako/src/plugins.rs b/crates/mako/src/plugins.rs index 49e6a256e..b2c545d58 100644 --- a/crates/mako/src/plugins.rs +++ b/crates/mako/src/plugins.rs @@ -1,11 +1,11 @@ pub mod async_runtime; pub mod bundless_compiler; +pub mod central_ensure; pub mod context_module; pub mod copy; pub mod detect_circular_dependence; pub mod duplicate_package_checker; pub mod emotion; -pub mod ensure_2; pub mod graphviz; pub mod hmr_runtime; pub mod ignore; diff --git a/crates/mako/src/plugins/central_ensure.rs b/crates/mako/src/plugins/central_ensure.rs new file mode 100644 index 000000000..37e98ac2b --- /dev/null +++ b/crates/mako/src/plugins/central_ensure.rs @@ -0,0 +1,76 @@ +use std::collections::BTreeMap; +use std::sync::Arc; + +use anyhow::anyhow; + +use crate::compiler::Context; +use crate::module::generate_module_id; +use crate::plugin::Plugin; + +pub struct CentralChunkEnsure {} + +impl Plugin for CentralChunkEnsure { + fn name(&self) -> &str { + "dev_ensure2" + } + fn runtime_plugins(&self, context: &Arc) -> anyhow::Result> { + let mg = context + .module_graph + .read() + .map_err(|e| anyhow!("Read_Module_Graph_error:\n{:?}", e))?; + let cg = context + .chunk_graph + .read() + .map_err(|e| anyhow!("Read_Chunk_Graph_error:\n{:?}", e))?; + + let mut chunk_async_map: BTreeMap> = Default::default(); + + mg.modules().iter().for_each(|module| { + let be_dynamic_imported = mg + .get_dependents(&module.id) + .iter() + .any(|(_, dep)| dep.resolve_type.is_dynamic_esm()); + + if be_dynamic_imported { + cg.get_chunk_for_module(&module.id) + .iter() + .for_each(|chunk| { + let deps_chunks = cg + .installable_descendants_chunk(&chunk.id) + .iter() + .map(|chunk_id| chunk_id.generate(context)) + .collect::>(); + + dbg!(&deps_chunks); + + chunk_async_map + .insert(generate_module_id(&module.id.id, context), deps_chunks); + }); + } + }); + + // TODO: compress the map to reduce duplicated chunk ids + let ensure_map = serde_json::to_string(&chunk_async_map)?; + + let runtime = format!( + r#" +(function(){{ + let map = {ensure_map}; + requireModule.updateEnsuire2Map = function(newMaping) {{ + map = newMaping; + }} + requireModule.ensure2 = function(chunkId){{ + let toEnsure = map[chunkId]; + if (toEnsure) {{ + return Promise.all(toEnsure.map(function(c){{ return requireModule.ensure(c); }})) + }}else{{ + return Promise.resolve(); + }} + }}; +}})(); +"# + ); + + Ok(vec![runtime]) + } +} diff --git a/crates/mako/src/plugins/ensure_2.rs b/crates/mako/src/plugins/ensure_2.rs deleted file mode 100644 index f4c3cb067..000000000 --- a/crates/mako/src/plugins/ensure_2.rs +++ /dev/null @@ -1,64 +0,0 @@ -use std::collections::BTreeMap; -use std::sync::Arc; - -use crate::compiler::Context; -use crate::generate::chunk::ChunkType; -use crate::plugin::Plugin; - -pub struct Ensure2 {} - -impl Plugin for Ensure2 { - fn name(&self) -> &str { - "dev_ensure2" - } - fn runtime_plugins(&self, _context: &Arc) -> anyhow::Result> { - let cg = _context.chunk_graph.read().unwrap(); - - let mut chunk_async_map: BTreeMap> = BTreeMap::new(); - - cg.get_chunks() - .into_iter() - .filter(|chunk| chunk.chunk_type == ChunkType::Async) - .for_each(|chunk| { - let chunk_ids = { - [ - cg.sync_dependencies_chunk(&chunk.id), - vec![chunk.id.clone()], - ] - .concat() - .iter() - .filter_map(|chunk_id| { - // skip empty chunk because it will not be generated - if cg.chunk(chunk_id).is_some_and(|c| !c.modules.is_empty()) { - Some(chunk_id.id.clone()) - } else { - None - } - }) - .collect::>() - }; - chunk_async_map.insert(chunk.id.id.clone(), chunk_ids); - }); - - // TODO: compress the map to reduce duplicated chunk ids - let ensure_map = serde_json::to_string(&chunk_async_map)?; - - let runtime = format!( - r#" -(function(){{ - let map = {ensure_map}; - requireModule.ensure2 = function(chunkId){{ - let toEnsure = map[chunkId]; - if (toEnsure) {{ - return Promise.all(toEnsure.map(function(c){{ return requireModule.ensure(c); }})) - }}else{{ - return Promise.resolve(); - }} - }}; -}})(); -"# - ); - - Ok(vec![runtime]) - } -} From beae8cebd7840b53d6aa592cc241af892fea5c00 Mon Sep 17 00:00:00 2001 From: pshu Date: Tue, 5 Nov 2024 22:58:11 +0800 Subject: [PATCH 06/11] =?UTF-8?q?refactor:=20=F0=9F=8E=A8=20extract=20modu?= =?UTF-8?q?le=20ensure=20map?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/mako/src/generate/hmr.rs | 21 +++++-- crates/mako/src/plugins/central_ensure.rs | 73 ++++++++++++----------- 2 files changed, 55 insertions(+), 39 deletions(-) diff --git a/crates/mako/src/generate/hmr.rs b/crates/mako/src/generate/hmr.rs index 43f917587..24af96892 100644 --- a/crates/mako/src/generate/hmr.rs +++ b/crates/mako/src/generate/hmr.rs @@ -10,6 +10,7 @@ use crate::compiler::Compiler; use crate::generate::chunk::Chunk; use crate::generate::generate_chunks::modules_to_js_stmts; use crate::module::ModuleId; +use crate::plugins::central_ensure::module_ensure_map; impl Compiler { pub fn generate_hmr_chunk( @@ -22,10 +23,22 @@ impl Compiler { let module_graph = &self.context.module_graph.read().unwrap(); let (js_stmts, _) = modules_to_js_stmts(module_ids, module_graph, &self.context).unwrap(); let content = include_str!("../runtime/runtime_hmr.js").to_string(); - let content = content.replace("__CHUNK_ID__", &chunk.id.id).replace( - "__runtime_code__", - &format!("runtime._h='{}';", current_hash), - ); + + let mut runtime_code_snippets = vec![format!("runtime._h='{}';\n", current_hash)]; + + if self.context.config.experimental.central_ensure { + if let Ok(map) = module_ensure_map(&self.context) { + runtime_code_snippets.push(format!( + "runtime.updateEnsure2Map({})", + serde_json::to_string(&map)? + )); + } + } + + let content = content + .replace("__CHUNK_ID__", &chunk.id.id) + .replace("__runtime_code__", &runtime_code_snippets.join("\n")); + let mut js_ast = JsAst::build(filename, content.as_str(), self.context.clone()) /* safe */ .unwrap(); diff --git a/crates/mako/src/plugins/central_ensure.rs b/crates/mako/src/plugins/central_ensure.rs index 37e98ac2b..747b6f4d2 100644 --- a/crates/mako/src/plugins/central_ensure.rs +++ b/crates/mako/src/plugins/central_ensure.rs @@ -9,45 +9,48 @@ use crate::plugin::Plugin; pub struct CentralChunkEnsure {} -impl Plugin for CentralChunkEnsure { - fn name(&self) -> &str { - "dev_ensure2" - } - fn runtime_plugins(&self, context: &Arc) -> anyhow::Result> { - let mg = context - .module_graph - .read() - .map_err(|e| anyhow!("Read_Module_Graph_error:\n{:?}", e))?; - let cg = context - .chunk_graph - .read() - .map_err(|e| anyhow!("Read_Chunk_Graph_error:\n{:?}", e))?; +pub fn module_ensure_map(context: &Arc) -> anyhow::Result>> { + let mg = context + .module_graph + .read() + .map_err(|e| anyhow!("Read_Module_Graph_error:\n{:?}", e))?; + let cg = context + .chunk_graph + .read() + .map_err(|e| anyhow!("Read_Chunk_Graph_error:\n{:?}", e))?; + + let mut chunk_async_map: BTreeMap> = Default::default(); - let mut chunk_async_map: BTreeMap> = Default::default(); + mg.modules().iter().for_each(|module| { + let be_dynamic_imported = mg + .get_dependents(&module.id) + .iter() + .any(|(_, dep)| dep.resolve_type.is_dynamic_esm()); - mg.modules().iter().for_each(|module| { - let be_dynamic_imported = mg - .get_dependents(&module.id) + if be_dynamic_imported { + cg.get_chunk_for_module(&module.id) .iter() - .any(|(_, dep)| dep.resolve_type.is_dynamic_esm()); + .for_each(|chunk| { + let deps_chunks = cg + .installable_descendants_chunk(&chunk.id) + .iter() + .map(|chunk_id| chunk_id.generate(context)) + .collect::>(); - if be_dynamic_imported { - cg.get_chunk_for_module(&module.id) - .iter() - .for_each(|chunk| { - let deps_chunks = cg - .installable_descendants_chunk(&chunk.id) - .iter() - .map(|chunk_id| chunk_id.generate(context)) - .collect::>(); + chunk_async_map.insert(generate_module_id(&module.id.id, context), deps_chunks); + }); + } + }); - dbg!(&deps_chunks); + Ok(chunk_async_map) +} - chunk_async_map - .insert(generate_module_id(&module.id.id, context), deps_chunks); - }); - } - }); +impl Plugin for CentralChunkEnsure { + fn name(&self) -> &str { + "dev_ensure2" + } + fn runtime_plugins(&self, context: &Arc) -> anyhow::Result> { + let chunk_async_map = module_ensure_map(context)?; // TODO: compress the map to reduce duplicated chunk ids let ensure_map = serde_json::to_string(&chunk_async_map)?; @@ -56,8 +59,8 @@ impl Plugin for CentralChunkEnsure { r#" (function(){{ let map = {ensure_map}; - requireModule.updateEnsuire2Map = function(newMaping) {{ - map = newMaping; + requireModule.updateEnsure2Map = function(newMapping) {{ + map = newMapping; }} requireModule.ensure2 = function(chunkId){{ let toEnsure = map[chunkId]; From 8cef5322b3a417e7febcb2c349d5d462369dde4f Mon Sep 17 00:00:00 2001 From: pshu Date: Wed, 6 Nov 2024 13:51:57 +0800 Subject: [PATCH 07/11] =?UTF-8?q?refactor:=20=F0=9F=8E=A8=20add=20back=20c?= =?UTF-8?q?hunk=20id=20in=20replace=20info?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/mako/src/generate/chunk_graph.rs | 6 ++++ crates/mako/src/generate/transform.rs | 35 +++++++++++++++----- crates/mako/src/plugins/bundless_compiler.rs | 1 + crates/mako/src/plugins/central_ensure.rs | 2 +- crates/mako/src/visitors/dep_replacer.rs | 2 ++ crates/mako/src/visitors/dynamic_import.rs | 13 +++++--- 6 files changed, 45 insertions(+), 14 deletions(-) diff --git a/crates/mako/src/generate/chunk_graph.rs b/crates/mako/src/generate/chunk_graph.rs index 208a66d63..33e505dce 100644 --- a/crates/mako/src/generate/chunk_graph.rs +++ b/crates/mako/src/generate/chunk_graph.rs @@ -63,6 +63,12 @@ impl ChunkGraph { self.graph.node_weights().find(|c| c.has_module(module_id)) } + pub fn get_async_chunk_for_module(&self, module_id: &ModuleId) -> Option<&Chunk> { + self.graph + .node_weights() + .find(|c| c.has_module(module_id) && matches!(c.chunk_type, ChunkType::Async)) + } + // pub fn get_chunk_by_id(&self, id: &String) -> Option<&Chunk> { // self.graph.node_weights().find(|c| c.id.id.eq(id)) // } diff --git a/crates/mako/src/generate/transform.rs b/crates/mako/src/generate/transform.rs index 102835672..b3598010f 100644 --- a/crates/mako/src/generate/transform.rs +++ b/crates/mako/src/generate/transform.rs @@ -87,24 +87,42 @@ pub fn transform_modules_in_thread( let mut resolved_deps: HashMap = deps .into_iter() .map(|(id, dep)| { - let to_replace_source = match &dep.resolve_type { + let replace_info = match &dep.resolve_type { ResolveType::Worker(import_options) => { let chunk_id = match import_options.get_chunk_name() { Some(chunk_name) => generate_module_id(chunk_name, &context), None => id.generate(&context), }; let chunk_graph = context.chunk_graph.read().unwrap(); - chunk_graph.chunk(&chunk_id.into()).unwrap().filename() + let chunk_name = + chunk_graph.chunk(&chunk_id.into()).unwrap().filename(); + + ResolvedReplaceInfo { + chunk_id: None, + to_replace_source: chunk_name, + resolved_module_id: id.clone(), + } } - _ => id.generate(&context), - }; + ResolveType::DynamicImport(import_options) => { + let chunk_id = Some(match import_options.get_chunk_name() { + Some(chunk_name) => generate_module_id(chunk_name, &context), + None => id.generate(&context), + }); - let resolve_replace_info = ResolvedReplaceInfo { - to_replace_source, - resolved_module_id: id.clone(), + ResolvedReplaceInfo { + chunk_id, + to_replace_source: id.generate(&context), + resolved_module_id: id.clone(), + } + } + _ => ResolvedReplaceInfo { + chunk_id: None, + to_replace_source: id.generate(&context), + resolved_module_id: id.clone(), + }, }; - (dep.source.clone(), resolve_replace_info) + (dep.source.clone(), replace_info) }) .collect(); insert_swc_helper_replace(&mut resolved_deps, &context); @@ -162,6 +180,7 @@ fn insert_swc_helper_replace( map.insert( m_id.id.clone(), ResolvedReplaceInfo { + chunk_id: None, to_replace_source: m_id.generate(context), resolved_module_id: m_id, }, diff --git a/crates/mako/src/plugins/bundless_compiler.rs b/crates/mako/src/plugins/bundless_compiler.rs index 11f2a64d6..e93aa3412 100644 --- a/crates/mako/src/plugins/bundless_compiler.rs +++ b/crates/mako/src/plugins/bundless_compiler.rs @@ -83,6 +83,7 @@ impl BundlessCompiler { Ok(( dep.source.clone(), ResolvedReplaceInfo { + chunk_id: None, to_replace_source: replacement, resolved_module_id: id.clone(), }, diff --git a/crates/mako/src/plugins/central_ensure.rs b/crates/mako/src/plugins/central_ensure.rs index 747b6f4d2..a28e02ba3 100644 --- a/crates/mako/src/plugins/central_ensure.rs +++ b/crates/mako/src/plugins/central_ensure.rs @@ -28,7 +28,7 @@ pub fn module_ensure_map(context: &Arc) -> anyhow::Result { #[derive(Debug, Clone)] pub struct ResolvedReplaceInfo { + pub chunk_id: Option, pub to_replace_source: String, pub resolved_module_id: ModuleId, } @@ -347,6 +348,7 @@ try { hashmap! { key.to_string() => ResolvedReplaceInfo { + chunk_id: None, to_replace_source: module_id.into(), resolved_module_id: "".into(), } diff --git a/crates/mako/src/visitors/dynamic_import.rs b/crates/mako/src/visitors/dynamic_import.rs index 71de72cbe..b8cd66ac9 100644 --- a/crates/mako/src/visitors/dynamic_import.rs +++ b/crates/mako/src/visitors/dynamic_import.rs @@ -9,11 +9,11 @@ use swc_core::ecma::utils::{ }; use swc_core::ecma::visit::{VisitMut, VisitMutWith}; -use super::dep_replacer::miss_throw_stmt; +use super::dep_replacer::{miss_throw_stmt, ResolvedReplaceInfo}; use crate::ast::utils::{is_dynamic_import, promise_all, require_ensure}; use crate::ast::DUMMY_CTXT; use crate::compiler::Context; -use crate::module::ModuleId; +use crate::generate::chunk::ChunkId; use crate::visitors::dep_replacer::DependenciesToReplace; pub struct DynamicImport<'a> { @@ -118,7 +118,7 @@ impl<'a> VisitMut for DynamicImport<'a> { { self.central_ensure(&generated_module_id) } else { - self.inline_ensure(&resolved_info.resolved_module_id, &self.context) + self.inline_ensure(resolved_info, &self.context) }; let lazy_require_call = @@ -155,11 +155,12 @@ impl DynamicImport<'_> { // build the Promise.all([...]) part // Promise.all([ require.ensure("id") ]).then(require.bind(require, "id")) // Promise.all([ require.ensure("d1"), require.ensure("id)]).then(require.bind(require, "id")) - fn inline_ensure(&self, module_id: &ModuleId, context: &Arc) -> Expr { + fn inline_ensure(&self, replace_info: &ResolvedReplaceInfo, context: &Arc) -> Expr { let chunk_graph = context.chunk_graph.read().unwrap(); + let init_chunk_id: ChunkId = replace_info.chunk_id.as_ref().unwrap().clone().into(); let chunk_ids = { - let chunk = chunk_graph.get_chunk_for_module(module_id); + let chunk = chunk_graph.chunk(&init_chunk_id); let chunk_ids = match chunk { Some(chunk) => { [ @@ -247,10 +248,12 @@ Promise.all([ let dep_to_replace = DependenciesToReplace { resolved: maplit::hashmap! { "@swc/helpers/_/_interop_require_wildcard".to_string() => ResolvedReplaceInfo { + chunk_id: None, to_replace_source: "hashed_helper".to_string(), resolved_module_id:"dummy".into() }, "foo".to_string() => ResolvedReplaceInfo { + chunk_id: Some("foo".into()), to_replace_source: "foo".into(), resolved_module_id: "foo".into() } From 8609332de583af7eb88399e50351a365286ec152 Mon Sep 17 00:00:00 2001 From: pshu Date: Thu, 7 Nov 2024 01:00:00 +0800 Subject: [PATCH 08/11] =?UTF-8?q?refactor:=20=F0=9F=8E=A8=20fix=20case=20o?= =?UTF-8?q?ne=20source=20with=20different=20import=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/mako/src/generate/transform.rs | 80 ++++++++++++++------------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/crates/mako/src/generate/transform.rs b/crates/mako/src/generate/transform.rs index b3598010f..93eab1617 100644 --- a/crates/mako/src/generate/transform.rs +++ b/crates/mako/src/generate/transform.rs @@ -84,47 +84,53 @@ pub fn transform_modules_in_thread( thread_pool::spawn(move || { let module_graph = context.module_graph.read().unwrap(); let deps = module_graph.get_dependencies(&module_id); - let mut resolved_deps: HashMap = deps - .into_iter() - .map(|(id, dep)| { - let replace_info = match &dep.resolve_type { - ResolveType::Worker(import_options) => { - let chunk_id = match import_options.get_chunk_name() { - Some(chunk_name) => generate_module_id(chunk_name, &context), - None => id.generate(&context), - }; - let chunk_graph = context.chunk_graph.read().unwrap(); - let chunk_name = - chunk_graph.chunk(&chunk_id.into()).unwrap().filename(); - - ResolvedReplaceInfo { - chunk_id: None, - to_replace_source: chunk_name, - resolved_module_id: id.clone(), - } - } - ResolveType::DynamicImport(import_options) => { - let chunk_id = Some(match import_options.get_chunk_name() { - Some(chunk_name) => generate_module_id(chunk_name, &context), - None => id.generate(&context), - }); - - ResolvedReplaceInfo { - chunk_id, - to_replace_source: id.generate(&context), - resolved_module_id: id.clone(), - } - } - _ => ResolvedReplaceInfo { + let mut resolved_deps: HashMap = Default::default(); + + deps.into_iter().for_each(|(id, dep)| { + let replace_info = match &dep.resolve_type { + ResolveType::Worker(import_options) => { + let chunk_id = match import_options.get_chunk_name() { + Some(chunk_name) => generate_module_id(chunk_name, &context), + None => id.generate(&context), + }; + let chunk_graph = context.chunk_graph.read().unwrap(); + let chunk_name = chunk_graph.chunk(&chunk_id.into()).unwrap().filename(); + + ResolvedReplaceInfo { chunk_id: None, + to_replace_source: chunk_name, + resolved_module_id: id.clone(), + } + } + ResolveType::DynamicImport(import_options) => { + let chunk_id = Some(match import_options.get_chunk_name() { + Some(chunk_name) => generate_module_id(chunk_name, &context), + None => id.generate(&context), + }); + + ResolvedReplaceInfo { + chunk_id, to_replace_source: id.generate(&context), resolved_module_id: id.clone(), - }, - }; + } + } + _ => ResolvedReplaceInfo { + chunk_id: None, + to_replace_source: id.generate(&context), + resolved_module_id: id.clone(), + }, + }; - (dep.source.clone(), replace_info) - }) - .collect(); + resolved_deps + .entry(dep.source.clone()) + .and_modify(|info: &mut ResolvedReplaceInfo| { + match (&replace_info.chunk_id, &info.chunk_id) { + (None, _) => {} + (Some(id), _) => info.chunk_id = Some(id.clone()), + } + }) + .or_insert(replace_info); + }); insert_swc_helper_replace(&mut resolved_deps, &context); let module = module_graph.get_module(&module_id).unwrap(); let info = module.info.as_ref().unwrap(); From 61bedb062c4f879982cb09db01dc8909183746c1 Mon Sep 17 00:00:00 2001 From: pshu Date: Thu, 7 Nov 2024 10:40:21 +0800 Subject: [PATCH 09/11] =?UTF-8?q?refactor:=20=F0=9F=8E=A8=20extract=20hmr?= =?UTF-8?q?=20runtime=20update=20code=20aspect?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/mako/src/generate/hmr.rs | 17 ++++++----------- crates/mako/src/plugin.rs | 12 ++++++++++++ crates/mako/src/plugins/central_ensure.rs | 11 +++++++++++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/crates/mako/src/generate/hmr.rs b/crates/mako/src/generate/hmr.rs index 24af96892..f531eac05 100644 --- a/crates/mako/src/generate/hmr.rs +++ b/crates/mako/src/generate/hmr.rs @@ -10,7 +10,6 @@ use crate::compiler::Compiler; use crate::generate::chunk::Chunk; use crate::generate::generate_chunks::modules_to_js_stmts; use crate::module::ModuleId; -use crate::plugins::central_ensure::module_ensure_map; impl Compiler { pub fn generate_hmr_chunk( @@ -24,16 +23,12 @@ impl Compiler { let (js_stmts, _) = modules_to_js_stmts(module_ids, module_graph, &self.context).unwrap(); let content = include_str!("../runtime/runtime_hmr.js").to_string(); - let mut runtime_code_snippets = vec![format!("runtime._h='{}';\n", current_hash)]; - - if self.context.config.experimental.central_ensure { - if let Ok(map) = module_ensure_map(&self.context) { - runtime_code_snippets.push(format!( - "runtime.updateEnsure2Map({})", - serde_json::to_string(&map)? - )); - } - } + let runtime_code_snippets = [ + format!("runtime._h='{}';", current_hash), + self.context + .plugin_driver + .hmr_runtime_update_code(&self.context)?, + ]; let content = content .replace("__CHUNK_ID__", &chunk.id.id) diff --git a/crates/mako/src/plugin.rs b/crates/mako/src/plugin.rs index 62ac2ffaf..9d67d6d1a 100644 --- a/crates/mako/src/plugin.rs +++ b/crates/mako/src/plugin.rs @@ -159,6 +159,10 @@ pub trait Plugin: Any + Send + Sync { Ok(Vec::new()) } + fn hmr_runtime_updates(&self, _context: &Arc) -> Result> { + Ok(Vec::new()) + } + fn optimize_module_graph( &self, _module_graph: &mut ModuleGraph, @@ -368,6 +372,14 @@ impl PluginDriver { Ok(plugins.join("\n")) } + pub fn hmr_runtime_update_code(&self, context: &Arc) -> Result { + let mut plugins = Vec::new(); + for plugin in &self.plugins { + plugins.extend(plugin.hmr_runtime_updates(context)?); + } + Ok(plugins.join("\n")) + } + pub fn optimize_module_graph( &self, module_graph: &mut ModuleGraph, diff --git a/crates/mako/src/plugins/central_ensure.rs b/crates/mako/src/plugins/central_ensure.rs index a28e02ba3..2f9d8ed49 100644 --- a/crates/mako/src/plugins/central_ensure.rs +++ b/crates/mako/src/plugins/central_ensure.rs @@ -76,4 +76,15 @@ impl Plugin for CentralChunkEnsure { Ok(vec![runtime]) } + + fn hmr_runtime_updates(&self, _context: &Arc) -> anyhow::Result> { + let map = module_ensure_map(_context)?; + + let update_mapping = format!( + "runtime.updateEnsure2Map({});", + serde_json::to_string(&map)? + ); + + Ok(vec![update_mapping]) + } } From 0f7e63a72f3210fc4d6618461f2e71c51c34f998 Mon Sep 17 00:00:00 2001 From: pshu Date: Thu, 7 Nov 2024 11:40:27 +0800 Subject: [PATCH 10/11] release: @umijs/mako@0.9.6-canary.20241107.1 --- packages/bundler-mako/package.json | 2 +- packages/mako/npm/darwin-arm64/package.json | 2 +- packages/mako/npm/darwin-x64/package.json | 2 +- packages/mako/npm/linux-arm64-gnu/package.json | 2 +- .../mako/npm/linux-arm64-musl/package.json | 2 +- packages/mako/npm/linux-x64-gnu/package.json | 2 +- packages/mako/npm/linux-x64-musl/package.json | 2 +- packages/mako/npm/win32-ia32-msvc/package.json | 2 +- packages/mako/npm/win32-x64-msvc/package.json | 2 +- packages/mako/package.json | 18 +++++++++--------- 10 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/bundler-mako/package.json b/packages/bundler-mako/package.json index 11fd6977d..1570889b1 100644 --- a/packages/bundler-mako/package.json +++ b/packages/bundler-mako/package.json @@ -3,7 +3,7 @@ "version": "0.9.4", "dependencies": { "@umijs/bundler-utils": "^4.0.81", - "@umijs/mako": "0.9.4", + "@umijs/mako": "0.9.6-canary.20241107.1", "chalk": "^4.1.2", "compression": "^1.7.4", "connect-history-api-fallback": "^2.0.0", diff --git a/packages/mako/npm/darwin-arm64/package.json b/packages/mako/npm/darwin-arm64/package.json index c24036a0d..f8cafcbcb 100644 --- a/packages/mako/npm/darwin-arm64/package.json +++ b/packages/mako/npm/darwin-arm64/package.json @@ -1,6 +1,6 @@ { "name": "@umijs/mako-darwin-arm64", - "version": "0.9.4", + "version": "0.9.6-canary.20241107.1", "os": [ "darwin" ], diff --git a/packages/mako/npm/darwin-x64/package.json b/packages/mako/npm/darwin-x64/package.json index b414fb895..a9b706742 100644 --- a/packages/mako/npm/darwin-x64/package.json +++ b/packages/mako/npm/darwin-x64/package.json @@ -1,6 +1,6 @@ { "name": "@umijs/mako-darwin-x64", - "version": "0.9.4", + "version": "0.9.6-canary.20241107.1", "os": [ "darwin" ], diff --git a/packages/mako/npm/linux-arm64-gnu/package.json b/packages/mako/npm/linux-arm64-gnu/package.json index fdcd92d34..375ca37d8 100644 --- a/packages/mako/npm/linux-arm64-gnu/package.json +++ b/packages/mako/npm/linux-arm64-gnu/package.json @@ -1,6 +1,6 @@ { "name": "@umijs/mako-linux-arm64-gnu", - "version": "0.9.4", + "version": "0.9.6-canary.20241107.1", "os": [ "linux" ], diff --git a/packages/mako/npm/linux-arm64-musl/package.json b/packages/mako/npm/linux-arm64-musl/package.json index 7e54c876c..f2f9a74af 100644 --- a/packages/mako/npm/linux-arm64-musl/package.json +++ b/packages/mako/npm/linux-arm64-musl/package.json @@ -1,6 +1,6 @@ { "name": "@umijs/mako-linux-arm64-musl", - "version": "0.9.4", + "version": "0.9.6-canary.20241107.1", "os": [ "linux" ], diff --git a/packages/mako/npm/linux-x64-gnu/package.json b/packages/mako/npm/linux-x64-gnu/package.json index 96601f1e1..dd60e2317 100644 --- a/packages/mako/npm/linux-x64-gnu/package.json +++ b/packages/mako/npm/linux-x64-gnu/package.json @@ -1,6 +1,6 @@ { "name": "@umijs/mako-linux-x64-gnu", - "version": "0.9.4", + "version": "0.9.6-canary.20241107.1", "os": [ "linux" ], diff --git a/packages/mako/npm/linux-x64-musl/package.json b/packages/mako/npm/linux-x64-musl/package.json index ed1af64c0..c3a16f4fe 100644 --- a/packages/mako/npm/linux-x64-musl/package.json +++ b/packages/mako/npm/linux-x64-musl/package.json @@ -1,6 +1,6 @@ { "name": "@umijs/mako-linux-x64-musl", - "version": "0.9.4", + "version": "0.9.6-canary.20241107.1", "os": [ "linux" ], diff --git a/packages/mako/npm/win32-ia32-msvc/package.json b/packages/mako/npm/win32-ia32-msvc/package.json index 2307c4de2..ffb29cde6 100644 --- a/packages/mako/npm/win32-ia32-msvc/package.json +++ b/packages/mako/npm/win32-ia32-msvc/package.json @@ -1,6 +1,6 @@ { "name": "@umijs/mako-win32-ia32-msvc", - "version": "0.9.4", + "version": "0.9.6-canary.20241107.1", "os": [ "win32" ], diff --git a/packages/mako/npm/win32-x64-msvc/package.json b/packages/mako/npm/win32-x64-msvc/package.json index 9567b2937..dace51b64 100644 --- a/packages/mako/npm/win32-x64-msvc/package.json +++ b/packages/mako/npm/win32-x64-msvc/package.json @@ -1,6 +1,6 @@ { "name": "@umijs/mako-win32-x64-msvc", - "version": "0.9.4", + "version": "0.9.6-canary.20241107.1", "os": [ "win32" ], diff --git a/packages/mako/package.json b/packages/mako/package.json index f2fdc9e41..7312858ca 100644 --- a/packages/mako/package.json +++ b/packages/mako/package.json @@ -1,6 +1,6 @@ { "name": "@umijs/mako", - "version": "0.9.4", + "version": "0.9.6-canary.20241107.1", "main": "dist/index.js", "types": "dist/index.d.ts", "bin": { @@ -75,14 +75,14 @@ "src:build": "father build" }, "optionalDependencies": { - "@umijs/mako-darwin-arm64": "0.9.4", - "@umijs/mako-linux-arm64-gnu": "0.9.4", - "@umijs/mako-linux-arm64-musl": "0.9.4", - "@umijs/mako-win32-ia32-msvc": "0.9.4", - "@umijs/mako-darwin-x64": "0.9.4", - "@umijs/mako-win32-x64-msvc": "0.9.4", - "@umijs/mako-linux-x64-gnu": "0.9.4", - "@umijs/mako-linux-x64-musl": "0.9.4" + "@umijs/mako-darwin-arm64": "0.9.6-canary.20241107.1", + "@umijs/mako-linux-arm64-gnu": "0.9.6-canary.20241107.1", + "@umijs/mako-linux-arm64-musl": "0.9.6-canary.20241107.1", + "@umijs/mako-win32-ia32-msvc": "0.9.6-canary.20241107.1", + "@umijs/mako-darwin-x64": "0.9.6-canary.20241107.1", + "@umijs/mako-win32-x64-msvc": "0.9.6-canary.20241107.1", + "@umijs/mako-linux-x64-gnu": "0.9.6-canary.20241107.1", + "@umijs/mako-linux-x64-musl": "0.9.6-canary.20241107.1" }, "repository": "git@github.com:umijs/mako.git" } \ No newline at end of file From 3c79e020485c2c7876da94588ad93c7ed577dca1 Mon Sep 17 00:00:00 2001 From: pshu Date: Thu, 7 Nov 2024 11:42:06 +0800 Subject: [PATCH 11/11] chore: bundler-mako@0.9.6-canary.20241107.1 --- packages/bundler-mako/package.json | 2 +- pnpm-lock.yaml | 66 +++++++++++++++--------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/packages/bundler-mako/package.json b/packages/bundler-mako/package.json index 1570889b1..d514a9900 100644 --- a/packages/bundler-mako/package.json +++ b/packages/bundler-mako/package.json @@ -1,6 +1,6 @@ { "name": "@umijs/bundler-mako", - "version": "0.9.4", + "version": "0.9.6-canary.20241107.1", "dependencies": { "@umijs/bundler-utils": "^4.0.81", "@umijs/mako": "0.9.6-canary.20241107.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index dea4f6ff2..23607a65b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -436,7 +436,7 @@ importers: specifier: ^4.0.81 version: 4.1.6 '@umijs/mako': - specifier: 0.9.4 + specifier: 0.9.6-canary.20241107.1 version: link:../mako chalk: specifier: ^4.1.2 @@ -537,29 +537,29 @@ importers: version: 21.1.1 optionalDependencies: '@umijs/mako-darwin-arm64': - specifier: 0.9.4 - version: 0.9.4 + specifier: 0.9.6-canary.20241107.1 + version: 0.9.6-canary.20241107.1 '@umijs/mako-darwin-x64': - specifier: 0.9.4 - version: 0.9.4 + specifier: 0.9.6-canary.20241107.1 + version: 0.9.6-canary.20241107.1 '@umijs/mako-linux-arm64-gnu': - specifier: 0.9.4 - version: 0.9.4 + specifier: 0.9.6-canary.20241107.1 + version: 0.9.6-canary.20241107.1 '@umijs/mako-linux-arm64-musl': - specifier: 0.9.4 - version: 0.9.4 + specifier: 0.9.6-canary.20241107.1 + version: 0.9.6-canary.20241107.1 '@umijs/mako-linux-x64-gnu': - specifier: 0.9.4 - version: 0.9.4 + specifier: 0.9.6-canary.20241107.1 + version: 0.9.6-canary.20241107.1 '@umijs/mako-linux-x64-musl': - specifier: 0.9.4 - version: 0.9.4 + specifier: 0.9.6-canary.20241107.1 + version: 0.9.6-canary.20241107.1 '@umijs/mako-win32-ia32-msvc': - specifier: 0.9.4 - version: 0.9.4 + specifier: 0.9.6-canary.20241107.1 + version: 0.9.6-canary.20241107.1 '@umijs/mako-win32-x64-msvc': - specifier: 0.9.4 - version: 0.9.4 + specifier: 0.9.6-canary.20241107.1 + version: 0.9.6-canary.20241107.1 devDependencies: '@napi-rs/cli': specifier: ^2.18.0 @@ -6512,8 +6512,8 @@ packages: dev: true optional: true - /@umijs/mako-darwin-arm64@0.9.4: - resolution: {integrity: sha512-/4KbpIWQrmcV3t4DLdJHmKL5IwjDui78yYSc9QFOw/k6Ks/6IybnzJmMW3qNIEbKe9bHwPVtZKFCg/J0Yp91FA==} + /@umijs/mako-darwin-arm64@0.9.6-canary.20241107.1: + resolution: {integrity: sha512-60MdWwE5zMsCc9aHiWQKuKimYqN0A9NKcZ9fqJ5kAeK2m5UR4THRVji7VXJzx3dP7I1jv48RDEqX3L8tfmvS4A==} engines: {node: '>= 10'} cpu: [arm64] os: [darwin] @@ -6530,8 +6530,8 @@ packages: dev: true optional: true - /@umijs/mako-darwin-x64@0.9.4: - resolution: {integrity: sha512-jaZudFYwcqhAGApnUh1+LoQkTUqT1XfKvAbH1xDZWfn2cvQaQI69+n+RTnxkBlLyxWMgX9GfvcqaMewGzdNWpg==} + /@umijs/mako-darwin-x64@0.9.6-canary.20241107.1: + resolution: {integrity: sha512-FYlKCR3SeUStRcKSs/AxuC14XtjfI6DSS3/VvTKCGAVfYm1uNPUJI3h8HSYB2XWN16NquIp3Hzsa9YtqFQfxaQ==} engines: {node: '>= 10'} cpu: [x64] os: [darwin] @@ -6539,8 +6539,8 @@ packages: dev: false optional: true - /@umijs/mako-linux-arm64-gnu@0.9.4: - resolution: {integrity: sha512-KB6aFpt/SBiH+Wv8uzTzog56NndsRAK1SUDtCDrE9+r1BoCnTsS3iiI/tVGINqxcuwTrzqrOqX7MAJ4PBqifgA==} + /@umijs/mako-linux-arm64-gnu@0.9.6-canary.20241107.1: + resolution: {integrity: sha512-slpLbt6wQMVmb7BxmtxhZQct2IWjzzjy102J4eBpLKswPP1j1dJgEoPSAriHrN/9+BynOKkDA7PSbENNIYiodQ==} engines: {node: '>= 10'} cpu: [arm64] os: [linux] @@ -6548,8 +6548,8 @@ packages: dev: false optional: true - /@umijs/mako-linux-arm64-musl@0.9.4: - resolution: {integrity: sha512-EmBDhM6BYw0RIT23BQ9S8u2UI/r+vIOtHTwVfgAkzISZaxZ9swxbE0JU9UO+IySQDACrGTDIHIDROBp/MFXnVg==} + /@umijs/mako-linux-arm64-musl@0.9.6-canary.20241107.1: + resolution: {integrity: sha512-z/6UrxAKOsDJzaI8jPA1eIWfkBFQey46oV2aNsmCc527z2QiwKA7vxxqTcTPYII2I2uu4KqivluQThmWVEvv8g==} engines: {node: '>= 10'} cpu: [arm64] os: [linux] @@ -6566,8 +6566,8 @@ packages: dev: true optional: true - /@umijs/mako-linux-x64-gnu@0.9.4: - resolution: {integrity: sha512-ii2/GuXSSu81y8QqWa4FF5rZFzOxgOYs0txLPxLIsWT62RcYhTaR712hr/RqtCZAzq+ERW1+bUToMdkETVpMFw==} + /@umijs/mako-linux-x64-gnu@0.9.6-canary.20241107.1: + resolution: {integrity: sha512-PvW7V6eZul8Xq0wtcgM0WEAPUgHVyGDIx0bwhp/ywhhfTxOgBdDproR2B0if1SOX/965aBiyD7qJ5zwY4ijZew==} engines: {node: '>= 10'} cpu: [x64] os: [linux] @@ -6584,8 +6584,8 @@ packages: dev: true optional: true - /@umijs/mako-linux-x64-musl@0.9.4: - resolution: {integrity: sha512-Sz+IVq+wVJ8vCGRZmQpcrOKeOSt1ASf294lN9Q/052Auw/nGaBAmnbL152BiISKlEEZEs83eaZYj79LFHkXnNA==} + /@umijs/mako-linux-x64-musl@0.9.6-canary.20241107.1: + resolution: {integrity: sha512-CblTU/xPcb5rNsxrsbp16Dy6JuATWSTIs2cfQKqK1pfpHPtFeZHBzjlxQUPh37J2ZvN0npWGHZ2OjZ53Ml2LbA==} engines: {node: '>= 10'} cpu: [x64] os: [linux] @@ -6593,8 +6593,8 @@ packages: dev: false optional: true - /@umijs/mako-win32-ia32-msvc@0.9.4: - resolution: {integrity: sha512-Mm/89T0IKcJvb0Z5BtMy92vlI5dJV0/05zRdm+1iURyrzjPPIPSXwjXfPm9H9d3rQhUj8BZetF+CTuC6DrIqhA==} + /@umijs/mako-win32-ia32-msvc@0.9.6-canary.20241107.1: + resolution: {integrity: sha512-iMVRVeKlscTI/p9Zv/erbU8linVWuLbOgp/ZFhZPZNl0tBAbSPykWfM3JOdBdKlwBtarYuKJ16ZbnqbKDOj5YQ==} engines: {node: '>= 10'} cpu: [ia32] os: [win32] @@ -6602,8 +6602,8 @@ packages: dev: false optional: true - /@umijs/mako-win32-x64-msvc@0.9.4: - resolution: {integrity: sha512-lTWThplSiunRQrbOvyybTkqPzOuKkcTwqMfT8PdicCUCbCqvCwYx59MSpYW9rpRCpB9a7dYoR+u9LKZhtcoG7Q==} + /@umijs/mako-win32-x64-msvc@0.9.6-canary.20241107.1: + resolution: {integrity: sha512-JcEjWjBdlaHP2/wR7u4QHyehguISHE7hGmf+MjdAz8XKy8qmFoG1duYN+YIc4n003vlL7rsmAmE77ATVV4+J9Q==} engines: {node: '>= 10'} cpu: [x64] os: [win32]