Skip to content

Commit

Permalink
feat: support WarnCaseSensitiveModulesPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
OceanPresentChao committed Oct 21, 2023
1 parent 3c05152 commit be1a51d
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 2 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/rspack_binding_options/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ rspack_plugin_split_chunks = { path = "../rspack_plugin_split_chunk
rspack_plugin_split_chunks_new = { path = "../rspack_plugin_split_chunks_new" }
rspack_plugin_swc_css_minimizer = { path = "../rspack_plugin_swc_css_minimizer" }
rspack_plugin_swc_js_minimizer = { path = "../rspack_plugin_swc_js_minimizer" }
rspack_plugin_warn_sensitive_module = { path = "../rspack_plugin_warn_sensitive_module" }
rspack_plugin_wasm = { path = "../rspack_plugin_wasm" }
rspack_plugin_worker = { path = "../rspack_plugin_worker" }
rspack_regex = { path = "../rspack_regex" }
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_binding_options/src/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ impl RawOptionsApply for RawOptions {

plugins.push(rspack_plugin_ensure_chunk_conditions::EnsureChunkConditionsPlugin.boxed());

plugins.push(rspack_plugin_warn_sensitive_module::WarnCaseSensitiveModulesPlugin.boxed());

Ok(Self::Options {
context,
mode,
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_ids/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod deterministic_module_ids_plugin;
pub use deterministic_module_ids_plugin::*;
mod named_module_ids_plugin;
pub use named_module_ids_plugin::*;
pub(crate) mod id_helpers;
pub mod id_helpers;
mod named_chunk_ids_plugin;
pub use named_chunk_ids_plugin::*;
mod stable_named_chunk_ids_plugin;
Expand Down
11 changes: 11 additions & 0 deletions crates/rspack_plugin_warn_sensitive_module/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
edition = "2021"
license = "MIT"
name = "rspack_plugin_warn_sensitive_module"
repository = "https://github.com/web-infra-dev/rspack"
version = "0.1.0"

[dependencies]
dashmap = { workspace = true }
rspack_core = { path = "../rspack_core" }
rspack_error = { path = "../rspack_error" }
96 changes: 96 additions & 0 deletions crates/rspack_plugin_warn_sensitive_module/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// https://github.com/webpack/webpack/blob/main/lib/WarnCaseSensitiveModulesPlugin.js

use std::collections::HashMap;

use dashmap::DashSet;
use rspack_core::{Compilation, Logger, Module, ModuleGraph, Plugin};
use rspack_error::Diagnostic;

#[derive(Debug)]
pub struct WarnCaseSensitiveModulesPlugin;

impl WarnCaseSensitiveModulesPlugin {
pub fn new() -> Self {
Self
}

pub fn create_sensitive_modules_warning(
&self,
modules: &Vec<&&Box<dyn Module>>,
graph: &ModuleGraph,
) -> String {
let mut message =
format!("There are multiple modules with names that only differ in casing.\n");

for m in modules {
let mut module_msg = format!(" - {}\n", m.identifier().to_string());
graph.get_incoming_connections(m).iter().for_each(|c| {
if let Some(original_identifier) = c.original_module_identifier.clone() {
module_msg.push_str(&format!(" - used by {}\n", original_identifier));
}
});
message.push_str(&module_msg);
}

message
}
}

// This Plugin warns when there are case sensitive modules in the compilation
// which can cause unexpected behavior when deployed on a case-insensitive environment
// it is executed in hook `compilation.seal`
impl Plugin for WarnCaseSensitiveModulesPlugin {
fn name(&self) -> &'static str {
"rspack.WarnCaseSensitiveModulesPlugin"
}

fn module_ids(&self, compilation: &mut Compilation) -> rspack_error::Result<()> {
let logger = compilation.get_logger(self.name());
let start = logger.time("check case sensitive modules");
let diagnostics: DashSet<Diagnostic> = DashSet::default();
let modules = compilation
.module_graph
.modules()
.values()
.collect::<Vec<_>>();
let mut module_without_case_map: HashMap<String, HashMap<String, &Box<dyn Module>>> =
HashMap::new();

for module in modules {
// Ignore `data:` URLs, because it's not a real path
if let Some(normal_module) = module.as_normal_module() {
if normal_module
.resource_resolved_data()
.encoded_content
.is_some()
{
continue;
}
}

let identifier = module.identifier().to_string();
let lower_identifier = identifier.to_lowercase();
let lower_map = module_without_case_map
.entry(lower_identifier)
.or_insert(HashMap::new());
lower_map.insert(identifier, module);
}

for lower_map in module_without_case_map.values() {
if lower_map.len() > 1 {
let mut modules = lower_map.values().collect::<Vec<_>>();
modules.sort_by_key(|m| m.identifier());
diagnostics.insert(Diagnostic::warn(
"Sensitive Modules Warn".to_string(),
self.create_sensitive_modules_warning(&modules, &compilation.module_graph),
0,
0,
));
}
}

compilation.push_batch_diagnostic(diagnostics.into_iter().collect());
logger.time_end(start);
Ok(())
}
}
1 change: 1 addition & 0 deletions crates/rspack_testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ rspack_plugin_json = { path = "../rspack_plugin_json" }
rspack_plugin_library = { path = "../rspack_plugin_library" }
rspack_plugin_remove_empty_chunks = { path = "../rspack_plugin_remove_empty_chunks" }
rspack_plugin_runtime = { path = "../rspack_plugin_runtime" }
rspack_plugin_warn_sensitive_module = { path = "../rspack_plugin_warn_sensitive_module" }
rspack_plugin_wasm = { path = "../rspack_plugin_wasm" }
rspack_regex = { path = "../rspack_regex" }
rspack_tracing = { path = "../rspack_tracing" }
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_testing/src/test_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ impl TestConfig {
// Notice the plugin need to be placed after SplitChunksPlugin
plugins.push(rspack_plugin_remove_empty_chunks::RemoveEmptyChunksPlugin.boxed());

plugins.push(rspack_plugin_warn_sensitive_module::WarnCaseSensitiveModulesPlugin.boxed());

plugins.push(rspack_plugin_javascript::InferAsyncModulesPlugin {}.boxed());
if self.experiments.async_web_assembly {
plugins.push(rspack_plugin_wasm::FetchCompileAsyncWasmPlugin {}.boxed());
Expand Down
2 changes: 1 addition & 1 deletion webpack-test/cases/errors/case-sensitive/test.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ module.exports = function(config) {
return fs.existsSync(path.join(__dirname, "TEST.FILTER.JS"));
};
*/
module.exports = () => {return "https://github.com/web-infra-dev/rspack/issues/4347"}
module.exports = () => true

0 comments on commit be1a51d

Please sign in to comment.