Skip to content

Commit

Permalink
feat: ✨ add central ensure plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
stormslowly committed Nov 5, 2024
1 parent 7fe969d commit 1abdb37
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 66 deletions.
5 changes: 4 additions & 1 deletion crates/mako/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}),
Expand Down Expand Up @@ -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 {}));

Check warning on line 302 in crates/mako/src/compiler.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/compiler.rs#L302

Added line #L302 was not covered by tests
}

if let Some(minifish_config) = &config._minifish {
let inject = if let Some(inject) = &minifish_config.inject {
let mut map = HashMap::new();
Expand Down
2 changes: 1 addition & 1 deletion crates/mako/src/plugins.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
76 changes: 76 additions & 0 deletions crates/mako/src/plugins/central_ensure.rs
Original file line number Diff line number Diff line change
@@ -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 {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L13 was not covered by tests
"dev_ensure2"
}
fn runtime_plugins(&self, context: &Arc<Context>) -> anyhow::Result<Vec<String>> {
let mg = context

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L15-L17

Added lines #L15 - L17 were not covered by tests
.module_graph
.read()
.map_err(|e| anyhow!("Read_Module_Graph_error:\n{:?}", e))?;
let cg = context

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L20-L21

Added lines #L20 - L21 were not covered by tests
.chunk_graph
.read()
.map_err(|e| anyhow!("Read_Chunk_Graph_error:\n{:?}", e))?;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L24 was not covered by tests

let mut chunk_async_map: BTreeMap<String, Vec<String>> = Default::default();

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L26 was not covered by tests

mg.modules().iter().for_each(|module| {
let be_dynamic_imported = mg
.get_dependents(&module.id)

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L28-L30

Added lines #L28 - L30 were not covered by tests
.iter()
.any(|(_, dep)| dep.resolve_type.is_dynamic_esm());

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L32 was not covered by tests

if be_dynamic_imported {
cg.get_chunk_for_module(&module.id)

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L34-L35

Added lines #L34 - L35 were not covered by tests
.iter()
.for_each(|chunk| {
let deps_chunks = cg
.installable_descendants_chunk(&chunk.id)

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L37-L39

Added lines #L37 - L39 were not covered by tests
.iter()
.map(|chunk_id| chunk_id.generate(context))
.collect::<Vec<_>>();

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L41-L42

Added lines #L41 - L42 were not covered by tests

dbg!(&deps_chunks);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L44 was not covered by tests

chunk_async_map
.insert(generate_module_id(&module.id.id, context), deps_chunks);
});

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L46-L48

Added lines #L46 - L48 were not covered by tests
}
});

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L50 was not covered by tests

// TODO: compress the map to reduce duplicated chunk ids
let ensure_map = serde_json::to_string(&chunk_async_map)?;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L53 was not covered by tests

let runtime = format!(

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L55 was not covered by tests
r#"
(function(){{
let map = {ensure_map};
requireModule.updateEnsuire2Map = function(newMaping) {{

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

View workflow job for this annotation

GitHub Actions / Spell Check

"Maping" should be "Mapping".
map = newMaping;

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

View workflow job for this annotation

GitHub Actions / Spell Check

"Maping" should be "Mapping".
}}
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])
}

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

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/central_ensure.rs#L74-L75

Added lines #L74 - L75 were not covered by tests
}
64 changes: 0 additions & 64 deletions crates/mako/src/plugins/ensure_2.rs

This file was deleted.

0 comments on commit 1abdb37

Please sign in to comment.