Skip to content

Commit 476a5b5

Browse files
committed
docs: add to readme
1 parent 42eff14 commit 476a5b5

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

README.md

+51-3
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,47 @@ export function main() {
121121

122122
</td></tr></tbody></table>
123123

124+
### Class Tree Shaking
125+
126+
> One of the hardest but the coolest.
127+
128+
<table><tbody><tr><td width="500px"> Input </td><td width="500px"> Output </td></tr><tr>
129+
<td valign="top">
130+
131+
```js
132+
class A {
133+
method(x) {
134+
console.log("A", x);
135+
}
136+
static static_prop = unknown;
137+
}
138+
class B extends A {
139+
method(x) {
140+
console.log("B", x);
141+
}
142+
unused() {
143+
console.log("unused");
144+
}
145+
}
146+
new B().method(A.static_prop);
147+
```
148+
149+
</td><td valign="top">
150+
151+
```js
152+
class A {
153+
static a = unknown;
154+
}
155+
class B extends A {
156+
a(x) {
157+
console.log("B", x);
158+
}
159+
}
160+
new B().a(A.a);
161+
```
162+
163+
</td></tr></tbody></table>
164+
124165
### JSX
125166

126167
> `createElement` also works, if it is directly imported from `react`.
@@ -204,9 +245,16 @@ export function main() {
204245

205246
## Comparison
206247

207-
- **Rollup**: Rollup tree-shakes the code in a multi-module context, while this project is focused on a single module. For some cases, this project can remove 10% more code than Rollup.
208-
- **Closure Compiler**: Closure Compiler can be considered as a tree shaker + minifier, while this project is only a tree shaker (for the minifier, we have `oxc_minifier`). Theoretically, we can shake more than Closure Compiler, but we cannot compare them directly because we don't have a equivalent minifier. Also, it's written in Java, which is hard to be integrated into the JS ecosystem.
209-
- **swc**: swc can also be considered as a tree shaker + minifier. TBH, currently swc is much faster and more complete. It is rule-based, which is a different approach from this project. It's also not compatible with the Oxc project, thus a new tree shaker is needed.
248+
- **Rollup**: Rollup tree-shakes the code in a multi-module context, and it has information about the side effects of the modules. This project does a more fine-grained tree-shaking, and it can be used as a post-processor for Rollup, and is expected to produce smaller code.
249+
- **Closure Compiler**/**swc**: they support both minification and dead code elimination, while this project is focused on tree-shaking (difference below). You can expect a size reduction when using tree-shaker on their output, and vice versa.
250+
251+
### What's Tree Shaking?
252+
253+
Here is a simple comparison:
254+
255+
- Minification: Removing whitespace, renaming variables, syntax-level optimizations, etc.
256+
- Dead code elimination: Removing code that is never executed, by using a set of rules, for example, "`if(false) { ... }` can be removed".
257+
- Tree shaking: Removing code that is never executed, by simulating the runtime behavior of the code. For example, "`if (x) { ... }` can only be preserved if `...` is reachable and has side effects".
210258

211259
## Todo
212260

0 commit comments

Comments
 (0)