-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
use std::collections::HashMap; | ||
use std::path::{Path, PathBuf}; | ||
use std::sync::{Arc, RwLock}; | ||
|
||
use semver::Version; | ||
|
||
use crate::compiler::Context; | ||
use crate::module_graph::ModuleGraph; | ||
use crate::plugin::Plugin; | ||
use crate::resolve::ResolverResource; | ||
|
||
#[derive(Debug, Clone)] | ||
struct PackageInfo { | ||
name: String, | ||
version: Version, | ||
path: PathBuf, | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct DuplicatePackageCheckerPlugin { | ||
verbose: bool, | ||
show_help: bool, | ||
emit_error: bool, | ||
} | ||
|
||
/// Cleans the path by replacing /node_modules/ or \node_modules\ with /~/ | ||
fn clean_path(module_path: &Path) -> PathBuf { | ||
let path_str = module_path.to_string_lossy(); | ||
let cleaned_path = path_str | ||
.replace("/node_modules/", "/~/") | ||
.replace("\\node_modules\\", "/~/"); | ||
PathBuf::from(cleaned_path) | ||
} | ||
|
||
/// Makes the cleaned path relative to the given context | ||
fn clean_path_relative_to_context(module_path: &Path, context: &Path) -> PathBuf { | ||
let cleaned_path = clean_path(module_path); | ||
let context_str = context.to_str().unwrap(); | ||
let cleaned_path_str = cleaned_path.to_str().unwrap(); | ||
|
||
if cleaned_path_str.starts_with(context_str) { | ||
let relative_path = cleaned_path_str.trim_start_matches(context_str); | ||
PathBuf::from(format!(".{}", relative_path)) | ||
} else { | ||
cleaned_path | ||
} | ||
} | ||
|
||
impl DuplicatePackageCheckerPlugin { | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
pub fn verbose(mut self, verbose: bool) -> Self { | ||
self.verbose = verbose; | ||
self | ||
} | ||
|
||
pub fn show_help(mut self, show_help: bool) -> Self { | ||
self.show_help = show_help; | ||
self | ||
} | ||
|
||
pub fn emit_error(mut self, emit_error: bool) -> Self { | ||
self.emit_error = emit_error; | ||
self | ||
} | ||
|
||
fn find_duplicates(packages: Vec<PackageInfo>) -> HashMap<String, Vec<PackageInfo>> { | ||
let mut package_map: HashMap<String, Vec<PackageInfo>> = HashMap::new(); | ||
|
||
for package in packages { | ||
package_map | ||
.entry(package.name.clone()) | ||
.or_default() | ||
.push(package); | ||
} | ||
|
||
package_map | ||
.into_iter() | ||
.filter(|(_, versions)| versions.len() > 1) | ||
.collect() | ||
} | ||
|
||
fn check_duplicates( | ||
&self, | ||
module_graph: &RwLock<ModuleGraph>, | ||
) -> HashMap<String, Vec<PackageInfo>> { | ||
let mut packages = Vec::new(); | ||
|
||
module_graph | ||
.read() | ||
.unwrap() | ||
.modules() | ||
.iter() | ||
.for_each(|module| { | ||
if let Some(ResolverResource::Resolved(resource)) = module | ||
.info | ||
.as_ref() | ||
.and_then(|info| info.resolved_resource.as_ref()) | ||
{ | ||
let package_json = resource.0.package_json().unwrap(); | ||
let raw_json = package_json.raw_json(); | ||
let default_version = serde_json::Value::String("0.0.0".to_string()); | ||
let version = raw_json | ||
.as_object() | ||
.unwrap() | ||
.get("version") | ||
.unwrap_or(&default_version) | ||
.as_str() | ||
.unwrap(); | ||
let package_info = PackageInfo { | ||
name: package_json.name.clone().unwrap_or("mako-pkg".to_string()), | ||
version: semver::Version::parse(version).unwrap(), | ||
path: package_json.path.clone(), | ||
}; | ||
packages.push(package_info); | ||
} | ||
}); | ||
|
||
Self::find_duplicates(packages) | ||
} | ||
} | ||
|
||
impl Plugin for DuplicatePackageCheckerPlugin { | ||
fn name(&self) -> &str { | ||
"DuplicatePackageCheckerPlugin" | ||
} | ||
|
||
fn after_build( | ||
&self, | ||
context: &Arc<Context>, | ||
_compiler: &crate::compiler::Compiler, | ||
) -> anyhow::Result<()> { | ||
let duplicates = self.check_duplicates(&context.module_graph); | ||
|
||
if !duplicates.is_empty() && self.verbose { | ||
let mut message = String::new(); | ||
|
||
for (name, instances) in duplicates { | ||
message.push_str(&format!("\nMultiple versions of {} found:\n", name)); | ||
for instance in instances { | ||
let mut line = format!(" {} {}", instance.version, instance.name); | ||
let path = instance.path.clone(); | ||
line.push_str(&format!( | ||
" from {}", | ||
clean_path_relative_to_context(&path, &context.root).display() | ||
)); | ||
message.push_str(&line); | ||
message.push('\n'); | ||
} | ||
} | ||
|
||
if self.show_help { | ||
message.push_str("\nCheck how you can resolve duplicate packages: \nhttps://github.com/darrenscerri/duplicate-package-checker-webpack-plugin#resolving-duplicate-packages-in-your-bundle\n"); | ||
} | ||
|
||
if !self.emit_error { | ||
println!("{}", message); | ||
} else { | ||
eprintln!("{}", message); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require('a'); | ||
require('b'); | ||
require('c'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"duplicatePackageChecker": { | ||
"verbose": true, | ||
"showHelp": true | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "test", | ||
"version": "1.0.0", | ||
"dependencies": { | ||
"a": "~1.0.0", | ||
"b": "~1.0.0" | ||
} | ||
} |