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

fix(es/minifier): minify.{keep_fnames,keep_classnames} should set both compress and mangle #7102

Merged
merged 2 commits into from Mar 21, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 68 additions & 0 deletions crates/swc/src/config/mod.rs
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions crates/swc/src/lib.rs
Expand Up @@ -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
Expand Down
34 changes: 32 additions & 2 deletions 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: {
Expand All @@ -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{}');
});