-
-
Notifications
You must be signed in to change notification settings - Fork 616
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
399b7fc
commit 3019604
Showing
10 changed files
with
114 additions
and
13 deletions.
There are no files selected for viewing
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
63 changes: 63 additions & 0 deletions
63
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,63 @@ | ||
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), | ||
..Default::default() | ||
}) | ||
} | ||
} | ||
|
||
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
4 changes: 1 addition & 3 deletions
4
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 |
---|---|---|
@@ -1,3 +1 @@ | ||
import "./a.css"; | ||
|
||
consol.log("a"); | ||
require("./a.css"); |
4 changes: 1 addition & 3 deletions
4
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 |
---|---|---|
@@ -1,3 +1 @@ | ||
import "./b.css"; | ||
|
||
consol.log("b"); | ||
require("./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
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
16 changes: 14 additions & 2 deletions
16
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,16 +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?: SwcCssMinimizerRspackPluginOptions) => undefined | ||
( | ||
options?: SwcCssMinimizerRspackPluginOptions | ||
): RawSwcCssMinimizerRspackPluginOptions => { | ||
return { | ||
test: options?.test, | ||
include: options?.include, | ||
exclude: options?.exclude | ||
}; | ||
} | ||
); |