diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index eb720fd5a89..bfae6c9962e 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -1720,6 +1720,7 @@ export interface RawSwcJsMinimizerOptions { mangle: any format: any module?: boolean + minify?: boolean } export interface RawSwcJsMinimizerRspackPluginOptions { diff --git a/crates/rspack_binding_options/src/options/raw_builtins/raw_swc_js_minimizer.rs b/crates/rspack_binding_options/src/options/raw_builtins/raw_swc_js_minimizer.rs index 1e8e233d817..87a8ed52916 100644 --- a/crates/rspack_binding_options/src/options/raw_builtins/raw_swc_js_minimizer.rs +++ b/crates/rspack_binding_options/src/options/raw_builtins/raw_swc_js_minimizer.rs @@ -35,6 +35,7 @@ pub struct RawSwcJsMinimizerOptions { pub mangle: serde_json::Value, pub format: serde_json::Value, pub module: Option, + pub minify: Option, } fn try_deserialize_into(value: serde_json::Value) -> Result @@ -86,6 +87,7 @@ impl TryFrom for PluginOptions { mangle, format: try_deserialize_into(value.minimizer_options.format)?, module: value.minimizer_options.module, + minify: value.minimizer_options.minify, ..Default::default() }, }) diff --git a/crates/rspack_plugin_swc_js_minimizer/src/lib.rs b/crates/rspack_plugin_swc_js_minimizer/src/lib.rs index abb86155a1a..37210813f08 100644 --- a/crates/rspack_plugin_swc_js_minimizer/src/lib.rs +++ b/crates/rspack_plugin_swc_js_minimizer/src/lib.rs @@ -45,6 +45,7 @@ pub struct PluginOptions { #[derive(Debug, Default)] pub struct MinimizerOptions { + pub minify: Option, pub compress: BoolOrDataConfig, pub mangle: BoolOrDataConfig, pub format: JsMinifyFormatOptions, @@ -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(), @@ -321,6 +323,7 @@ impl Plugin for SwcJsMinimizerRspackPlugin { #[derive(Debug, Clone, Default)] pub struct JsMinifyOptions { + pub minify: bool, pub compress: BoolOrDataConfig, pub mangle: BoolOrDataConfig, pub format: JsMinifyFormatOptions, diff --git a/crates/rspack_plugin_swc_js_minimizer/src/minify.rs b/crates/rspack_plugin_swc_js_minimizer/src/minify.rs index 38b825e2736..fbad523a295 100644 --- a/crates/rspack_plugin_swc_js_minimizer/src/minify.rs +++ b/crates/rspack_plugin_swc_js_minimizer/src/minify.rs @@ -315,7 +315,7 @@ pub fn minify( names: source_map_names, }, None, - true, + opts.minify, Some(&comments), &opts.format, ) diff --git a/packages/rspack-test-tools/tests/configCases/plugins/minify-disable-minify/a.js b/packages/rspack-test-tools/tests/configCases/plugins/minify-disable-minify/a.js new file mode 100644 index 00000000000..48fa4b48369 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/plugins/minify-disable-minify/a.js @@ -0,0 +1,4 @@ +const a = process.env.a; +const b = process.env.b; + +console.log(a + b); diff --git a/packages/rspack-test-tools/tests/configCases/plugins/minify-disable-minify/index.js b/packages/rspack-test-tools/tests/configCases/plugins/minify-disable-minify/index.js new file mode 100644 index 00000000000..bb456e067bb --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/plugins/minify-disable-minify/index.js @@ -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);"); +}); diff --git a/packages/rspack-test-tools/tests/configCases/plugins/minify-disable-minify/rspack.config.js b/packages/rspack-test-tools/tests/configCases/plugins/minify-disable-minify/rspack.config.js new file mode 100644 index 00000000000..91f1b856045 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/plugins/minify-disable-minify/rspack.config.js @@ -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 + } + }) + ] +}; diff --git a/packages/rspack-test-tools/tests/configCases/plugins/minify-disable-minify/test.config.js b/packages/rspack-test-tools/tests/configCases/plugins/minify-disable-minify/test.config.js new file mode 100644 index 00000000000..8313648c1e0 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/plugins/minify-disable-minify/test.config.js @@ -0,0 +1,6 @@ +/** @type {import("../../../../dist").TConfigCaseConfig} */ +module.exports = { + findBundle: (i, options) => { + return ["main.js"]; + } +}; diff --git a/packages/rspack/etc/api.md b/packages/rspack/etc/api.md index 080f08739c5..0f041b99dda 100644 --- a/packages/rspack/etc/api.md +++ b/packages/rspack/etc/api.md @@ -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; @@ -15208,6 +15203,7 @@ export type SwcJsMinimizerRspackPluginOptions = { include?: AssetConditions; extractComments?: ExtractCommentsOptions | undefined; minimizerOptions?: { + minify?: boolean; compress?: TerserCompressOptions | boolean; mangle?: TerserMangleOptions | boolean; format?: JsFormatOptions & ToSnakeCaseProperties; diff --git a/packages/rspack/src/builtin-plugin/SwcJsMinimizerPlugin.ts b/packages/rspack/src/builtin-plugin/SwcJsMinimizerPlugin.ts index 945be4f6953..e6e91867eb5 100644 --- a/packages/rspack/src/builtin-plugin/SwcJsMinimizerPlugin.ts +++ b/packages/rspack/src/builtin-plugin/SwcJsMinimizerPlugin.ts @@ -23,6 +23,7 @@ export type SwcJsMinimizerRspackPluginOptions = { include?: AssetConditions; extractComments?: ExtractCommentsOptions | undefined; minimizerOptions?: { + minify?: boolean; compress?: TerserCompressOptions | boolean; mangle?: TerserMangleOptions | boolean; format?: JsFormatOptions & ToSnakeCaseProperties; @@ -296,6 +297,7 @@ export const SwcJsMinimizerRspackPlugin = create( compress, mangle, format, + minify: options?.minimizerOptions?.minify, module: options?.minimizerOptions?.module } }; diff --git a/website/docs/en/plugins/rspack/swc-js-minimizer-rspack-plugin.mdx b/website/docs/en/plugins/rspack/swc-js-minimizer-rspack-plugin.mdx index a2070eb7e0a..5d7d2bd662e 100644 --- a/website/docs/en/plugins/rspack/swc-js-minimizer-rspack-plugin.mdx +++ b/website/docs/en/plugins/rspack/swc-js-minimizer-rspack-plugin.mdx @@ -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; diff --git a/website/docs/zh/plugins/rspack/swc-js-minimizer-rspack-plugin.mdx b/website/docs/zh/plugins/rspack/swc-js-minimizer-rspack-plugin.mdx index 9aec05f036d..82ee982c7a8 100644 --- a/website/docs/zh/plugins/rspack/swc-js-minimizer-rspack-plugin.mdx +++ b/website/docs/zh/plugins/rspack/swc-js-minimizer-rspack-plugin.mdx @@ -28,6 +28,7 @@ module.exports = { include?: AssetConditions; extractComments?: boolean | RegExp; minimizerOptions?: { + minify?: boolean; compress?: TerserCompressOptions | boolean; mangle?: TerserMangleOptions | boolean; module?: boolean;