Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: magic comment chunk name #1628

Merged
merged 18 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/mako/src/ast/js_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl JsAst {
}

pub fn analyze_deps(&self, context: Arc<Context>) -> Vec<Dependency> {
let mut visitor = DepAnalyzer::new(self.unresolved_mark);
let mut visitor = DepAnalyzer::new(self.unresolved_mark, context.clone());
GLOBALS.set(&context.meta.script.globals, || {
self.ast.visit_with(&mut visitor);
visitor.dependencies
Expand Down
33 changes: 8 additions & 25 deletions crates/mako/src/dev/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::debug;
use crate::build::BuildError;
use crate::compiler::Compiler;
use crate::generate::transform::transform_modules;
use crate::module::{Dependency, Module, ModuleId, ResolveTypeFlags};
use crate::module::{Dependency, Module, ModuleId};
use crate::module_graph::ModuleGraph;
use crate::resolve::{self, clear_resolver_cache};

Expand Down Expand Up @@ -451,33 +451,16 @@ impl Diff {
return true;
}

let new_deps = new_dependencies
let new_deps: HashMap<&ModuleId, &Dependency> = new_dependencies
.iter()
.fold(HashMap::new(), |mut map, (module_id, dep)| {
let flag: ResolveTypeFlags = (&dep.resolve_type).into();

map.entry(module_id.clone())
.and_modify(|e: &mut ResolveTypeFlags| {
e.insert(flag);
})
.or_insert(flag);
map
});
.map(|(module_id, dep)| (module_id, dep))
.collect();

let original = module_graph.get_dependencies(module_id).into_iter().fold(
HashMap::new(),
|mut map, (module_id, dep)| {
let flag: ResolveTypeFlags = (&dep.resolve_type).into();
map.entry(module_id.clone())
.and_modify(|e: &mut ResolveTypeFlags| e.insert(flag))
.or_insert(flag);
map
},
);
let original: HashMap<&ModuleId, &Dependency> = module_graph
.get_dependencies(module_id)
.into_iter()
.collect();

// there is an edge case we need to consider later
// if an edge ResolveTypeFlag(Sync + Async) changes to ResolveTypeFlag(Sync), is it
// changed nor not ?
!new_deps.eq(&original)
}
}
Expand Down
14 changes: 14 additions & 0 deletions crates/mako/src/generate/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ use crate::module::ModuleId;
use crate::module_graph::ModuleGraph;
use crate::utils::url_safe_base64_encode;

// TODO: Refact ChunkId
/*
* ChunkId and ModuleId is not a same thing. For example, like below codes:
* import(/* webpackChunkName: "myChunk" */, './lazy');
* the chunkId should be "myChunk", not be moduleId of "./lazy".
* We need a struct to store more chunk info, it may be like:
* struct {
* identifier: JsWord,
* root_module: ModuleId,
* import_options: import_options {
* chunk_name: Option<String>
* }
* }
* */
pub type ChunkId = ModuleId;

