Skip to content

Commit 5fca865

Browse files
committed
feat: expand minifier docs and transformer global variable replacement docs
1 parent 12692a2 commit 5fca865

File tree

8 files changed

+427
-1
lines changed

8 files changed

+427
-1
lines changed

.vitepress/config/en.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,39 @@ export const enConfig = defineLocaleConfig("root", {
107107
text: "Plugins",
108108
link: "/docs/guide/usage/transformer/plugins",
109109
},
110+
{
111+
text: "Global Variable Replacement",
112+
link: "/docs/guide/usage/transformer/global-variable-replacement",
113+
},
110114
{
111115
text: "Isolated Declarations",
112116
link: "/docs/guide/usage/transformer/isolated-declarations",
113117
},
114118
],
115119
},
116120
{ text: "Formatter", link: "/docs/guide/usage/formatter" },
117-
{ text: "Minifier", link: "/docs/guide/usage/minifier" },
121+
{
122+
text: "Minifier",
123+
link: "/docs/guide/usage/minifier",
124+
items: [
125+
{
126+
text: "Dead Code Elimination",
127+
link: "/docs/guide/usage/minifier/dead-code-elimination",
128+
},
129+
{
130+
text: "Syntax Normalization",
131+
link: "/docs/guide/usage/minifier/syntax-normalization",
132+
},
133+
{
134+
text: "Mangling",
135+
link: "/docs/guide/usage/minifier/mangling",
136+
},
137+
{
138+
text: "Whitespace Stripping",
139+
link: "/docs/guide/usage/minifier/whitespace-stripping",
140+
},
141+
],
142+
},
118143
{ text: "Resolver", link: "/docs/guide/usage/resolver" },
119144
],
120145
},

src/docs/guide/usage/minifier.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
We recommend thoroughly testing its output before deploying to production environments.
66
:::
77

