-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
Describe the bug
Thank you for creating such a great tool.
While testing, I noticed that the runtime behavior of the code built with SWC differs from the behavior in the development environment, and during this process I discovered the following issue.
In the SWC Playground, the following behavior also can be observed.
When two conditional branches have similar structures and differ only by string literals, the minifier appears to treat them as equivalent and merges them into a single branch.
Input code
export const buildErrorLog = ({
errorType,
mode,
}: {
errorType?: string;
mode: 'modeA' | 'modeB';
}) => {
const isModeA = mode === 'modeA';
const isModeB = mode === 'modeB';
if (errorType === 'A_ERROR') {
const message = 'A error occured';
return { fieldX: true, fieldY: true, message };
}
if (errorType === 'B_ERROR') {
const message = 'B error occured';
return { fieldX: true, fieldY: true, message };
}
return { fieldX: true, fieldY: true, message: 'Invalid configuration' };
};Config
This behavior occurs in the SWC Playground when compiling TypeScript targeting ES2015 or later with minify, compress, and mangle enabled.
(A SWC Playground link is attached.)Link to the code that reproduces this issue
SWC Info output
No response
Expected behavior
The minifier should not merge these two conditional branches.
Actual behavior
export const buildErrorLog = ({ errorType: e, mode: r }) =>
"A_ERROR" === e || "B_ERROR" === e
? { fieldX: !0, fieldY: !0, message: "A error occured" }
: { fieldX: !0, fieldY: !0, message: "Invalid configuration" };
Version
1.11.29
Additional context
Based on testing in the playground, this issue appears to occur when targeting over ES2015, starting from version 1.11.29.
(I am currently using version 1.13.19)