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

feat: remove fields of SwcJsMinimizerRspackPluginOptions #6950

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion Cargo.lock

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

6 changes: 3 additions & 3 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1433,9 +1433,9 @@ export interface RawStyleConfig {

export interface RawSwcJsMinimizerRspackPluginOptions {
extractComments?: RawExtractComments
compress: boolean | string
mangle: boolean | string
format: string
compress: any
mangle: any
format: any
module?: boolean
test?: string | RegExp | (string | RegExp)[]
include?: string | RegExp | (string | RegExp)[]
Expand Down
1 change: 0 additions & 1 deletion crates/rspack_binding_options/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,5 @@ rspack_swc_visitors = { path = "../rspack_swc_visitors" }
rustc-hash = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
swc_config = { workspace = true }
swc_core = { workspace = true, default-features = false, features = ["ecma_transforms_react"] }
tokio = { workspace = true, features = ["rt", "rt-multi-thread", "macros", "test-util", "parking_lot"] }
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use rspack_plugin_swc_js_minimizer::{
ExtractComments, OptionWrapper, SwcJsMinimizerRspackPluginOptions, SwcJsMinimizerRule,
SwcJsMinimizerRules,
};
use serde::Deserialize;
use swc_config::config_types::BoolOrDataConfig;
use serde::de::DeserializeOwned;
use swc_core::base::BoolOrDataConfig;

type RawSwcJsMinimizerRule = Either<String, JsRegExp>;
type RawSwcJsMinimizerRules = Either3<String, JsRegExp, Vec<RawSwcJsMinimizerRule>>;
Expand All @@ -22,12 +22,12 @@ pub struct RawExtractComments {
}

#[derive(Debug)]
#[napi(object)]
#[napi(object, object_to_js = false)]
pub struct RawSwcJsMinimizerRspackPluginOptions {
pub extract_comments: Option<RawExtractComments>,
pub compress: Either<bool, String>,
pub mangle: Either<bool, String>,
pub format: String,
pub compress: serde_json::Value,
pub mangle: serde_json::Value,
pub format: serde_json::Value,
pub module: Option<bool>,
#[napi(ts_type = "string | RegExp | (string | RegExp)[]")]
pub test: Option<RawSwcJsMinimizerRules>,
Expand All @@ -37,13 +37,11 @@ pub struct RawSwcJsMinimizerRspackPluginOptions {
pub exclude: Option<RawSwcJsMinimizerRules>,
}

fn try_deserialize_into<'de, T: 'de + Deserialize<'de>>(
value: &'de Either<bool, String>,
) -> Result<BoolOrDataConfig<T>> {
Ok(match value {
Either::A(b) => BoolOrDataConfig::from_bool(*b),
Either::B(s) => BoolOrDataConfig::from_obj(serde_json::from_str(s).into_diagnostic()?),
})
fn try_deserialize_into<T>(value: serde_json::Value) -> Result<T>
where
T: DeserializeOwned,
{
serde_json::from_value(value).into_diagnostic()
}

fn into_condition(c: Option<RawSwcJsMinimizerRules>) -> Option<SwcJsMinimizerRules> {
Expand Down Expand Up @@ -74,11 +72,19 @@ impl TryFrom<RawSwcJsMinimizerRspackPluginOptions> for SwcJsMinimizerRspackPlugi
type Error = rspack_error::Error;

fn try_from(value: RawSwcJsMinimizerRspackPluginOptions) -> Result<Self> {
let compress = try_deserialize_into::<
BoolOrDataConfig<rspack_plugin_swc_js_minimizer::TerserCompressorOptions>,
>(value.compress)?
.or(|| BoolOrDataConfig::from_bool(true));
let mangle = try_deserialize_into::<
BoolOrDataConfig<rspack_plugin_swc_js_minimizer::MangleOptions>,
>(value.mangle)?
.or(|| BoolOrDataConfig::from_bool(true));
Ok(Self {
extract_comments: into_extract_comments(value.extract_comments),
compress: try_deserialize_into(&value.compress)?,
mangle: try_deserialize_into(&value.mangle)?,
format: serde_json::from_str(&value.format).into_diagnostic()?,
compress,
mangle,
format: try_deserialize_into(value.format)?,
module: value.module,
test: into_condition(value.test),
include: into_condition(value.include),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Object {
"hotModuleReplacement": false,
},
"name": "main.js",
"size": 207,
"size": 206,
"type": "asset",
},
],
Expand All @@ -35,7 +35,7 @@ Object {
"files": Array [
"main.js",
],
"hash": "93a9c2315c37cadce79d",
"hash": "0de84e4ce8fcd639bcde",
"id": "909",
"idHints": Array [],
"initial": true,
Expand Down Expand Up @@ -132,10 +132,10 @@ Object {
"assets": Array [
Object {
"name": "main.js",
"size": 207,
"size": 206,
},
],
"assetsSize": 207,
"assetsSize": 206,
"chunks": Array [
"909",
],
Expand All @@ -146,7 +146,7 @@ Object {
"errors": Array [],
"errorsCount": 0,
"filteredModules": undefined,
"hash": "99b766bb1908ecab2ebb",
"hash": "53775971d218329cdf79",
"modules": Array [
Object {
"assets": Array [],
Expand Down Expand Up @@ -214,10 +214,10 @@ Object {
"assets": Array [
Object {
"name": "main.js",
"size": 207,
"size": 206,
},
],
"assetsSize": 207,
"assetsSize": 206,
"chunks": Array [
"909",
],
Expand Down Expand Up @@ -266,7 +266,7 @@ Object {
"files": Array [
"main.js",
],
"hash": "4936042f572297f50e12",
"hash": "b9e27e54724c1cf2d77c",
"id": "909",
"idHints": Array [],
"initial": true,
Expand Down Expand Up @@ -561,7 +561,7 @@ Object {
"errors": Array [],
"errorsCount": 0,
"filteredModules": undefined,
"hash": "ab30e6de8b30ef4cc8fb",
"hash": "9e5faccc8912709a0754",
"modules": Array [
Object {
"assets": Array [],
Expand Down Expand Up @@ -966,7 +966,7 @@ Object {
"hotModuleReplacement": false,
},
"name": "main.js",
"size": 207,
"size": 206,
"type": "asset",
},
],
Expand All @@ -983,7 +983,7 @@ Object {
"files": Array [
"main.js",
],
"hash": "93a9c2315c37cadce79d",
"hash": "0de84e4ce8fcd639bcde",
"id": "909",
"idHints": Array [],
"initial": true,
Expand Down Expand Up @@ -1058,7 +1058,7 @@ Object {
"hotModuleReplacement": false,
},
"name": "main.js",
"size": 346,
"size": 337,
"type": "asset",
},
],
Expand All @@ -1077,7 +1077,7 @@ Object {
"files": Array [
"main.js",
],
"hash": "39e2485ff95dc65d08d1",
"hash": "e76e82e3d015ae94d407",
"id": "909",
"idHints": Array [],
"initial": true,
Expand Down Expand Up @@ -1375,10 +1375,10 @@ exports.c = require(\\"./c?c=3\\");
"assets": Array [
Object {
"name": "main.js",
"size": 346,
"size": 337,
},
],
"assetsSize": 346,
"assetsSize": 337,
"chunks": Array [
"909",
],
Expand All @@ -1389,7 +1389,7 @@ exports.c = require(\\"./c?c=3\\");
"errors": Array [],
"errorsCount": 0,
"filteredModules": undefined,
"hash": "9065311db3fa4c6235f4",
"hash": "177b5548f3cb0edb1d8d",
"modules": Array [
Object {
"assets": Array [],
Expand Down Expand Up @@ -1658,10 +1658,10 @@ exports.c = require(\\"./c?c=3\\");
"assets": Array [
Object {
"name": "main.js",
"size": 346,
"size": 337,
},
],
"assetsSize": 346,
"assetsSize": 337,
"chunks": Array [
"909",
],
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading