Skip to content

Commit

Permalink
fix: should minimize asset with query
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder committed Apr 19, 2024
1 parent 75770e6 commit cbbe957
Showing 4 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/rspack_plugin_swc_css_minimizer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@ version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
once_cell = { workspace = true }
regex = { workspace = true }
rspack_core = { path = "../rspack_core" }
rspack_error = { path = "../rspack_error" }
rspack_hook = { path = "../rspack_hook" }
7 changes: 6 additions & 1 deletion crates/rspack_plugin_swc_css_minimizer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use once_cell::sync::Lazy;
use rayon::prelude::*;
use regex::Regex;
use rspack_core::{rspack_sources::MapOptions, Compilation, CompilationProcessAssets, Plugin};
use rspack_error::Result;
use rspack_hook::{plugin, plugin_hook};
use rspack_plugin_css::swc_css_compiler::{SwcCssCompiler, SwcCssSourceMapGenConfig};

static CSS_ASSET_REGEXP: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\.css(\?.*)?$").expect("Invalid RegExp"));

#[plugin]
#[derive(Debug, Default)]
pub struct SwcCssMinimizerRspackPlugin;
@@ -13,7 +18,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
compilation
.assets_mut()
.par_iter_mut()
.filter(|(filename, _)| filename.ends_with(".css"))
.filter(|(filename, _)| CSS_ASSET_REGEXP.is_match(filename))
.try_for_each(|(filename, original)| -> Result<()> {
if original.get_info().minimized {
return Ok(());
7 changes: 5 additions & 2 deletions crates/rspack_plugin_swc_js_minimizer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ use std::hash::Hash;
use std::path::Path;
use std::sync::{mpsc, Arc, Mutex};

use once_cell::sync::OnceCell;
use once_cell::sync::{Lazy, OnceCell};
use rayon::prelude::*;
use regex::Regex;
use rspack_core::rspack_sources::{ConcatSource, MapOptions, RawSource, SourceExt, SourceMap};
@@ -33,6 +33,9 @@ use self::minify::{match_object, minify};

const PLUGIN_NAME: &str = "rspack.SwcJsMinimizerRspackPlugin";

static JAVASCRIPT_ASSET_REGEXP: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\.[cm]?js(\?.*)?$").expect("Invalid RegExp"));

#[derive(Debug, Default)]
pub struct SwcJsMinimizerRspackPluginOptions {
pub extract_comments: Option<ExtractComments>,
@@ -203,7 +206,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
.assets_mut()
.par_iter_mut()
.filter(|(filename, original)| {
if !(filename.ends_with(".js") || filename.ends_with(".cjs") || filename.ends_with(".mjs")) {
if !JAVASCRIPT_ASSET_REGEXP.is_match(filename) {
return false
}

0 comments on commit cbbe957

Please sign in to comment.