Skip to content

Commit

Permalink
feat: keep diagnostic semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a committed Sep 19, 2023
1 parent 5dc81f3 commit e603ae1
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions crates/rspack_plugin_swc_js_minimizer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
mod minify;

use std::{collections::HashMap, hash::Hash, sync::Mutex};
use std::{
collections::HashMap,
hash::Hash,
sync::{mpsc, Mutex},
};

use async_trait::async_trait;
use minify::{match_object, minify};
Expand All @@ -12,7 +16,7 @@ use rspack_core::{
AssetInfo, CompilationAsset, JsChunkHashArgs, Plugin, PluginContext, PluginJsChunkHashHookOutput,
PluginProcessAssetsOutput, ProcessAssetsArgs,
};
use rspack_error::{internal_error, Result};
use rspack_error::{internal_error, Diagnostic, Result};
use rspack_regex::RspackRegex;
use rspack_util::try_any_sync;
use swc_config::config_types::BoolOrDataConfig;
Expand Down Expand Up @@ -99,6 +103,7 @@ impl Plugin for SwcJsMinimizerRspackPlugin {
.as_ref()
.is_some_and(|library| library.library_type == "module");

let (tx, rx) = mpsc::channel::<Vec<Diagnostic>>();
// collect all extracted comments info
let all_extracted_comments = Mutex::new(HashMap::new());
let extract_comments_option = &minify_options.extract_comments.clone();
Expand Down Expand Up @@ -129,24 +134,24 @@ impl Plugin for SwcJsMinimizerRspackPlugin {
..Default::default()
};

let result = compilation
.assets_mut()
.par_iter_mut()
.filter(|(filename, original)| {
if !(filename.ends_with(".js") || filename.ends_with(".cjs") || filename.ends_with(".mjs")) {
return false
}
compilation
.assets_mut()
.par_iter_mut()
.filter(|(filename, original)| {
if !(filename.ends_with(".js") || filename.ends_with(".cjs") || filename.ends_with(".mjs")) {
return false
}

let is_matched = match_object(minify_options, filename)
.unwrap_or(false);
let is_matched = match_object(minify_options, filename)
.unwrap_or(false);

if !is_matched || original.get_info().minimized {
return false
}
if !is_matched || original.get_info().minimized {
return false
}

true
})
.try_for_each(|(filename, original)| -> Result<()> {
true
})
.try_for_each_with(tx,|tx,(filename, original)| -> Result<()> {
if let Some(original_source) = original.get_source() {
let input = original_source.source().to_string();
let input_source_map = original_source.map(&MapOptions::default());
Expand All @@ -169,7 +174,9 @@ impl Plugin for SwcJsMinimizerRspackPlugin {
) {
Ok(r) => r,
Err(e) => {
return Err(e)
tx.send(e.into())
.map_err(|e| internal_error!(e.to_string()))?;
return Ok(())
}
};
let source = if let Some(map) = &output.map {
Expand All @@ -190,12 +197,9 @@ impl Plugin for SwcJsMinimizerRspackPlugin {
}

Ok(())
});
})?;

match result {
Ok(_) => {}
Err(e) => compilation.push_batch_diagnostic(e.into()),
};
compilation.push_batch_diagnostic(rx.into_iter().flatten().collect::<Vec<_>>());

// write all extracted comments to assets
all_extracted_comments
Expand Down

0 comments on commit e603ae1

Please sign in to comment.