8+
## Features
9+
10+
- [Eliminate dead code.](./minifier/dead-code-elimination)
11+
- [Transforms syntaxes to make the output shorter and repetitive.](./minifier/syntax-normalization)
12+
- [Mangle variable names.](./minifier/mangling)
13+
- [Remove whitespace and comments.](./minifier/whitespace-stripping)
14+
15+
## Assumptions
16+
17+
To allow better optimizations, Oxc minifier makes some assumptions about your code. See [Assumptions document](https://github.com/oxc-project/oxc/blob/main/crates/oxc_minifier/docs/ASSUMPTIONS.md) for more information.
18+
819
## Installation
920

1021
### With Rolldown
@@ -22,6 +33,10 @@ Use the umbrella crate [oxc][url-oxc-crate] with the `minifier` feature.
2233

2334
Rust usage example can be found [here](https://github.com/oxc-project/oxc/blob/main/crates/oxc_minifier/examples/minifier.rs).
2435

36+
## Integrations
37+
38+
- [`unplugin-oxc`](https://github.com/unplugin/unplugin-oxc)
39+
2540
<!-- Links -->
2641

2742
[url-oxc-crate]: https://docs.rs/oxc
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# Dead Code Elimination
2+
3+
Oxc minifier supports eliminating dead code. For example, it removes the statements inside a `if (false)` block and the unused private class fields.
4+
5+
This feature is always enabled, but you can remove more code by enabling some options.
6+
7+
::: tip Useful features in the transformer
8+
9+
Other than the options below, you can also use [the `define` feature in the transformer](/docs/guide/usage/transformer/global-variable-replacement#define) to replace global identifiers with constant expressions to remove more dead code.
10+
11+
:::
12+
13+
## Drop Console
14+
15+
You can remove all `console.*` calls by enabling the `dropConsole` option. This option behaves similar to [Terser](https://terser.org/)'s `drop_console` option and [esbuild's `drop: ['console']` option](https://esbuild.github.io/api/#drop).
16+
17+
```js
18+
// input
19+
const bar = window.bar();
20+
console.log("foo", bar, baz());
21+
22+
// output
23+
const bar = window.bar();
24+
```
25+
26+
```js
27+
// Example
28+
import { minify } from "oxc-minify";
29+
30+
const result = minify("lib.js", code, {
31+
compress: {
32+
dropConsole: true,
33+
},
34+
});
35+
```
36+
37+
::: warning The whole call expression is removed
38+
39+
Note that this option removes the whole call expression including the arguments. This is intentional because removing the evaluation of call arguments is useful for improving the runtime performance if those are expensive to compute. However, if any of those arguments have side effects, this transformation will change the behavior of your code.
40+
41+
<!-- TODO: suggest pure functions option when exposed -->
42+
43+
:::
44+
45+
## Drop Debugger
46+
47+
You can remove all `debugger` statements by enabling the `dropDebugger` option. This option is enabled by default. This option behaves similar to [Terser](https://terser.org/)'s `drop_debugger` option and [esbuild's `drop: ['debugger']` option](https://esbuild.github.io/api/#drop).
48+
49+
```js
50+
// input
51+
debugger;
52+
53+
// output
54+
```
55+
56+
```js
57+
// Example
58+
import { minify } from "oxc-minify";
59+
60+
const result = minify("lib.js", code, {
61+
compress: {
62+
dropDebugger: true,
63+
},
64+
});
65+
```
66+
67+
## Drop Labels
68+
69+
You can remove all labeled statements with specified labels by enabling the `dropLabels` option. This option behaves similar to [esbuild's `dropLabels` option](https://esbuild.github.io/api/#drop-labels).
70+
71+
```js
72+
// input
73+
DEV: console.log("foo");
74+
console.log("bar");
75+
76+
// output
77+
console.log("bar");
78+
```
79+
80+
```js
81+
// Example
82+
import { minify } from "oxc-minify";
83+
84+
const result = minify("lib.js", code, {
85+
compress: {
86+
dropLabels: ["DEV"],
87+
},
88+
});
89+
```
90+
91+
## Unused Declarations
92+
93+
All unused function / class / variable declarations are removed by default. You can keep them by using the `unused` option.
94+
95+
```js
96+
// input
97+
{
98+
function foo() {}
99+
}
100+
101+
// output
102+
```
103+
104+
```js
105+
// Example
106+
import { minify } from "oxc-minify";
107+
108+
const result = minify("lib.js", code, {
109+
compress: {
110+
unused: true, // or "keep_assign"
111+
},
112+
});
113+
```
114+
115+
## Keep `name` Property Values
116+
117+
By default, Oxc minifier assumes that your code does not rely on the `name` property of functions / classes. This is because the `name` property is inferred from the function / class name or the variable name and keeping the original name would prevent reducing the output size.
118+
119+
To keep the `name` property values, you can use the `keepNames` option.
120+
121+
```js
122+
// input
123+
var bar = function foo() {};
124+
125+
// output
126+
var bar = function foo() {};
127+
```
128+
129+
```js
130+
// Example
131+
import { minify } from "oxc-minify";
132+
133+
const result = minify("lib.js", code, {
134+
compress: {
135+
keepNames: true, // shorthand of { function: true, class: true }
136+
},
137+
});
138+
```
139+
140+
::: tip `mangle.keepNames` option
141+
142+
If you are using the mangling feature, you may also want to enable [the `mangle.keepNames` option](./mangling#keep-name-property-values).
143+
144+
:::
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Mangling
2+
3+
Oxc minifier supports mangling variable names and private class fields.
4+
5+
This feature is enabled by default and can be disabled by setting the `mangle` option to `false`.
6+
7+
## Top Level Variables
8+
9+
Top level variables are not mangled by default for non module code. You can enable mangling for top level variables by setting the `mangle.toplevel` option to `true`.
10+
11+
```js
12+
// input
13+
var foo = 1;
14+
15+
// output
16+
var e = 1;
17+
```
18+
19+
```js
20+
// Example
21+
import { minify } from "oxc-minify";
22+
23+
const result = minify("lib.js", code, {
24+
module: false, // non-module code
25+
compress: {
26+
mangle: {
27+
toplevel: true,
28+
},
29+
},
30+
});
31+
```
32+
33+
## Keep `name` Property Values
34+
35+
Mangling variable names can change the `name` property values of functions / classes. You can keep the original `name` property values by enabling the `mangle.keepNames` option.
36+
37+
```js
38+
// input
39+
var foo = function() {};
40+
41+
// output
42+
var foo = function() {};
43+
```
44+
45+
```js
46+
// Example
47+
import { minify } from "oxc-minify";
48+
49+
const result = minify("lib.js", code, {
50+
compress: {
51+
mangle: {
52+
keepNames: true, // shorthand of { function: true, class: true }
53+
},
54+
},
55+
});
56+
```
57+
58+
::: tip `compress.keepNames` option
59+
60+
When enabling this option, you may also want to enable [the `compress.keepNames` option](./dead-code-elimination#keep-name-property-values).
61+
62+
:::
63+
64+
## Debugging The Mangler
65+
66+
To debug the mangler, you can enable the `mangle.debug` option. When this option is enabled, the mangler will use `slot_0`, `slot_1`, ... as variable names.
67+
68+
```js
69+
// input
70+
var foo = 1;
71+
72+
// output
73+
var slot_0 = 1;
74+
```
75+
76+
```js
77+
// Example
78+
import { minify } from "oxc-minify";
79+
80+
const result = minify("lib.js", code, {
81+
compress: {
82+
mangle: {
83+
debug: true,
84+
},
85+
},
86+
});
87+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Syntax Normalization
2+
3+
Oxc minifier supports transforming syntaxes to make the output shorter and repetitive.
4+
5+
This feature is enabled by default and can be disabled by setting the `compress` option to `false`.
6+
7+
## Target
8+
9+
Oxc minifier uses some syntaxes that are only supported in newer environments. You can specify the target environment by setting the `target` option. The default value is `esnext`, which allows the use of any syntaxes that are supported by the latest ECMAScript standard. The supported value is same as [the `target` option in the transformer](/docs/guide/usage/transformer/lowering#target).
10+
11+
```js
12+
import { minify } from "oxc-minify";
13+
14+
const result = minify("lib.js", code, {
15+
compress: {
16+
target: "chrome87",
17+
},
18+
});
19+
```
20+
21+
## Join Variables
22+
23+
By default, consecutive variable declarations are merged into a single declaration. You can disable this behavior by setting the `compress.joinVars` option to `false`.
24+
25+
```js
26+
// input
27+
var foo = 1;
28+
var bar = 2;
29+
30+
// output
31+
var foo = 1;
32+
var bar = 2;
33+
```
34+
35+
```js
36+
// Example
37+
import { minify } from "oxc-minify";
38+
39+
const result = minify("lib.js", code, {
40+
compress: {
41+
joinVars: false,
42+
},
43+
});
44+
```
45+
46+
## Sequences
47+
48+
By default, consecutive statements are merged into a single statement using the comma operator. You can disable this behavior by setting the `compress.sequences` option to `false`.
49+
50+
```js
51+
// input
52+
foo();
53+
bar();
54+
55+
// output
56+
foo();
57+
bar();
58+
```
59+
60+
```js
61+
// Example
62+
import { minify } from "oxc-minify";
63+
64+
const result = minify("lib.js", code, {
65+
compress: {
66+
sequences: false,
67+
},
68+
});
69+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Whitespace Stripping
2+
3+
Oxc minifier supports removing whitespace and comments.
4+
5+
This feature is enabled by default and can be disabled by setting the `codegen.removeWhitespace` option to `false`.

src/docs/guide/usage/transformer.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- [Transforming TypeScript to JavaScript.](./transformer/typescript)
77
- [Transforming JSX to JavaScript, with built-in React Refresh.](./transformer/jsx)
88
- [Built-in support for popular plugins like styled-components.](./transformer/plugins)
9+
- [Replacing global variables.](./transformer/global-variable-replacement)
910
- [TypeScript Isolated Declarations Emit without using the TypeScript compiler.](./transformer/isolated-declarations)
1011

1112
## Installation

0 commit comments

Comments
 (0)