-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support magic comment chunk name basically
- Loading branch information
Showing
18 changed files
with
283 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,11 @@ use tracing::debug; | |
|
||
use crate::ast::file::parse_path; | ||
use crate::compiler::Compiler; | ||
use crate::config::ChunkGroup; | ||
use crate::dev::update::UpdateResult; | ||
use crate::generate::chunk::{Chunk, ChunkId, ChunkType}; | ||
use crate::generate::chunk_graph::ChunkGraph; | ||
use crate::module::{ModuleId, ResolveType}; | ||
use crate::module::{generate_module_id, ModuleId, ResolveType}; | ||
|
||
pub type GroupUpdateResult = Option<(Vec<ChunkId>, Vec<(ModuleId, ChunkId, ChunkType)>)>; | ||
|
||
|
@@ -44,22 +45,33 @@ impl Compiler { | |
ChunkType::Entry(entry.clone(), entry_chunk_name.to_string(), false), | ||
&mut chunk_graph, | ||
vec![], | ||
&None, | ||
); | ||
let chunk_name = chunk.filename(); | ||
visited.insert(chunk.id.clone()); | ||
visited.insert((chunk.id.clone(), None)); | ||
edges.extend( | ||
[dynamic_dependencies.clone(), worker_dependencies.clone()] | ||
.concat() | ||
.into_iter() | ||
.map(|dep| (chunk.id.clone(), dep.generate(&self.context).into())), | ||
.map(|dep| { | ||
( | ||
chunk.id.clone(), | ||
match dep { | ||
(_, Some(chunk_group)) => { | ||
generate_module_id(chunk_group.name, &self.context).into() | ||
} | ||
(module_id, None) => module_id.generate(&self.context).into(), | ||
}, | ||
) | ||
}), | ||
); | ||
chunk_graph.add_chunk(chunk); | ||
|
||
/* A worker can self-spawn from it's source file, that will leads to a circular dependencies, | ||
* a real case is https://unpkg.com/browse/@antv/[email protected]/pkg-parallel/. | ||
* Memorize handled workers to avoid infinite resolving | ||
*/ | ||
let mut visited_workers = HashSet::<ModuleId>::new(); | ||
let mut visited_workers = HashSet::<(ModuleId, Option<ChunkGroup>)>::new(); | ||
|
||
// 抽离成两个函数处理动态依赖中可能有 worker 依赖、worker 依赖中可能有动态依赖的复杂情况 | ||
self.handle_dynamic_dependencies( | ||
|
@@ -88,29 +100,47 @@ impl Compiler { | |
fn handle_dynamic_dependencies( | ||
&self, | ||
chunk_name: &str, | ||
dynamic_dependencies: Vec<ModuleId>, | ||
visited: &HashSet<ModuleId>, | ||
dynamic_dependencies: Vec<(ModuleId, Option<ChunkGroup>)>, | ||
visited: &HashSet<(ModuleId, Option<ChunkGroup>)>, | ||
edges: &mut Vec<(ModuleId, ModuleId)>, | ||
chunk_graph: &mut ChunkGraph, | ||
visited_workers: &mut HashSet<ModuleId>, | ||
visited_workers: &mut HashSet<(ModuleId, Option<ChunkGroup>)>, | ||
) { | ||
visit_modules(dynamic_dependencies, Some(visited.clone()), |head| { | ||
let (chunk, dynamic_dependencies, mut worker_dependencies) = self.create_chunk( | ||
head, | ||
&head.0, | ||
ChunkType::Async, | ||
chunk_graph, | ||
vec![chunk_name.to_string()], | ||
&head.1, | ||
); | ||
|
||
worker_dependencies.retain(|w| !visited_workers.contains(w)); | ||
|
||
edges.extend( | ||
[dynamic_dependencies.clone(), worker_dependencies.clone()] | ||
.concat() | ||
.into_iter() | ||
.map(|dep| (chunk.id.clone(), dep.generate(&self.context).into())), | ||
); | ||
chunk_graph.add_chunk(chunk); | ||
if let Some(exited_chunk) = chunk_graph.mut_chunk(&chunk.id) { | ||
chunk | ||
.modules | ||
.iter() | ||
.for_each(|m| exited_chunk.add_module(m.clone())); | ||
} else { | ||
edges.extend( | ||
[dynamic_dependencies.clone(), worker_dependencies.clone()] | ||
.concat() | ||
.into_iter() | ||
.map(|dep| { | ||
( | ||
chunk.id.clone(), | ||
match dep { | ||
(_, Some(chunk_group)) => { | ||
generate_module_id(chunk_group.name, &self.context).into() | ||
} | ||
(module_id, None) => module_id.generate(&self.context).into(), | ||
}, | ||
) | ||
}), | ||
); | ||
chunk_graph.add_chunk(chunk); | ||
} | ||
|
||
self.handle_worker_dependencies( | ||
chunk_name, | ||
|
@@ -128,18 +158,19 @@ impl Compiler { | |
fn handle_worker_dependencies( | ||
&self, | ||
chunk_name: &str, | ||
worker_dependencies: Vec<ModuleId>, | ||
visited: &HashSet<ModuleId>, | ||
worker_dependencies: Vec<(ModuleId, Option<ChunkGroup>)>, | ||
visited: &HashSet<(ModuleId, Option<ChunkGroup>)>, | ||
edges: &mut Vec<(ModuleId, ModuleId)>, | ||
chunk_graph: &mut ChunkGraph, | ||
visited_workers: &mut HashSet<ModuleId>, | ||
visited_workers: &mut HashSet<(ModuleId, Option<ChunkGroup>)>, | ||
) { | ||
visit_modules(worker_dependencies, Some(visited.clone()), |head| { | ||
let (chunk, dynamic_dependencies, mut worker_dependencies) = self.create_chunk( | ||
head, | ||
ChunkType::Worker(head.clone()), | ||
&head.0, | ||
ChunkType::Worker(head.0.clone()), | ||
chunk_graph, | ||
vec![chunk_name.to_string()], | ||
&head.1, | ||
); | ||
|
||
worker_dependencies.retain(|w| !visited_workers.contains(w)); | ||
|
@@ -148,7 +179,17 @@ impl Compiler { | |
[dynamic_dependencies.clone(), worker_dependencies.clone()] | ||
.concat() | ||
.into_iter() | ||
.map(|dep| (chunk.id.clone(), dep.generate(&self.context).into())), | ||
.map(|dep| { | ||
( | ||
chunk.id.clone(), | ||
match dep { | ||
(_, Some(chunk_group)) => { | ||
generate_module_id(chunk_group.name, &self.context).into() | ||
} | ||
(module_id, None) => module_id.generate(&self.context).into(), | ||
}, | ||
) | ||
}), | ||
); | ||
|
||
chunk_graph.add_chunk(chunk); | ||
|
@@ -238,13 +279,17 @@ impl Compiler { | |
let async_chunk_modules = update_result | ||
.added | ||
.iter() | ||
.filter(|module_id| { | ||
.filter_map(|module_id| { | ||
module_graph | ||
.get_dependents(module_id) | ||
.iter() | ||
.any(|(_, dep)| dep.resolve_type == ResolveType::DynamicImport) | ||
.find_map(|(_, dep)| match &dep.resolve_type { | ||
ResolveType::DynamicImport(chunk_group) => { | ||
Some((module_id.clone(), chunk_group.clone())) | ||
} | ||
_ => None, | ||
}) | ||
}) | ||
.cloned() | ||
.collect::<_>(); | ||
|
||
// create chunk for added async module | ||
|
@@ -315,8 +360,10 @@ impl Compiler { | |
.get_dependencies(head) | ||
.into_iter() | ||
.filter(|(_, dep)| { | ||
dep.resolve_type != ResolveType::DynamicImport | ||
&& dep.resolve_type != ResolveType::Worker | ||
!matches!( | ||
dep.resolve_type, | ||
ResolveType::DynamicImport(_) | ResolveType::Worker(_) | ||
) | ||
}) | ||
.collect::<Vec<_>>(); | ||
let mut next_module_ids = vec![]; | ||
|
@@ -376,32 +423,41 @@ impl Compiler { | |
}) | ||
} | ||
|
||
#[allow(clippy::type_complexity)] | ||
fn create_chunk( | ||
&self, | ||
entry_module_id: &ModuleId, | ||
chunk_id: &ChunkId, | ||
chunk_type: ChunkType, | ||
chunk_graph: &mut ChunkGraph, | ||
shared_chunk_names: Vec<String>, | ||
) -> (Chunk, Vec<ModuleId>, Vec<ModuleId>) { | ||
crate::mako_profile_function!(&entry_module_id.id); | ||
chunk_group: &Option<ChunkGroup>, | ||
) -> ( | ||
Chunk, | ||
Vec<(ModuleId, Option<ChunkGroup>)>, | ||
Vec<(ModuleId, Option<ChunkGroup>)>, | ||
) { | ||
crate::mako_profile_function!(&chunk_id.id); | ||
let mut dynamic_entries = vec![]; | ||
let mut worker_entries = vec![]; | ||
|
||
let chunk_id = entry_module_id.generate(&self.context); | ||
let mut chunk = Chunk::new(chunk_id.into(), chunk_type.clone()); | ||
let chunk_id_str = match chunk_group { | ||
Some(chunk_group) => generate_module_id(chunk_group.name.clone(), &self.context), | ||
None => chunk_id.generate(&self.context), | ||
}; | ||
let mut chunk = Chunk::new(chunk_id_str.into(), chunk_type.clone()); | ||
|
||
let module_graph = self.context.module_graph.read().unwrap(); | ||
|
||
let mut chunk_deps = visit_modules(vec![entry_module_id.clone()], None, |head| { | ||
let mut chunk_deps = visit_modules(vec![chunk_id.clone()], None, |head| { | ||
let mut next_module_ids = vec![]; | ||
|
||
for (dep_module_id, dep) in module_graph.get_dependencies(head) { | ||
match dep.resolve_type { | ||
ResolveType::DynamicImport => { | ||
dynamic_entries.push(dep_module_id.clone()); | ||
match &dep.resolve_type { | ||
ResolveType::DynamicImport(chunk_group) => { | ||
dynamic_entries.push((dep_module_id.clone(), chunk_group.clone())); | ||
} | ||
ResolveType::Worker => { | ||
worker_entries.push(dep_module_id.clone()); | ||
ResolveType::Worker(chunk_group) => { | ||
worker_entries.push((dep_module_id.clone(), chunk_group.clone())); | ||
} | ||
// skip shared modules from entry chunks, but except worker chunk modules | ||
_ if matches!(chunk_type, ChunkType::Worker(_)) | ||
|
@@ -430,7 +486,7 @@ impl Compiler { | |
|
||
fn create_update_async_chunks( | ||
&self, | ||
async_module_ids: Vec<ModuleId>, | ||
async_module_ids: Vec<(ModuleId, Option<ChunkGroup>)>, | ||
chunk_graph: &mut ChunkGraph, | ||
shared_chunk_names: Vec<String>, | ||
) -> Vec<ChunkId> { | ||
|
@@ -439,10 +495,11 @@ impl Compiler { | |
|
||
visit_modules(async_module_ids, None, |head| { | ||
let (new_chunk, dynamic_dependencies, worker_dependencies) = self.create_chunk( | ||
head, | ||
&head.0, | ||
ChunkType::Async, | ||
chunk_graph, | ||
shared_chunk_names.clone(), | ||
&head.1, | ||
); | ||
let chunk_id = new_chunk.id.clone(); | ||
|
||
|
@@ -454,11 +511,16 @@ impl Compiler { | |
.map(|dep| { | ||
( | ||
chunk_id.clone(), | ||
match chunk_graph.get_chunk_for_module(&dep) { | ||
match chunk_graph.get_chunk_for_module(&dep.0) { | ||
// ref existing chunk | ||
Some(chunk) => chunk.id.clone(), | ||
// ref new chunk | ||
None => dep.generate(&self.context).into(), | ||
None => match dep { | ||
(_, Some(chunk_group)) => { | ||
generate_module_id(chunk_group.name, &self.context).into() | ||
} | ||
(module_id, None) => module_id.generate(&self.context).into(), | ||
}, | ||
}, | ||
) | ||
}), | ||
|
@@ -469,8 +531,8 @@ impl Compiler { | |
// continue to visit non-existing dynamic dependencies | ||
dynamic_dependencies | ||
.into_iter() | ||
.filter(|dep| chunk_graph.get_chunk_for_module(dep).is_none()) | ||
.collect::<Vec<ModuleId>>() | ||
.filter(|dep| chunk_graph.get_chunk_for_module(&dep.0).is_none()) | ||
.collect::<Vec<(ModuleId, Option<ChunkGroup>)>>() | ||
}); | ||
|
||
// add edges | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.