diff --git a/crates/swc/src/config/mod.rs b/crates/swc/src/config/mod.rs index ad147b7aecf9..ecc4106d6159 100644 --- a/crates/swc/src/config/mod.rs +++ b/crates/swc/src/config/mod.rs @@ -415,6 +415,74 @@ impl Options { }); } + if js_minify.is_some() && js_minify.as_ref().unwrap().keep_fnames { + js_minify = js_minify.map(|c| { + let compress = c + .compress + .unwrap_as_option(|default| match default { + Some(true) => Some(Default::default()), + _ => None, + }) + .map(|mut c| { + c.keep_fnames = true; + c + }) + .map(BoolOrDataConfig::from_obj) + .unwrap_or_else(|| BoolOrDataConfig::from_bool(false)); + let mangle = c + .mangle + .unwrap_as_option(|default| match default { + Some(true) => Some(Default::default()), + _ => None, + }) + .map(|mut c| { + c.keep_fn_names = true; + c + }) + .map(BoolOrDataConfig::from_obj) + .unwrap_or_else(|| BoolOrDataConfig::from_bool(false)); + JsMinifyOptions { + compress, + mangle, + ..c + } + }); + } + + if js_minify.is_some() && js_minify.as_ref().unwrap().keep_classnames { + js_minify = js_minify.map(|c| { + let compress = c + .compress + .unwrap_as_option(|default| match default { + Some(true) => Some(Default::default()), + _ => None, + }) + .map(|mut c| { + c.keep_classnames = true; + c + }) + .map(BoolOrDataConfig::from_obj) + .unwrap_or_else(|| BoolOrDataConfig::from_bool(false)); + let mangle = c + .mangle + .unwrap_as_option(|default| match default { + Some(true) => Some(Default::default()), + _ => None, + }) + .map(|mut c| { + c.keep_class_names = true; + c + }) + .map(BoolOrDataConfig::from_obj) + .unwrap_or_else(|| BoolOrDataConfig::from_bool(false)); + JsMinifyOptions { + compress, + mangle, + ..c + } + }); + } + let regenerator = transform.regenerator.clone(); let preserve_comments = if preserve_all_comments { diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs index bd945f3fcd0a..b5c2d70e25e3 100644 --- a/crates/swc/src/lib.rs +++ b/crates/swc/src/lib.rs @@ -1028,6 +1028,15 @@ impl Compiler { } } + if opts.keep_fnames { + if let Some(opts) = &mut min_opts.compress { + opts.keep_fnames = true; + } + if let Some(opts) = &mut min_opts.mangle { + opts.keep_fn_names = true; + } + } + let comments = SingleThreadedComments::default(); let module = self diff --git a/node-swc/__tests__/minify/issue_6996_test.mjs b/node-swc/__tests__/minify/issue_6996_test.mjs index 0272ffbfa29c..ec3e0fb13ecf 100644 --- a/node-swc/__tests__/minify/issue_6996_test.mjs +++ b/node-swc/__tests__/minify/issue_6996_test.mjs @@ -1,7 +1,7 @@ import swc from "../../.."; it("should not throw when keep_fnames is on", async () => { - async function minify() { + async function transform() { const { code } = await swc.transform('function Foo() {}', { jsc: { minify: { @@ -11,5 +11,35 @@ it("should not throw when keep_fnames is on", async () => { }); return code; } - await expect(minify()).resolves.toEqual('function Foo() {}\n'); + // should not reject with unknown field `keep_fnames` + await expect(transform()).resolves.toEqual('function Foo() {}\n'); +}); + +it("should pass on keep_fnames to minify.{mangle,compress}", async () => { + const { code } = await swc.transform('export default function Foo() {}', { + jsc: { + minify: { + compress: {}, + mangle: {}, + keep_fnames: true, + }, + }, + minify: true, + }); + expect(code).toEqual('export default function Foo(){}'); +}); + +it("should pass on keep_classnames to minify.{mangle,compress}", async () => { + const { code } = await swc.transform('export default class Foo {}', { + jsc: { + minify: { + compress: {}, + mangle: {}, + keep_classnames: true, + }, + target: 'esnext', + }, + minify: true, + }); + expect(code).toEqual('export default class Foo{}'); });