Skip to content

Commit

Permalink
refactor: 🎨 add back chunk id in replace info
Browse files Browse the repository at this point in the history
  • Loading branch information
stormslowly committed Nov 6, 2024
1 parent beae8ce commit 8cef532
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
6 changes: 6 additions & 0 deletions crates/mako/src/generate/chunk_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 67 in crates/mako/src/generate/chunk_graph.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/chunk_graph.rs#L66-L67

Added lines #L66 - L67 were not covered by tests
.node_weights()
.find(|c| c.has_module(module_id) && matches!(c.chunk_type, ChunkType::Async))
}

Check warning on line 70 in crates/mako/src/generate/chunk_graph.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/chunk_graph.rs#L69-L70

Added lines #L69 - L70 were not covered by tests

// pub fn get_chunk_by_id(&self, id: &String) -> Option<&Chunk> {
// self.graph.node_weights().find(|c| c.id.id.eq(id))
// }
Expand Down
35 changes: 27 additions & 8 deletions crates/mako/src/generate/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,42 @@ pub fn transform_modules_in_thread(
let mut resolved_deps: HashMap<String, ResolvedReplaceInfo> = 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),

Check warning on line 94 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L91-L94

Added lines #L91 - L94 were not covered by tests
};
let chunk_graph = context.chunk_graph.read().unwrap();

Check warning on line 96 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L96

Added line #L96 was not covered by tests
chunk_graph.chunk(&chunk_id.into()).unwrap().filename()
let chunk_name =
chunk_graph.chunk(&chunk_id.into()).unwrap().filename();

Check warning on line 98 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L98

Added line #L98 was not covered by tests

ResolvedReplaceInfo {
chunk_id: None,

Check warning on line 101 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L100-L101

Added lines #L100 - L101 were not covered by tests
to_replace_source: chunk_name,
resolved_module_id: id.clone(),

Check warning on line 103 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L103

Added line #L103 was not covered by tests
}
}
_ => 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),

Check warning on line 109 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L105-L109

Added lines #L105 - L109 were not covered by tests
});

let resolve_replace_info = ResolvedReplaceInfo {
to_replace_source,
resolved_module_id: id.clone(),
ResolvedReplaceInfo {

Check warning on line 112 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L112

Added line #L112 was not covered by tests
chunk_id,
to_replace_source: id.generate(&context),
resolved_module_id: id.clone(),

Check warning on line 115 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L114-L115

Added lines #L114 - L115 were not covered by tests
}
}

Check warning on line 117 in crates/mako/src/generate/transform.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/generate/transform.rs#L117

Added line #L117 was not covered by tests
_ => 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);
Expand Down Expand Up @@ -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,
},
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/plugins/bundless_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl BundlessCompiler {
Ok((
dep.source.clone(),
ResolvedReplaceInfo {
chunk_id: None,
to_replace_source: replacement,
resolved_module_id: id.clone(),

Check warning on line 88 in crates/mako/src/plugins/bundless_compiler.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/plugins/bundless_compiler.rs#L83-L88

Added lines #L83 - L88 were not covered by tests
},
Expand Down
2 changes: 1 addition & 1 deletion crates/mako/src/plugins/central_ensure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn module_ensure_map(context: &Arc<Context>) -> anyhow::Result<BTreeMap<Stri
.any(|(_, dep)| dep.resolve_type.is_dynamic_esm());

Check warning on line 28 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

Added line #L28 was not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L30 - L31 were not covered by tests
.iter()
.for_each(|chunk| {
let deps_chunks = cg
Expand Down
2 changes: 2 additions & 0 deletions crates/mako/src/visitors/dep_replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct DepReplacer<'a> {

#[derive(Debug, Clone)]

Check warning on line 26 in crates/mako/src/visitors/dep_replacer.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/visitors/dep_replacer.rs#L26

Added line #L26 was not covered by tests
pub struct ResolvedReplaceInfo {
pub chunk_id: Option<String>,
pub to_replace_source: String,
pub resolved_module_id: ModuleId,

Check warning on line 30 in crates/mako/src/visitors/dep_replacer.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/visitors/dep_replacer.rs#L28-L30

Added lines #L28 - L30 were not covered by tests
}
Expand Down Expand Up @@ -347,6 +348,7 @@ try {
hashmap! {
key.to_string() =>
ResolvedReplaceInfo {
chunk_id: None,
to_replace_source: module_id.into(),
resolved_module_id: "".into(),
}
Expand Down
13 changes: 8 additions & 5 deletions crates/mako/src/visitors/dynamic_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<'a> VisitMut for DynamicImport<'a> {
{
self.central_ensure(&generated_module_id)

Check warning on line 119 in crates/mako/src/visitors/dynamic_import.rs

View check run for this annotation

Codecov / codecov/patch

crates/mako/src/visitors/dynamic_import.rs#L119

Added line #L119 was not covered by tests
} else {
self.inline_ensure(&resolved_info.resolved_module_id, &self.context)
self.inline_ensure(resolved_info, &self.context)
};

let lazy_require_call =
Expand Down Expand Up @@ -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<Context>) -> Expr {
fn inline_ensure(&self, replace_info: &ResolvedReplaceInfo, context: &Arc<Context>) -> 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) => {
[
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 8cef532

Please sign in to comment.