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 17 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
1 change: 1 addition & 0 deletions crates/mako/src/config/experimental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::create_deserialize_fn;
pub struct ExperimentalConfig {
pub webpack_syntax_validate: Vec<String>,
pub require_context: bool,
pub magic_comment_chunk_name: bool,
xusd320 marked this conversation as resolved.
Show resolved Hide resolved
#[serde(deserialize_with = "deserialize_detect_loop")]
pub detect_circular_dependence: Option<DetectCircularDependence>,
}
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/config/mako.config.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"experimental": {
"webpackSyntaxValidate": [],
"requireContext": true,
"magicCommentChunkName": false,
"detectCircularDependence": {
"ignores": ["node_modules"],
"graphviz": false
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
Loading