-
-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support WarnCaseSensitiveModulesPlugin
- Loading branch information
1 parent
3c05152
commit e764139
Showing
9 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[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" } | ||
rspack_ids = { path = "../rspack_ids" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// 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; | ||
use rspack_ids::id_helpers; | ||
|
||
#[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\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) = id_helpers::get_used_module_ids_and_modules(compilation, None); | ||
let modules = modules | ||
.into_iter() | ||
.filter_map(|i| compilation.module_graph.module_by_identifier(&i)) | ||
.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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters