-
-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support
test
, include
and exclude
options for `SwcCssMini…
…mizerRspackPlugin` (#7111) * pass options top minimizer * update docs * fix api.md * move match_object to lib.rs * Update crates/rspack_plugin_swc_css_minimizer/src/lib.rs Co-authored-by: Gengkun <[email protected]> * remove unnecessary default spread * fix rustfmt error --------- Co-authored-by: Gengkun <[email protected]>
- Loading branch information
1 parent
070a6c5
commit 7540e88
Showing
16 changed files
with
271 additions
and
8 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
62 changes: 62 additions & 0 deletions
62
crates/rspack_binding_options/src/options/raw_builtins/raw_swc_css_minimizer.rs
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,62 @@ | ||
use napi::{bindgen_prelude::Either3, Either}; | ||
use napi_derive::napi; | ||
use rspack_error::Result; | ||
use rspack_napi::regexp::{JsRegExp, JsRegExpExt}; | ||
use rspack_plugin_swc_css_minimizer::{ | ||
SwcCssMinimizerRspackPluginOptions, SwcCssMinimizerRule, SwcCssMinimizerRules, | ||
}; | ||
|
||
type RawSwcCssMinimizerRule = Either<String, JsRegExp>; | ||
type RawSwcCssMinimizerRules = Either3<String, JsRegExp, Vec<RawSwcCssMinimizerRule>>; | ||
struct RawSwcCssMinimizerRuleWrapper(RawSwcCssMinimizerRule); | ||
struct RawSwcCssMinimizerRulesWrapper(RawSwcCssMinimizerRules); | ||
|
||
#[derive(Debug)] | ||
#[napi(object, object_to_js = false)] | ||
pub struct RawSwcCssMinimizerRspackPluginOptions { | ||
#[napi(ts_type = "string | RegExp | (string | RegExp)[]")] | ||
pub test: Option<RawSwcCssMinimizerRules>, | ||
#[napi(ts_type = "string | RegExp | (string | RegExp)[]")] | ||
pub include: Option<RawSwcCssMinimizerRules>, | ||
#[napi(ts_type = "string | RegExp | (string | RegExp)[]")] | ||
pub exclude: Option<RawSwcCssMinimizerRules>, | ||
} | ||
|
||
fn into_condition(c: Option<RawSwcCssMinimizerRules>) -> Option<SwcCssMinimizerRules> { | ||
c.map(|test| RawSwcCssMinimizerRulesWrapper(test).into()) | ||
} | ||
|
||
impl TryFrom<RawSwcCssMinimizerRspackPluginOptions> for SwcCssMinimizerRspackPluginOptions { | ||
type Error = rspack_error::Error; | ||
|
||
fn try_from(value: RawSwcCssMinimizerRspackPluginOptions) -> Result<Self> { | ||
Ok(Self { | ||
test: into_condition(value.test), | ||
include: into_condition(value.include), | ||
exclude: into_condition(value.exclude), | ||
}) | ||
} | ||
} | ||
|
||
impl From<RawSwcCssMinimizerRuleWrapper> for SwcCssMinimizerRule { | ||
fn from(x: RawSwcCssMinimizerRuleWrapper) -> Self { | ||
match x.0 { | ||
Either::A(v) => Self::String(v), | ||
Either::B(v) => Self::Regexp(v.to_rspack_regex()), | ||
} | ||
} | ||
} | ||
|
||
impl From<RawSwcCssMinimizerRulesWrapper> for SwcCssMinimizerRules { | ||
fn from(value: RawSwcCssMinimizerRulesWrapper) -> Self { | ||
match value.0 { | ||
Either3::A(v) => Self::String(v), | ||
Either3::B(v) => Self::Regexp(v.to_rspack_regex()), | ||
Either3::C(v) => Self::Array( | ||
v.into_iter() | ||
.map(|v| RawSwcCssMinimizerRuleWrapper(v).into()) | ||
.collect(), | ||
), | ||
} | ||
} | ||
} |
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
3 changes: 3 additions & 0 deletions
3
packages/rspack-test-tools/tests/configCases/plugins/minify-exclude-css/a.css
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,3 @@ | ||
html { | ||
margin: 0; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/rspack-test-tools/tests/configCases/plugins/minify-exclude-css/a.js
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 @@ | ||
require("./a.css"); |
3 changes: 3 additions & 0 deletions
3
packages/rspack-test-tools/tests/configCases/plugins/minify-exclude-css/b.css
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,3 @@ | ||
html { | ||
margin: 0; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/rspack-test-tools/tests/configCases/plugins/minify-exclude-css/b.js
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 @@ | ||
require("./b.css"); |
12 changes: 12 additions & 0 deletions
12
packages/rspack-test-tools/tests/configCases/plugins/minify-exclude-css/index.js
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 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
it("[minify-exclude-css]: chunk a should be minified", () => { | ||
const content = fs.readFileSync(path.resolve(__dirname, "a.css"), "utf-8"); | ||
expect(content).not.toMatch("\n"); | ||
}); | ||
|
||
it("[minify-exclude-css]: chunk b should not be minified", () => { | ||
const content = fs.readFileSync(path.resolve(__dirname, "b.css"), "utf-8"); | ||
expect(content).toMatch("\n"); | ||
}); |
29 changes: 29 additions & 0 deletions
29
packages/rspack-test-tools/tests/configCases/plugins/minify-exclude-css/rspack.config.js
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,29 @@ | ||
const rspack = require("@rspack/core"); | ||
/** | ||
* @type {import("@rspack/core").Configuration} | ||
*/ | ||
module.exports = { | ||
entry: { | ||
a: "./a.js", | ||
b: "./b.js", | ||
main: "./index.js" | ||
}, | ||
output: { | ||
filename: "[name].js" | ||
}, | ||
module: { | ||
generator: { | ||
"css/auto": { | ||
exportsOnly: false | ||
} | ||
} | ||
}, | ||
optimization: { | ||
minimize: true, | ||
minimizer: [ | ||
new rspack.SwcCssMinimizerRspackPlugin({ | ||
exclude: [/b\.css/] | ||
}) | ||
] | ||
} | ||
}; |
6 changes: 6 additions & 0 deletions
6
packages/rspack-test-tools/tests/configCases/plugins/minify-exclude-css/test.config.js
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,6 @@ | ||
/** @type {import("../../../..").TConfigCaseConfig} */ | ||
module.exports = { | ||
findBundle: (i, options) => { | ||
return ["main.js"]; | ||
} | ||
}; |
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
24 changes: 22 additions & 2 deletions
24
packages/rspack/src/builtin-plugin/SwcCssMinimizerPlugin.ts
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 |
---|---|---|
@@ -1,8 +1,28 @@ | ||
import { BuiltinPluginName } from "@rspack/binding"; | ||
import { | ||
BuiltinPluginName, | ||
RawSwcCssMinimizerRspackPluginOptions | ||
} from "@rspack/binding"; | ||
|
||
import { create } from "./base"; | ||
|
||
type MinifyCondition = string | RegExp; | ||
type MinifyConditions = MinifyCondition | MinifyCondition[]; | ||
|
||
export type SwcCssMinimizerRspackPluginOptions = { | ||
test?: MinifyConditions; | ||
exclude?: MinifyConditions; | ||
include?: MinifyConditions; | ||
}; | ||
|
||
export const SwcCssMinimizerRspackPlugin = create( | ||
BuiltinPluginName.SwcCssMinimizerRspackPlugin, | ||
(options?: any /* TODO: extend more options */) => undefined | ||
( | ||
options?: SwcCssMinimizerRspackPluginOptions | ||
): RawSwcCssMinimizerRspackPluginOptions => { | ||
return { | ||
test: options?.test, | ||
include: options?.include, | ||
exclude: options?.exclude | ||
}; | ||
} | ||
); |
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
7540e88
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Ran ecosystem CI: Open
7540e88
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Benchmark detail: Open