Skip to content

Commit

Permalink
perf: WarnCaseSensitiveModulesPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Dec 12, 2024
1 parent 532a15d commit 64093b6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
1 change: 1 addition & 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_plugin_warn_sensitive_module/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ version = "0.2.0"

[dependencies]
cow-utils = { workspace = true }
rayon = { workspace = true }
rspack_collections = { workspace = true }
rspack_core = { workspace = true }
rspack_error = { workspace = true }
Expand Down
47 changes: 27 additions & 20 deletions crates/rspack_plugin_warn_sensitive_module/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// https://github.com/webpack/webpack/blob/main/lib/WarnCaseSensitiveModulesPlugin.js

use std::collections::HashMap;
use std::{borrow::Cow, collections::HashMap};

use cow_utils::CowUtils;
use rayon::prelude::*;
use rspack_collections::{Identifier, IdentifierSet};
use rspack_core::{
ApplyContext, Compilation, CompilationSeal, CompilerOptions, Logger, ModuleGraph, Plugin,
Expand Down Expand Up @@ -52,33 +53,39 @@ async fn seal(&self, compilation: &mut Compilation) -> Result<()> {
let start = logger.time("check case sensitive modules");
let mut diagnostics: Vec<Diagnostic> = vec![];
let module_graph = compilation.get_module_graph();
let mut not_conflect: FxHashMap<String, Identifier> =
HashMap::with_capacity_and_hasher(module_graph.modules().len(), FxBuildHasher);
let mut conflict: FxHashMap<String, IdentifierSet> = FxHashMap::default();

for module in module_graph.modules().values() {
// 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 lower_identifier_mappings = module_graph
.modules()
.values()
.par_bridge()
.filter_map(|module| {
if let Some(normal_module) = module.as_normal_module() {
if normal_module
.resource_resolved_data()
.encoded_content
.is_some()
{
return None;
}
}
}
let identifier = module.identifier();
Some((identifier, identifier.as_str().cow_to_lowercase()))
})
.collect::<Vec<_>>();

let identifier = module.identifier();
let lower_identifier = identifier.cow_to_lowercase();
let mut not_conflect: FxHashMap<&str, Identifier> =
HashMap::with_capacity_and_hasher(module_graph.modules().len(), FxBuildHasher);
let mut conflict: FxHashMap<&str, IdentifierSet> = FxHashMap::default();
for (identifier, lower_identifier) in &lower_identifier_mappings {
if let Some(prev_identifier) = not_conflect.remove(lower_identifier.as_ref()) {
conflict.insert(
lower_identifier.into_owned(),
IdentifierSet::from_iter([prev_identifier, identifier]),
lower_identifier.as_ref(),
IdentifierSet::from_iter([prev_identifier, *identifier]),
);
} else if let Some(set) = conflict.get_mut(lower_identifier.as_ref()) {
set.insert(identifier);
set.insert(*identifier);
} else {
not_conflect.insert(lower_identifier.into_owned(), identifier);
not_conflect.insert(lower_identifier.as_ref(), *identifier);
}
}

Expand Down

0 comments on commit 64093b6

Please sign in to comment.