#[derive(Clone, PartialEq, Eq, Debug)]
Expand Down
4 changes: 1 addition & 3 deletions crates/mako/src/generate/chunk_pot/ast_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ pub(crate) fn render_css_chunk(
&& matches!(info.ast, crate::module::ModuleAst::Css(_))
{
let relative_source = diff_paths(&module_id.id, &context.root)
.unwrap_or((&module_id.id).into())
.to_string_lossy()
.to_string();
.map_or(module_id.id.clone(), |p| p.to_string_lossy().to_string());

chain_map.insert(
relative_source,
Expand Down
153 changes: 107 additions & 46 deletions crates/mako/src/generate/group_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::compiler::Compiler;
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, ImportOptions, ModuleId, ResolveType};

pub type GroupUpdateResult = Option<(Vec<ChunkId>, Vec<(ModuleId, ChunkId, ChunkType)>)>;

Expand Down Expand Up @@ -44,22 +44,33 @@ impl Compiler {
ChunkType::Entry(entry.clone(), entry_chunk_name.to_string(), false),
&mut chunk_graph,
vec![],
&ImportOptions::default(),
);
let chunk_name = chunk.filename();
visited.insert(chunk.id.clone());
visited.insert((chunk.id.clone(), ImportOptions::default()));
edges.extend(
[dynamic_dependencies.clone(), worker_dependencies.clone()]
.concat()
.into_iter()
.map(|dep| (chunk.id.clone(), dep.generate(&self.context).into())),
.iter()
.map(|dep| {
(
chunk.id.clone(),
match dep.1.get_chunk_name() {
Some(chunk_name) => {
generate_module_id(chunk_name, &self.context).into()
}
None => dep.0.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, ImportOptions)>::new();

// 抽离成两个函数处理动态依赖中可能有 worker 依赖、worker 依赖中可能有动态依赖的复杂情况
self.handle_dynamic_dependencies(
Expand Down Expand Up @@ -88,29 +99,47 @@ impl Compiler {
fn handle_dynamic_dependencies(
&self,
chunk_name: &str,
dynamic_dependencies: Vec<ModuleId>,
visited: &HashSet<ModuleId>,
dynamic_dependencies: Vec<(ModuleId, ImportOptions)>,
visited: &HashSet<(ModuleId, ImportOptions)>,
edges: &mut Vec<(ModuleId, ModuleId)>,
chunk_graph: &mut ChunkGraph,
visited_workers: &mut HashSet<ModuleId>,
visited_workers: &mut HashSet<(ModuleId, ImportOptions)>,
) {
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()
.iter()
.map(|dep| {
(
chunk.id.clone(),
match dep.1.get_chunk_name() {
Some(chunk_name) => {
generate_module_id(chunk_name, &self.context).into()
}
None => dep.0.generate(&self.context).into(),
},
)
}),
);
chunk_graph.add_chunk(chunk);
}

self.handle_worker_dependencies(
chunk_name,
Expand All @@ -128,27 +157,38 @@ impl Compiler {
fn handle_worker_dependencies(
&self,
chunk_name: &str,
worker_dependencies: Vec<ModuleId>,
visited: &HashSet<ModuleId>,
worker_dependencies: Vec<(ModuleId, ImportOptions)>,
visited: &HashSet<(ModuleId, ImportOptions)>,
edges: &mut Vec<(ModuleId, ModuleId)>,
chunk_graph: &mut ChunkGraph,
visited_workers: &mut HashSet<ModuleId>,
visited_workers: &mut HashSet<(ModuleId, ImportOptions)>,
) {
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));

edges.extend(
[dynamic_dependencies.clone(), worker_dependencies.clone()]
.concat()
.into_iter()
.map(|dep| (chunk.id.clone(), dep.generate(&self.context).into())),
.iter()
.map(|dep| {
(
chunk.id.clone(),
match dep.1.get_chunk_name() {
Some(chunk_name) => {
generate_module_id(chunk_name, &self.context).into()
}
None => dep.0.generate(&self.context).into(),
},
)
}),
);

chunk_graph.add_chunk(chunk);
Expand Down Expand Up @@ -238,13 +278,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(import_options) => {
Some((module_id.clone(), import_options.clone()))
}
_ => None,
})
})
.cloned()
.collect::<_>();

// create chunk for added async module
Expand Down Expand Up @@ -315,8 +359,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(_)
)
xusd320 marked this conversation as resolved.
Show resolved Hide resolved
})
.collect::<Vec<_>>();
let mut next_module_ids = vec![];
Expand Down Expand Up @@ -376,32 +422,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);
import_options: &ImportOptions,
) -> (
Chunk,
Vec<(ModuleId, ImportOptions)>,
Vec<(ModuleId, ImportOptions)>,
) {
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 import_options.get_chunk_name() {
Some(chunk_name) => generate_module_id(chunk_name, &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(_))
Expand Down Expand Up @@ -430,7 +485,7 @@ impl Compiler {

fn create_update_async_chunks(
&self,
async_module_ids: Vec<ModuleId>,
async_module_ids: Vec<(ModuleId, ImportOptions)>,
chunk_graph: &mut ChunkGraph,
shared_chunk_names: Vec<String>,
) -> Vec<ChunkId> {
Expand All @@ -439,26 +494,32 @@ 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,
xusd320 marked this conversation as resolved.
Show resolved Hide resolved
);
let chunk_id = new_chunk.id.clone();

// record edges and add chunk to graph
edges.extend(
[dynamic_dependencies.clone(), worker_dependencies.clone()]
.concat()
.into_iter()
.iter()
.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.1.get_chunk_name() {
Some(chunk_name) => {
generate_module_id(chunk_name, &self.context).into()
}
None => dep.0.generate(&self.context).into(),
},
},
)
}),
Expand All @@ -469,8 +530,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, ImportOptions)>>()
});

// add edges
Expand Down
Loading
Loading