diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index eb720fd5a895..2e3f4c739c03 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -1728,6 +1728,7 @@ export interface RawSwcJsMinimizerRspackPluginOptions { exclude?: string | RegExp | (string | RegExp)[] extractComments?: RawExtractComments minimizerOptions: RawSwcJsMinimizerOptions + minify?: boolean } export interface RawToOptions { 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 1e8e233d8174..469e2ece27f0 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 @@ -26,6 +26,7 @@ pub struct RawSwcJsMinimizerRspackPluginOptions { pub exclude: Option, pub extract_comments: Option, pub minimizer_options: RawSwcJsMinimizerOptions, + pub minify: Option, } #[derive(Debug)] @@ -81,6 +82,7 @@ impl TryFrom for PluginOptions { test: value.test.map(into_asset_conditions), include: value.include.map(into_asset_conditions), exclude: value.exclude.map(into_asset_conditions), + minify: value.minify, minimizer_options: MinimizerOptions { compress, mangle, diff --git a/crates/rspack_plugin_swc_js_minimizer/src/lib.rs b/crates/rspack_plugin_swc_js_minimizer/src/lib.rs index abb86155a1af..cb4e2989e298 100644 --- a/crates/rspack_plugin_swc_js_minimizer/src/lib.rs +++ b/crates/rspack_plugin_swc_js_minimizer/src/lib.rs @@ -36,6 +36,7 @@ static JAVASCRIPT_ASSET_REGEXP: LazyLock = #[derive(Debug, Hash)] pub struct PluginOptions { + pub minify: Option, pub test: Option, pub include: Option, pub exclude: Option, @@ -199,6 +200,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { }; let js_minify_options = JsMinifyOptions { + minify: 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 38b825e27368..fbad523a2959 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/__snapshots__/StatsAPI.test.js.snap b/packages/rspack-test-tools/tests/__snapshots__/StatsAPI.test.js.snap index 94d5499bd92e..b1d8b21a4531 100644 --- a/packages/rspack-test-tools/tests/__snapshots__/StatsAPI.test.js.snap +++ b/packages/rspack-test-tools/tests/__snapshots__/StatsAPI.test.js.snap @@ -52,7 +52,7 @@ Object { "main.js", ], "filteredModules": undefined, - "hash": "333e59d94047c9b4ec6c", + "hash": "c0151c6dfca2168c65d7", "id": "909", "idHints": Array [], "initial": true, @@ -176,7 +176,7 @@ Object { "errorsCount": 0, "filteredAssets": undefined, "filteredModules": undefined, - "hash": "a156b269cd7b4340da9d", + "hash": "d46aa17c3e1115ff8d8a", "modules": Array [ Object { "assets": Array [], @@ -324,7 +324,7 @@ Object { "main.js", ], "filteredModules": undefined, - "hash": "a2024ddd80c1dac51782", + "hash": "e9a1c09dae741d8eab71", "id": "909", "idHints": Array [], "initial": true, @@ -691,7 +691,7 @@ Object { "errorsCount": 0, "filteredAssets": undefined, "filteredModules": undefined, - "hash": "959ba20161293349edf3", + "hash": "1234d41312362d1e5f6e", "modules": Array [ Object { "assets": Array [], @@ -1458,7 +1458,7 @@ Object { "files": Array [ "main.js", ], - "hash": "333e59d94047c9b4ec6c", + "hash": "c0151c6dfca2168c65d7", "id": "909", "idHints": Array [], "initial": true, @@ -1714,7 +1714,7 @@ Object { "main.js", ], "filteredModules": undefined, - "hash": "add6ca394a084131cdb7", + "hash": "b05777d51d49dfde7aa8", "id": "909", "idHints": Array [], "initial": true, @@ -2063,7 +2063,7 @@ exports.c = require(\\"./c?c=3\\"); "errorsCount": 0, "filteredAssets": undefined, "filteredModules": undefined, - "hash": "0e205cc26142ad8eade1", + "hash": "b1ef105808f10ca0733b", "modules": Array [ Object { "assets": Array [], 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 000000000000..48fa4b483690 --- /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 000000000000..bb456e067bbe --- /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 000000000000..6be9149ddbf7 --- /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({ + minify: false, + minimizerOptions: { + 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 000000000000..8313648c1e03 --- /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-test-tools/tests/statsAPICases/basic.js b/packages/rspack-test-tools/tests/statsAPICases/basic.js index b602a395f1d0..323cf95b9fd6 100644 --- a/packages/rspack-test-tools/tests/statsAPICases/basic.js +++ b/packages/rspack-test-tools/tests/statsAPICases/basic.js @@ -39,7 +39,7 @@ module.exports = { entry ./fixtures/a cjs self exports reference self [585] ./fixtures/a.js - Rspack compiled successfully (a156b269cd7b4340da9d)" + Rspack compiled successfully (d46aa17c3e1115ff8d8a)" `); } }; diff --git a/packages/rspack-test-tools/tests/statsAPICases/chunks.js b/packages/rspack-test-tools/tests/statsAPICases/chunks.js index 803aceb173c3..aa0c4751cd92 100644 --- a/packages/rspack-test-tools/tests/statsAPICases/chunks.js +++ b/packages/rspack-test-tools/tests/statsAPICases/chunks.js @@ -27,7 +27,7 @@ module.exports = { "chunkB.js", ], "filteredModules": undefined, - "hash": "9aa837bfaefd1fd4ec18", + "hash": "94e5f4b9ba950ceab14f", "id": "250", "idHints": Array [], "initial": false, @@ -144,7 +144,7 @@ module.exports = { "main.js", ], "filteredModules": undefined, - "hash": "0e471e878e0dbdf0ba5a", + "hash": "5e75c3a7bac911893513", "id": "909", "idHints": Array [], "initial": true, diff --git a/packages/rspack/etc/api.md b/packages/rspack/etc/api.md index 080f08739c51..23d892bdafe4 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; @@ -15207,6 +15202,7 @@ export type SwcJsMinimizerRspackPluginOptions = { exclude?: AssetConditions; include?: AssetConditions; extractComments?: ExtractCommentsOptions | undefined; + minify?: boolean; minimizerOptions?: { compress?: TerserCompressOptions | boolean; mangle?: TerserMangleOptions | boolean; diff --git a/packages/rspack/src/builtin-plugin/SwcJsMinimizerPlugin.ts b/packages/rspack/src/builtin-plugin/SwcJsMinimizerPlugin.ts index 945be4f6953a..726e343fce10 100644 --- a/packages/rspack/src/builtin-plugin/SwcJsMinimizerPlugin.ts +++ b/packages/rspack/src/builtin-plugin/SwcJsMinimizerPlugin.ts @@ -22,6 +22,7 @@ export type SwcJsMinimizerRspackPluginOptions = { exclude?: AssetConditions; include?: AssetConditions; extractComments?: ExtractCommentsOptions | undefined; + minify?: boolean; minimizerOptions?: { compress?: TerserCompressOptions | boolean; mangle?: TerserMangleOptions | boolean; @@ -288,6 +289,7 @@ export const SwcJsMinimizerRspackPlugin = create( } return { + minify: options?.minify, test: options?.test, include: options?.include, exclude: options?.exclude, 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 a2070eb7e0a6..d24ae79bbc89 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,6 +27,7 @@ module.exports = { exclude?: AssetConditions; include?: AssetConditions; extractComments?: boolean | RegExp; + minify?: boolean; minimizerOptions: { compress?: TerserCompressOptions | boolean; mangle?: TerserMangleOptions | 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 9aec05f036d6..4ee94de1c754 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 @@ -27,6 +27,7 @@ module.exports = { exclude?: AssetConditions; include?: AssetConditions; extractComments?: boolean | RegExp; + minify?: boolean; minimizerOptions?: { compress?: TerserCompressOptions | boolean; mangle?: TerserMangleOptions | boolean;