diff --git a/crates/mako/src/compiler.rs b/crates/mako/src/compiler.rs index 8bfebba60..e6e4b8a7d 100644 --- a/crates/mako/src/compiler.rs +++ b/crates/mako/src/compiler.rs @@ -237,7 +237,7 @@ impl Compiler { Arc::new(plugins::async_runtime::AsyncRuntimePlugin {}), Arc::new(plugins::emotion::EmotionPlugin {}), Arc::new(plugins::tree_shaking::FarmTreeShake {}), - Arc::new(plugins::loop_detector::LoopDetector {}), + Arc::new(plugins::detect_circular_dependence::LoopDetector {}), ]; plugins.extend(builtin_plugins); diff --git a/crates/mako/src/config/config.rs b/crates/mako/src/config/config.rs index de6536bb8..2ac6601ce 100644 --- a/crates/mako/src/config/config.rs +++ b/crates/mako/src/config/config.rs @@ -101,7 +101,7 @@ create_deserialize_fn!(deserialize_inline_css, InlineCssConfig); create_deserialize_fn!(deserialize_rsc_client, RscClientConfig); create_deserialize_fn!(deserialize_rsc_server, RscServerConfig); create_deserialize_fn!(deserialize_stats, StatsConfig); -create_deserialize_fn!(deserialize_detect_loop, DetectLoop); +create_deserialize_fn!(deserialize_detect_loop, DetectCircularDependence); #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "camelCase")] @@ -428,8 +428,8 @@ pub struct RscClientConfig { #[derive(Deserialize, Serialize, Debug)] #[serde(rename_all = "camelCase")] -pub struct DetectLoop { - pub ignore_node_modules: bool, +pub struct DetectCircularDependence { + pub ignores: Vec, pub graphviz: bool, } @@ -439,7 +439,7 @@ pub struct ExperimentalConfig { pub webpack_syntax_validate: Vec, pub require_context: bool, #[serde(deserialize_with = "deserialize_detect_loop")] - pub detect_loop: Option, + pub detect_circular_dependence: Option, } #[derive(Deserialize, Serialize, Debug)] @@ -715,7 +715,7 @@ const DEFAULT_CONFIG: &str = r#" "experimental": { "webpackSyntaxValidate": [], "requireContext": true, - "detectLoop": { "ignoreNodeModules": true, "graphviz": false } + "detectCircularDependence": { "ignores": ["node_modules"], "graphviz": false } }, "useDefineForClassFields": true, "emitDecoratorMetadata": false, diff --git a/crates/mako/src/plugins/loop_detector.rs b/crates/mako/src/plugins/detect_circular_dependence.rs similarity index 73% rename from crates/mako/src/plugins/loop_detector.rs rename to crates/mako/src/plugins/detect_circular_dependence.rs index 8ef1f4645..e22b7e072 100644 --- a/crates/mako/src/plugins/loop_detector.rs +++ b/crates/mako/src/plugins/detect_circular_dependence.rs @@ -12,21 +12,32 @@ pub struct LoopDetector {} impl Plugin for LoopDetector { fn name(&self) -> &str { - "loop_detector" + "loop_circular_dependence" } fn generate_begin(&self, context: &Arc) -> Result<()> { - if let Some(detect_loop) = &context.config.experimental.detect_loop + if let Some(detect_loop) = &context.config.experimental.detect_circular_dependence && !context.args.watch { let module_graph = context.module_graph.read().unwrap(); let (_, loops) = module_graph.toposort(); + let ignore_regexes = detect_loop + .ignores + .iter() + .map(|s| { + regex::Regex::new(s).map_err(|e| { + anyhow::anyhow!("Invalid regex: {} in detectCircularDependence#ignore", e) + }) + }) + .collect::>>()?; + let loop_lines = loops .iter() .filter(|ids| { - if detect_loop.ignore_node_modules { - !ids.iter().any(|id| id.id.contains("node_modules")) + if !ignore_regexes.is_empty() { + !ids.iter() + .any(|id| ignore_regexes.iter().any(|r| r.is_match(&id.id))) } else { true } @@ -52,7 +63,7 @@ impl Plugin for LoopDetector { if !loop_lines.is_empty() { for l in &loop_lines { - println!("{} Found a Dependency Loop: {}", "Warning".yellow(), l); + println!("{} Circular Dependencies: {}", "Warning".yellow(), l); } if detect_loop.graphviz { @@ -61,10 +72,7 @@ impl Plugin for LoopDetector { std::fs::write(context.root.join("_mako_loop_detector.dot"), dot)?; } } - - Ok(()) - } else { - Ok(()) } + Ok(()) } } diff --git a/crates/mako/src/plugins/mod.rs b/crates/mako/src/plugins/mod.rs index 78939714d..322dd987b 100644 --- a/crates/mako/src/plugins/mod.rs +++ b/crates/mako/src/plugins/mod.rs @@ -2,13 +2,13 @@ pub mod async_runtime; pub mod bundless_compiler; pub mod context_module; pub mod copy; +pub mod detect_circular_dependence; pub mod emotion; pub mod graphviz; pub mod hmr_runtime; pub mod ignore; pub mod import; pub mod invalid_webpack_syntax; -pub mod loop_detector; pub mod manifest; pub mod minifish; pub mod require_context; diff --git a/packages/bundler-mako/index.js b/packages/bundler-mako/index.js index 01b9e88ae..27ea2bfc4 100644 --- a/packages/bundler-mako/index.js +++ b/packages/bundler-mako/index.js @@ -624,6 +624,14 @@ async function getMakoConfig(opts) { plugins: opts.config.lessLoader?.plugins, }, analyze: analyze || process.env.ANALYZE ? {} : undefined, + experimental: { + webpackSyntaxValidate: [], + requireContext: true, + detectCircularDependence: { + ignores: ['node_modules', '\\.umi'], + graphviz: false, + }, + }, ...mako, };