swc/crates/swc_ecma_minifier
CPunisher 7344a638b5
feat(es/minifier): Optimize switch with side effect and termination tests (#9677)
**Description:**

This is learnt from https://github.com/terser/terser/pull/1044
**Key point:** all the cases (statements) after the last side-effect and
non-terminated one are useless.

There are also some points on whether a statement is side-effect free:
- **Var declaration:** I think var declaration always contains side
effect.
- **Function declaration:** In non-strict mode, function declaration is
same as var declaration. In strict mode, function is declared in
enclosing block scope, so it's side-effect free. But there is a bad case
when we call the function before it is declared in the switch case:
```js
"use strict";
const k = (() => {
    let x = 1;
    switch (x) {
        case y():
        case x: {
            async function y() {
                console.log(123);
            }
        }
    }
    return x;
})();
```

**This is an existing bug.** I'm not sure whether we should fix it since
it is a very bad code and I also find some similar bad cases with
terser.

~I'm also not sure it's appropriate to introduce context variable
`in_strict` for `StmtExt:: may_have_side_effects`, because `in_strict`
could change from `false` to `true`. But this is a **conservative**
mistake and does not break the program. Maybe we should use visitor for
context-aware side effect checker in the future.~

**Related issue:**

 - Closes https://github.com/swc-project/swc/issues/8953
2024-10-29 10:39:05 +09:00
..
benches feat(es/minifier): Support mangle cache (#9489) 2024-08-25 23:42:53 +00:00
examples feat(es/minifier): Support mangle cache (#9489) 2024-08-25 23:42:53 +00:00
fuzz fix(deps): update cargo (patch) (#9317) 2024-07-23 18:38:43 +09:00
inputs fix(es/minifier): Fix bugs (#2955) 2022-01-05 05:12:52 +09:00
scripts fix(es/minifier): Abort IIFE invoker in function parameters (#8828) 2024-04-09 15:22:38 +09:00
src feat(es/minifier): Optimize switch with side effect and termination tests (#9677) 2024-10-29 10:39:05 +09:00
tests feat(es/minifier): Optimize switch with side effect and termination tests (#9677) 2024-10-29 10:39:05 +09:00
.eslintrc chore(repo): Configure prettier (#4523) 2022-05-04 14:25:28 +00:00
.gitignore feat(es/minifier): Remove duplicate var declarations (#5373) 2022-08-03 13:05:56 +09:00
Cargo.toml chore: Publish crates with swc_core v1.0.5 2024-10-24 23:28:24 +09:00
package.json fix(es/minifier): Fix optimization of assignment expressions (#3231) 2022-01-12 08:26:56 +09:00
README.md doc(es/minifier): Add contributing section 2024-07-13 17:05:31 +09:00
yarn.lock fix(es/minifier): Fix optimization of assignment expressions (#3231) 2022-01-12 08:26:56 +09:00

Minifier

EcmaScript minifier for the SWC project. This is basically a port of terser.

Note

Currently name mangler is very simple. To focus on creating a MVP, I (kdy1) will use simple logic for name mangler and implement the content-aware name mangler after publishing first non-beta version.

Debugging tips

If the output contains variables named e, t, n it typically means the original library is published in a minified form and the input contains eval. The current swc name mangler does not do anything if eval is used.

Profiling the minifier

From mac os x, run

./scripts/instrument/all.sh path/to/input/dir

Contributing

Testing

Please prefer execution tests over unit tests. Execution tests are more useful because there's no chance of human error while reviewing.

Execution tests

You can add a test to ./tests/exec.rs

You can run ./scripts/exec.sh from ./crates/swc_ecma_minifier to run execution tests of SWC minifier. exec.sh runs the cargo test with --features debug, and it makes the cargo test print lots of debug logging. You can search for "change", and you can know the code responsible the optimization. The minifier has report_change! macro, and it prints the location of the relevant code.

Fixture tests

You can add a test to ./tests/fixture. You can select any directory, but please prefer to use the issues directory. You can run ./scripts/test.sh from ./crates/swc_ecma_minifier to run fixture tests. You can run it like ./scripts/test.sh foo to run test cases only with foo in the file path. If you want to get location of change, you can do ./scripts/test.sh foo --features debug.