Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should minimize asset with query #6301

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.foo {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require("./index.css");

const fs = require("fs");
const path = require("path");

function test() {
return 123;
}

it("basic", () => {
expect(test()).toBe(123);
});

it("format", () => {
const content = fs.readFileSync(__filename, "utf-8");
expect(content).not.toMatch("\n");
});

it("css", () => {
const content = fs.readFileSync(
path.resolve(__dirname, "bundle0.css"),
"utf-8"
);
expect(content).not.toMatch("\n");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**@type {import('@rspack/cli').Configuration}*/
module.exports = {
module: {
generator: {
"css/auto": {
exportsOnly: false,
}
}
},
output: {
filename: 'bundle0.js?hash=[contenthash]',
cssFilename: 'bundle0.css?hash=[contenthash]'
},
optimization: {
minimize: true
}
};
Loading