Skip to content

Commit

Permalink
refactor: 🎨 extract hmr runtime update code aspect
Browse files Browse the repository at this point in the history
  • Loading branch information
stormslowly committed Nov 7, 2024
1 parent 8609332 commit 61bedb0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
17 changes: 6 additions & 11 deletions crates/mako/src/generate/hmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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

Check warning on line 28 in crates/mako/src/generate/hmr.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/hmr.rs#L26-L28

Added lines #L26 - L28 were not covered by tests
.plugin_driver
.hmr_runtime_update_code(&self.context)?,
];

Check warning on line 31 in crates/mako/src/generate/hmr.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/hmr.rs#L30-L31

Added lines #L30 - L31 were not covered by tests

let content = content
.replace("__CHUNK_ID__", &chunk.id.id)
Expand Down
12 changes: 12 additions & 0 deletions crates/mako/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ pub trait Plugin: Any + Send + Sync {
Ok(Vec::new())
}

fn hmr_runtime_updates(&self, _context: &Arc<Context>) -> Result<Vec<String>> {
Ok(Vec::new())
}

Check warning on line 164 in crates/mako/src/plugin.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugin.rs#L162-L164

Added lines #L162 - L164 were not covered by tests

fn optimize_module_graph(
&self,
_module_graph: &mut ModuleGraph,
Expand Down Expand Up @@ -368,6 +372,14 @@ impl PluginDriver {
Ok(plugins.join("\n"))
}

pub fn hmr_runtime_update_code(&self, context: &Arc<Context>) -> Result<String> {
let mut plugins = Vec::new();
for plugin in &self.plugins {
plugins.extend(plugin.hmr_runtime_updates(context)?);

Check warning on line 378 in crates/mako/src/plugin.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugin.rs#L375-L378

Added lines #L375 - L378 were not covered by tests
}
Ok(plugins.join("\n"))
}

Check warning on line 381 in crates/mako/src/plugin.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugin.rs#L380-L381

Added lines #L380 - L381 were not covered by tests

pub fn optimize_module_graph(
&self,
module_graph: &mut ModuleGraph,
Expand Down
11 changes: 11 additions & 0 deletions crates/mako/src/plugins/central_ensure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,15 @@ impl Plugin for CentralChunkEnsure {

Ok(vec![runtime])
}

Check warning on line 78 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L77-L78

Added lines #L77 - L78 were not covered by tests

fn hmr_runtime_updates(&self, _context: &Arc<Context>) -> anyhow::Result<Vec<String>> {
let map = module_ensure_map(_context)?;

Check warning on line 81 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L80-L81

Added lines #L80 - L81 were not covered by tests

let update_mapping = format!(

Check warning on line 83 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L83

Added line #L83 was not covered by tests
"runtime.updateEnsure2Map({});",
serde_json::to_string(&map)?

Check warning on line 85 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L85

Added line #L85 was not covered by tests
);

Ok(vec![update_mapping])
}

Check warning on line 89 in crates/mako/src/plugins/central_ensure.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L88-L89

Added lines #L88 - L89 were not covered by tests
}

0 comments on commit 61bedb0

Please sign in to comment.