Skip to content

Commit

Permalink
feat(rspack_plugin_swc_js_minimizer): add minify option (#7599)
Browse files Browse the repository at this point in the history
* feat(rspack_plugin_swc_js_minimizer): add minify option

* fix: move minify field

* fix: api
  • Loading branch information
fi3ework authored Aug 19, 2024
1 parent debd406 commit c483c0e
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 8 deletions.
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,7 @@ export interface RawSwcJsMinimizerOptions {
mangle: any
format: any
module?: boolean
minify?: boolean
}

export interface RawSwcJsMinimizerRspackPluginOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct RawSwcJsMinimizerOptions {
pub mangle: serde_json::Value,
pub format: serde_json::Value,
pub module: Option<bool>,
pub minify: Option<bool>,
}

fn try_deserialize_into<T>(value: serde_json::Value) -> Result<T>
Expand Down Expand Up @@ -86,6 +87,7 @@ impl TryFrom<RawSwcJsMinimizerRspackPluginOptions> for PluginOptions {
mangle,
format: try_deserialize_into(value.minimizer_options.format)?,
module: value.minimizer_options.module,
minify: value.minimizer_options.minify,
..Default::default()
},
})
Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_plugin_swc_js_minimizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct PluginOptions {

#[derive(Debug, Default)]
pub struct MinimizerOptions {
pub minify: Option<bool>,
pub compress: BoolOrDataConfig<TerserCompressorOptions>,
pub mangle: BoolOrDataConfig<MangleOptions>,
pub format: JsMinifyFormatOptions,
Expand Down Expand Up @@ -199,6 +200,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
};

let js_minify_options = JsMinifyOptions {
minify: minimizer_options.minify.unwrap_or(true),
compress: minimizer_options.compress.clone(),
mangle: minimizer_options.mangle.clone(),
format: minimizer_options.format.clone(),
Expand Down Expand Up @@ -321,6 +323,7 @@ impl Plugin for SwcJsMinimizerRspackPlugin {

#[derive(Debug, Clone, Default)]
pub struct JsMinifyOptions {
pub minify: bool,
pub compress: BoolOrDataConfig<TerserCompressorOptions>,
pub mangle: BoolOrDataConfig<MangleOptions>,
pub format: JsMinifyFormatOptions,
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_swc_js_minimizer/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub fn minify(
names: source_map_names,
},
None,
true,
opts.minify,
Some(&comments),
&opts.format,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const a = process.env.a;
const b = process.env.b;

console.log(a + b);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const fs = require("fs");
const path = require("path");

it("[minify-disable-minify]: should not minify code", () => {
const content = fs.readFileSync(path.resolve(__dirname, "a.js"), "utf-8");

expect(content).toContain("let a = process.env.a;");
expect(content).toContain("console.log(a + process.env.b);");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const rspack = require("@rspack/core");
/**
* @type {import("@rspack/core").Configuration}
*/
module.exports = {
entry: {
a: "./a",
main: "./index"
},
output: {
filename: "[name].js"
},
optimization: {
minimize: true
},
plugins: [
new rspack.SwcJsMinimizerRspackPlugin({
minimizerOptions: {
minify: false,
mangle: false
}
})
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import("../../../../dist").TConfigCaseConfig} */
module.exports = {
findBundle: (i, options) => {
return ["main.js"];
}
};
8 changes: 2 additions & 6 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -15189,12 +15189,7 @@ type StringOrBufferCallback = (err: NodeJS.ErrnoException | null, data?: string
export const SwcJsMinimizerRspackPlugin: {
new (options?: SwcJsMinimizerRspackPluginOptions | undefined): {
name: BuiltinPluginName;
_args: [options?: SwcJsMinimizerRspackPluginOptions | undefined]; /**
* - `false`: removes all comments
* - `'some'`: preserves some comments
* - `'all'`: preserves all comments
* @default false
*/
_args: [options?: SwcJsMinimizerRspackPluginOptions | undefined];
affectedHooks: "done" | "make" | "compile" | "emit" | "afterEmit" | "invalid" | "thisCompilation" | "afterDone" | "compilation" | "normalModuleFactory" | "contextModuleFactory" | "initialize" | "shouldEmit" | "infrastructureLog" | "beforeRun" | "run" | "assetEmitted" | "failed" | "shutdown" | "watchRun" | "watchClose" | "environment" | "afterEnvironment" | "afterPlugins" | "afterResolvers" | "beforeCompile" | "afterCompile" | "finishMake" | "entryOption" | undefined;
raw(compiler: Compiler_2): BuiltinPlugin;
apply(compiler: Compiler_2): void;
Expand All @@ -15208,6 +15203,7 @@ export type SwcJsMinimizerRspackPluginOptions = {
include?: AssetConditions;
extractComments?: ExtractCommentsOptions | undefined;
minimizerOptions?: {
minify?: boolean;
compress?: TerserCompressOptions | boolean;
mangle?: TerserMangleOptions | boolean;
format?: JsFormatOptions & ToSnakeCaseProperties<JsFormatOptions>;
Expand Down
2 changes: 2 additions & 0 deletions packages/rspack/src/builtin-plugin/SwcJsMinimizerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type SwcJsMinimizerRspackPluginOptions = {
include?: AssetConditions;
extractComments?: ExtractCommentsOptions | undefined;
minimizerOptions?: {
minify?: boolean;
compress?: TerserCompressOptions | boolean;
mangle?: TerserMangleOptions | boolean;
format?: JsFormatOptions & ToSnakeCaseProperties<JsFormatOptions>;
Expand Down Expand Up @@ -296,6 +297,7 @@ export const SwcJsMinimizerRspackPlugin = create(
compress,
mangle,
format,
minify: options?.minimizerOptions?.minify,
module: options?.minimizerOptions?.module
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports = {
exclude?: AssetConditions;
include?: AssetConditions;
extractComments?: boolean | RegExp;
minimizerOptions: {
minimizerOptions?: {
minify?: boolean;
compress?: TerserCompressOptions | boolean;
mangle?: TerserMangleOptions | boolean;
module?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
include?: AssetConditions;
extractComments?: boolean | RegExp;
minimizerOptions?: {
minify?: boolean;
compress?: TerserCompressOptions | boolean;
mangle?: TerserMangleOptions | boolean;
module?: boolean;
Expand Down

2 comments on commit c483c0e

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-08-16 218bda0) Current Change
10000_development-mode + exec 2.35 s ± 21 ms 2.35 s ± 23 ms -0.23 %
10000_development-mode_hmr + exec 713 ms ± 13 ms 707 ms ± 12 ms -0.79 %
10000_production-mode + exec 3.02 s ± 38 ms 3.03 s ± 21 ms +0.41 %
arco-pro_development-mode + exec 1.91 s ± 68 ms 1.91 s ± 50 ms -0.20 %
arco-pro_development-mode_hmr + exec 439 ms ± 2 ms 437 ms ± 2.5 ms -0.50 %
arco-pro_production-mode + exec 3.48 s ± 110 ms 3.48 s ± 77 ms +0.26 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.52 s ± 98 ms 3.54 s ± 63 ms +0.57 %
threejs_development-mode_10x + exec 1.71 s ± 14 ms 1.72 s ± 16 ms +0.44 %
threejs_development-mode_10x_hmr + exec 823 ms ± 9.3 ms 818 ms ± 8 ms -0.58 %
threejs_production-mode_10x + exec 5.53 s ± 39 ms 5.56 s ± 34 ms +0.47 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
nx ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ❌ failure
examples ✅ success

Please sign in to comment.