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(rspack_plugin_swc_js_minimizer): add minify option #7599

Merged
merged 3 commits into from
Aug 19, 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: 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
Loading