Skip to content

Commit

Permalink
feat: expose keepFnNames and keepClassNames options of builtin sw…
Browse files Browse the repository at this point in the history
…c minfier (#4121)

* feat(minfier): expose keepFnNames and keepClassNames options of builtin swc minfier

* chore: change keep_class_names, keep_fn_names, and changed tests use field name

* chore: remove unused code

* Update crates/rspack_testing/test.config.scheme.json

* Update crates/rspack_testing/test.config.scheme.json

* chore: update snap

* Apply suggestions from code review

* chore: update snap

* chore: format codebase

---------

Co-authored-by: Hana <[email protected]>
  • Loading branch information
xinxinhe1810 and h-a-n-a authored Sep 8, 2023
1 parent e1df35d commit 14ded15
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 22 deletions.
2 changes: 2 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,8 @@ export interface RawLibraryOptions {
export interface RawMinification {
passes: number
dropConsole: boolean
keepClassNames: boolean
keepFnNames: boolean
comments: "all" | "some" | "false"
asciiOnly: boolean
pureFuncs: Array<string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub struct RawMinificationConditions {
pub struct RawMinification {
pub passes: u32,
pub drop_console: bool,
pub keep_class_names: bool,
pub keep_fn_names: bool,
#[napi(ts_type = r#""all" | "some" | "false""#)]
pub comments: String,
pub ascii_only: bool,
Expand Down Expand Up @@ -59,6 +61,8 @@ impl TryFrom<RawMinification> for Minification {
Ok(Self {
passes: value.passes as usize,
drop_console: value.drop_console,
keep_class_names: value.keep_class_names,
keep_fn_names: value.keep_fn_names,
pure_funcs: value.pure_funcs,
ascii_only: value.ascii_only,
comments: value.comments,
Expand Down
14 changes: 12 additions & 2 deletions crates/rspack_plugin_swc_js_minimizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use swc_ecma_minifier::option::{
pub struct Minification {
pub passes: usize,
pub drop_console: bool,
pub keep_class_names: bool,
pub keep_fn_names: bool,
pub pure_funcs: Vec<String>,
pub extract_comments: Option<String>,
pub ascii_only: bool,
Expand Down Expand Up @@ -115,6 +117,12 @@ impl Plugin for SwcJsMinimizerPlugin {
..Default::default()
};

let mangle = MangleOptions {
keep_class_names: minify_options.keep_class_names,
keep_fn_names: minify_options.keep_fn_names,
..Default::default()
};

let comments = match minify_options.comments.as_str() {
"false" => JsMinifyCommentOption::False,
"all" => JsMinifyCommentOption::PreserveAllComments,
Expand Down Expand Up @@ -146,13 +154,15 @@ impl Plugin for SwcJsMinimizerPlugin {
let input_source_map = original_source.map(&MapOptions::default());
let js_minify_options = JsMinifyOptions {
compress: BoolOrDataConfig::from_obj(compress.clone()),
mangle: BoolOrDataConfig::from_obj(mangle.clone()),
format: format.clone(),
source_map: BoolOrDataConfig::from_bool(input_source_map.is_some()),
inline_sources_content: true, /* Using true so original_source can be None in SourceMapSource */
emit_source_map_columns,
module: is_module,
..Default::default()
};

let output = match minify(
&js_minify_options,
input,
Expand Down Expand Up @@ -236,8 +246,8 @@ pub struct JsMinifyOptions {
pub mangle: BoolOrDataConfig<MangleOptions>,
pub format: JsMinifyFormatOptions,
pub ecma: TerserEcmaVersion,
pub keep_classnames: bool,
pub keep_fnames: bool,
pub keep_class_names: bool,
pub keep_fn_names: bool,
pub module: bool,
pub safari10: bool,
pub toplevel: bool,
Expand Down
8 changes: 8 additions & 0 deletions crates/rspack_testing/test.config.scheme.json
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@
"default": false,
"type": "boolean"
},
"keepClassNames": {
"default": false,
"type": "boolean"
},
"keepFnNames": {
"default": false,
"type": "boolean"
},
"extractComments": {
"default": null,
"type": [
Expand Down
4 changes: 4 additions & 0 deletions packages/rspack/src/builtin-plugin/SwcJsMinimizerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type MinifyConditions = MinifyCondition | MinifyCondition[];
export type SwcJsMinimizerPluginOptions = {
passes?: number;
dropConsole?: boolean;
keepClassNames?: boolean;
keepFnNames?: boolean;
pureFuncs?: Array<string>;
extractComments?: boolean | RegExp;
comments?: false | "all" | "some";
Expand Down Expand Up @@ -58,6 +60,8 @@ export const SwcJsMinimizerPlugin = create(
return {
passes: options?.passes ?? 1,
dropConsole: options?.dropConsole ?? false,
keepClassNames: options?.keepClassNames ?? false,
keepFnNames: options?.keepFnNames ?? false,
pureFuncs: options?.pureFuncs ?? [],
comments: options?.comments ? options.comments : "false",
asciiOnly: options?.asciiOnly ?? false,
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack/tests/Stats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("Stats", () => {
entry ./fixtures/a
./fixtures/a.js [876] {main}
entry ./fixtures/a
rspack compiled successfully (b32fac08a5e8721cacff)"
rspack compiled successfully (31124a919605602fc9ae)"
`);
});

Expand Down Expand Up @@ -79,7 +79,7 @@ describe("Stats", () => {
rspack compiled with 1 error (27ec1c09308b67dcfd6f)"
rspack compiled with 1 error (18c9b1da202481673984)"
`);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/rspack/tests/__snapshots__/Stats.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ exports[`Stats should have stats 1`] = `
"errors": [],
"errorsCount": 0,
"filteredModules": undefined,
"hash": "b32fac08a5e8721cacff",
"hash": "31124a919605602fc9ae",
"logging": {},
"modules": [
{
Expand Down Expand Up @@ -416,7 +416,7 @@ exports.c = require("./c?c=3");
"errors": [],
"errorsCount": 0,
"filteredModules": undefined,
"hash": "5eccb83e369e53af1313",
"hash": "089a36c7767bb55f0574",
"logging": {},
"modules": [
{
Expand Down
32 changes: 16 additions & 16 deletions packages/rspack/tests/__snapshots__/StatsTestCases.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ import rawModule from './raw.png'",
"errors": [],
"errorsCount": 0,
"filteredModules": undefined,
"hash": "31df38a93780ad25981d",
"hash": "94f12aa69d3989830e25",
"logging": {},
"modules": [
{
Expand Down Expand Up @@ -337,7 +337,7 @@ chunk {main} bundle.js (main) [entry]
entry ./index
./stringModule.js [363] {main}
esm import ./stringModule [10]
rspack compiled successfully (31df38a93780ad25981d)"
rspack compiled successfully (94f12aa69d3989830e25)"
`;

exports[`StatsTestCases should print correct stats for filename 1`] = `
Expand Down Expand Up @@ -500,7 +500,7 @@ exports[`StatsTestCases should print correct stats for filename 1`] = `
"errors": [],
"errorsCount": 0,
"filteredModules": undefined,
"hash": "7cdaa6ba61cec2cb7288",
"hash": "b1dbe683f73e6b1f7f2c",
"logging": {},
"modules": [
{
Expand Down Expand Up @@ -602,7 +602,7 @@ chunk {main} main.xxxx.js (main) >{dynamic_js}< [entry]
entry ./index
./dynamic.js [426] {dynamic_js}
dynamic import ./dynamic [10]
rspack compiled successfully (7cdaa6ba61cec2cb7288)"
rspack compiled successfully (b1dbe683f73e6b1f7f2c)"
`;

exports[`StatsTestCases should print correct stats for hot+production 1`] = `
Expand Down Expand Up @@ -697,7 +697,7 @@ exports[`StatsTestCases should print correct stats for hot+production 1`] = `
"errors": [],
"errorsCount": 0,
"filteredModules": undefined,
"hash": "d71f062b534cbc5af842",
"hash": "86548123ac24bd06a6bf",
"logging": {},
"modules": [
{
Expand Down Expand Up @@ -760,7 +760,7 @@ chunk {main} bundle.js (main) [entry]
entry ./index.js
./index.js [10] {main}
entry ./index.js
rspack compiled successfully (d71f062b534cbc5af842)"
rspack compiled successfully (86548123ac24bd06a6bf)"
`;

exports[`StatsTestCases should print correct stats for identifier-let-strict-mode 1`] = `
Expand Down Expand Up @@ -2023,7 +2023,7 @@ console.log(a);
],
"errorsCount": 1,
"filteredModules": undefined,
"hash": "7d2d2ee1a9384ad715fa",
"hash": "6ca407f06f94c982125d",
"logging": {},
"modules": [
{
Expand Down Expand Up @@ -2144,7 +2144,7 @@ error[internal]: Resolve error
rspack compiled with 1 error (7d2d2ee1a9384ad715fa)"
rspack compiled with 1 error (6ca407f06f94c982125d)"
`;
exports[`StatsTestCases should print correct stats for resolve-unexpected-exports-in-pkg 1`] = `
Expand Down Expand Up @@ -2288,7 +2288,7 @@ console.log(a);
],
"errorsCount": 1,
"filteredModules": undefined,
"hash": "d4ff2f3bc5aba8d564b3",
"hash": "a9093cd411993b4b9c01",
"logging": {},
"modules": [
{
Expand Down Expand Up @@ -2403,7 +2403,7 @@ error[internal]: Export should be relative path and start with "./", but got ../
rspack compiled with 1 error (d4ff2f3bc5aba8d564b3)"
rspack compiled with 1 error (a9093cd411993b4b9c01)"
`;
exports[`StatsTestCases should print correct stats for simple 1`] = `
Expand Down Expand Up @@ -2498,7 +2498,7 @@ exports[`StatsTestCases should print correct stats for simple 1`] = `
"errors": [],
"errorsCount": 0,
"filteredModules": undefined,
"hash": "44e8a78970b06279cd26",
"hash": "7a27fed57156f35cc7b2",
"logging": {},
"modules": [
{
Expand Down Expand Up @@ -2561,7 +2561,7 @@ chunk {main} bundle.js (main) [entry]
entry ./index
./index.js [10] {main}
entry ./index
rspack compiled successfully (44e8a78970b06279cd26)"
rspack compiled successfully (7a27fed57156f35cc7b2)"
`;
exports[`StatsTestCases should print correct stats for simple-module-source 1`] = `
Expand Down Expand Up @@ -2722,7 +2722,7 @@ import rawModule from './raw.png'",
"errors": [],
"errorsCount": 0,
"filteredModules": undefined,
"hash": "a563cef06d977112d3e1",
"hash": "ef78d0d53e6750a65525",
"logging": {},
"modules": [
{
Expand Down Expand Up @@ -2849,7 +2849,7 @@ Entrypoint main 631 bytes = bundle.js
./raw.png
./index.js
./stringModule.js
rspack compiled successfully (a563cef06d977112d3e1)"
rspack compiled successfully (ef78d0d53e6750a65525)"
`;
exports[`StatsTestCases should print correct stats for stats-hooks 1`] = `
Expand Down Expand Up @@ -2943,7 +2943,7 @@ exports[`StatsTestCases should print correct stats for stats-hooks 1`] = `
"errors": [],
"errorsCount": 0,
"filteredModules": undefined,
"hash": "44e8a78970b06279cd26",
"hash": "7a27fed57156f35cc7b2",
"logging": {},
"modules": [
{
Expand Down Expand Up @@ -3000,7 +3000,7 @@ exports[`StatsTestCases should print correct stats for stats-hooks 2`] = `
asset bundle.js 589 bytes [emitted111] (name: main) [testA: aaaaaa]
Entrypoint main 589 bytes = bundle.js
./index.js
rspack compiled successfully (44e8a78970b06279cd26)"
rspack compiled successfully (7a27fed57156f35cc7b2)"
`;
exports[`StatsTestCases should print correct stats for try-require--module 1`] = `
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class KeepClass {}

it("should keep class names", () => {
const name = KeepClass.name;
expect(name).toBe("KeepClass");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
builtins: {
minifyOptions: {
keepClassNames: true
}
},
optimization: {
minimize: true
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function main() {}

it("should keep fn names", () => {
const name = main.name;
expect(name).toBe("main");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
builtins: {
minifyOptions: {
keepFnNames: true
}
},
optimization: {
minimize: true
}
};

0 comments on commit 14ded15

Please sign in to comment.