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]) + } }