Commit graph

95 commits

Author SHA1 Message Date
7086cmd
f9ae70c74a feat(minifier): minify basic arithmetic calculations. (#6280)
It uses to_string to check which is shorter, which is extremely tough. Waiting for further refactor.
2024-10-07 10:41:06 +00:00
camchenry
4008afe512 feat(minifier): fold array and object constructors (#6257)
This will fold expressions like `new Object()` to `{}`, and `new Array()` to `[]`. Based on the closure compiler tests: b7e380b632/test/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntaxTest.java (L78).

This is outside my usual area, so feedback is welcome.

NOTE: this was previously a full stack of PRs, but Graphite decided to stop working completely for some reason and only gave me this error when I submitted a PR:
```
ERROR: Failed to submit PR for 10-02-feat_minifier_fold_single_arg_new_array_expressions:
{}
```
so I decided to just completely remake this stack and submit as 1 PR.
2024-10-07 06:02:07 +00:00
Boshen
020bb80b65 refactor(codegen)!: change to CodegenReturn::code and CodegenReturn::map (#6310) 2024-10-06 05:05:47 +00:00
7086cmd
37cbabbac4 fix(minifier): should not handle the strict operation for bool comparison. (#6261) 2024-10-03 12:16:10 +00:00
7086cmd
23b646484c feat(minifier): fold true / false comparison. (#6225)
Input:
```js
a == false
```
Previous:
```js
a == !1
```
Current:
```js
a == 0
```

Only handle it when it is non-plus, non-relation binary expressions. Align with [Closure Compiler](https://closure-compiler.appspot.com/home#code%3D%252F%252F%2520%253D%253DClosureCompiler%253D%253D%250A%252F%252F%2520%2540compilation_level%2520SIMPLE_OPTIMIZATIONS%250A%252F%252F%2520%2540output_file_name%2520default.js%250A%252F%252F%2520%2540formatting%2520pretty_print%250A%252F%252F%2520%253D%253D%252FClosureCompiler%253D%253D%250Ax%2520%253C%253C%2520true%253B%250A%250Ax%2520%252B%2520true%253B%250A%250Ax%2520-%2520true%253B%250A%250Ax%2520%257C%2520true%253B%250A%250Ax%2520%2525%2520true%253B%250A%250Ay%2520!%253D%2520false%253B%250A%250Af()%2520%253D%253D%2520false%253B%250A%250Ax%2520instanceof%2520true%250A%250Ax%2520**%2520true%250A%250Ax%2520%2526%2520true%250A%250Ax%2520%255E%2520false%250A%250Ax%2520%253D%253D%2520(x%2520instanceof%2520false)%250A%250Ax%2520instanceof%2520(x%2520%253C%253C%2520true)%250A%250Ax%2520%253D%253D%2520fake(false)).
2024-10-02 13:37:17 +00:00
7086cmd
585ccdad8c feat(minifier): support subtraction assignment. (#6214)
Due to the potential for string concatenation when using the `+=` operator, we should only handle the scenario when using the `-=` operator.
2024-10-02 01:42:56 +00:00
7086cmd
cca0034e8b feat(minifier): handle positive NaN and Infinity. (#6207)
`+NaN` -> `NaN`, `+Infinity` -> `Infinity`.
2024-10-01 10:12:19 +00:00
Boshen
5c323a2105 feat(minifier): loop compressor passes (#6013) 2024-09-24 03:09:35 +00:00
camchenry
02d5637dbc perf(ast-tools): use FxHashMap over std::collections::HashMap (#5997) 2024-09-23 18:28:54 +00:00
Boshen
612f638bcd
chore: change just c to run cargo conformance 2024-09-22 23:50:30 +08:00
Boshen
f4aefb57d8 fix(codegen): print let[0] as (let)[0] (#5947) 2024-09-21 15:09:55 +00:00
Boshen
d901772daa feat(codegen): implement minify number from terser (#5929) 2024-09-20 14:42:28 +00:00
Boshen
6bc13f6cd4 feat(minifier): add MinimizeConditions pass (#5747)
I expect small performance regression.

But managed to improve the following case from react.developmement.js

```
oxc  main ❯ diff before.js after.js
670c670
< 		if (!(dispatcher !== null)) throw Error("Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:\n1. You might have mismatching versions of React and the renderer (such as React DOM)\n2. You might be breaking the Rules of Hooks\n3. You might have more than one copy of React in the same app\nSee https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.");
---
> 		if (dispatcher === null) throw Error("Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:\n1. You might have mismatching versions of React and the renderer (such as React DOM)\n2. You might be breaking the Rules of Hooks\n3. You might have more than one copy of React in the same app\nSee https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.");
```
2024-09-13 08:31:45 +00:00
Boshen
748e6f80fe
chore(minsize): update minsize.snap 2024-09-12 19:02:46 +08:00
Boshen
63a830e08c
chore(dprint): format toml files (#5599)
Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
2024-09-08 14:26:16 +08:00
Boshen
ce4d4698b4 feat(codegen)!: remove const generic MINIFY (#5001)
This is a premature optimization, makes the code complicated, and bloats the final binary size.

The minify option is moved to `CodegenOptions`
2024-08-20 08:13:27 +00:00
Boshen
94d3c31933 fix(minifier): avoid removing function declaration from KeepVar (#4722) 2024-08-07 05:09:32 +00:00
Boshen
a558492bf9 feat(codegen): implement BinaryExpressionVisitor (#4548)
part of https://github.com/oxc-project/backlog/issues/58

`monitor-oxc` run: https://github.com/oxc-project/monitor-oxc/actions/runs/10179047831
binary expression stack length tally using `counts` in top 100 npm packages from monitor-oxc:

```
29772 counts
(  1)    17652 (59.3%, 59.3%): 0
(  2)     5772 (19.4%, 78.7%): 1
(  3)     3204 (10.8%, 89.4%): 2
(  4)     1276 ( 4.3%, 93.7%): 3
(  5)      616 ( 2.1%, 95.8%): 4
(  6)      308 ( 1.0%, 96.8%): 5
(  7)      202 ( 0.7%, 97.5%): 6
(  8)      168 ( 0.6%, 98.1%): 7
(  9)      114 ( 0.4%, 98.5%): 9
( 10)       90 ( 0.3%, 98.8%): 8
( 11)       84 ( 0.3%, 99.0%): 13
( 12)       58 ( 0.2%, 99.2%): 10
( 13)       48 ( 0.2%, 99.4%): 12
( 14)       32 ( 0.1%, 99.5%): 11
( 15)       20 ( 0.1%, 99.6%): 134
( 16)       16 ( 0.1%, 99.6%): 18
( 17)       16 ( 0.1%, 99.7%): 20
( 18)       12 ( 0.0%, 99.7%): 19
( 19)       12 ( 0.0%, 99.8%): 35
( 20)       12 ( 0.0%, 99.8%): 51
( 21)       10 ( 0.0%, 99.8%): 15
( 22)        6 ( 0.0%, 99.9%): 17
( 23)        6 ( 0.0%, 99.9%): 21
( 24)        6 ( 0.0%, 99.9%): 45
( 25)        4 ( 0.0%, 99.9%): 14
( 26)        4 ( 0.0%, 99.9%): 26
( 27)        4 ( 0.0%, 99.9%): 53
( 28)        2 ( 0.0%, 99.9%): 172
( 29)        2 ( 0.0%, 99.9%): 214
( 30)        2 ( 0.0%,100.0%): 22
( 31)        2 ( 0.0%,100.0%): 27
( 32)        2 ( 0.0%,100.0%): 28
( 33)        2 ( 0.0%,100.0%): 29
( 34)        2 ( 0.0%,100.0%): 31
( 35)        2 ( 0.0%,100.0%): 36
( 36)        2 ( 0.0%,100.0%): 46
( 37)        2 ( 0.0%,100.0%): 55
```
2024-07-31 12:44:19 +00:00
Boshen
35654e665c feat(codegen): align operator precedence with esbuild (#4509)
closes #4339
2024-07-28 11:48:51 +00:00
Boshen
6a94e3f573 fix(codegen): fixes for esbuild test cases (#4503) 2024-07-28 08:57:15 +00:00
Boshen
83bd40db4e
chore: turn off doctest for all [[bin]] 2024-07-14 16:55:19 +08:00
Boshen
33e96e23e4 feat(tasks/minsize): include esbuild minified size (#4202) 2024-07-11 13:02:32 +00:00
Boshen
bb646f2bf9 feat(tasks/minsize): show target size for easier comparison (#4201) 2024-07-11 12:38:45 +00:00
Boshen
e3e663bae4 feat(mangler): initialize crate and integrate into minifier (#4197) 2024-07-11 10:35:13 +00:00
Boshen
ca0b4fa08a refactor(tasks): clean up test files and remove libs.txt (#4172) 2024-07-10 17:38:06 +00:00
Boshen
f3a21a28d7
chore: do not compile test crates that have no tests 2024-06-24 00:20:04 +08:00
Boshen
051ceb6539
chore: improve some format by running cargo +nightly fmt 2024-06-19 00:48:30 +08:00
Boshen
5c38a0fd69 feat(codegen)!: new code gen API (#3740)
This PR introduces two type alias to avoid the confusing const generic `pub struct Codegen<'a, const MINIFY: bool>`

* CodeGenerator - Code generator without whitespace removal.
* WhitespaceRemover - Code generator with whitespace removal.

Usage is changed to a builder pattern:

```rust
CodeGenerator::new()
  .enable_comment(...)
  .enable_sourcemap(...)
  .build(&program);
```
2024-06-18 15:50:12 +00:00
Boshen
982e6f08df chore: make println and eprintln opt-in (#3712)
I noticed accidental `println` can be merged, which isn't really nice.
2024-06-17 10:40:34 +00:00
Boshen
b58d8eb88f fix!(codegen): remove the unecessary 4th argument from Codegen::new (#3640) 2024-06-12 07:58:54 +00:00
Boshen
d77ec9f95a
chore: remove trailing whitespaces for all files; add .editorconfig (#3639) 2024-06-12 15:47:26 +08:00
IWANABETHATGUY
0cdb45a1ff
feat(oxc_codegen): preserve annotate comment (#3465)
1. Copy tests from
efa3dd2d8e/internal/bundler_tests/bundler_dce_test.go (L3833-L3971)
2. Add option to preserve annotate comment like `/* #__NO_SIDE_EFFECTS__
*/` and `/* #__PURE__ */`
2024-05-30 15:25:23 +08:00
Boshen
f366d9bd7c
chore(minsize): remove brotlic because it takes too long to compile (#2954) 2024-04-13 13:24:25 +08:00
Boshen
95fc28168c
chore: apply cargo autoinherit (#2826)
See https://github.com/mainmatter/cargo-autoinherit
2024-03-26 23:57:50 +08:00
underfin
d9b77d853b
refactor(sourcemap): change sourcemap name to take a reference (#2779) 2024-03-23 21:40:05 +08:00
Boshen
ef932a3c27
refactor(codegen): clean up API around building sourcemaps (#2602)
closes #2564
2024-03-04 16:03:33 +08:00
Andrew McClenaghan
e6d536cb9b
feat(codegen): configurable typescript codegen (#2443)
- Adds option to `CodegenOptions` - `enable_typescript` to enable output
of TS.
- Stops skipping output that is TS when `enable_typescript` is enabled
- Adds TS support to 
    - Function
    - FormalParameter
    - BindingPattern
 - Adds basic tests for TS generation

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2024-02-20 12:09:28 +08:00
Boshen
70a0076eed
refactor: remove global allocator from non-user facing apps (#2401)
The runtime performance gains does not out weight the compilation speed from
building the custom allocators, which takes about a minute to build on
slower machines.
2024-02-12 14:09:05 +08:00
renovate[bot]
4b83d97c3b
chore(deps): update cargo (#2191) 2024-01-29 11:38:47 +08:00
Boshen
1a576f60a8
refactor(rust): move to workspace lint table (#1444) 2023-11-20 14:38:10 +08:00
Boshen
73d6d40778
rust: do not compile libs and bins that do not have tests (#1342) 2023-11-16 13:35:24 +08:00
Boshen
801d78a3c6
refactor(minifier): make the minifier api only accept an ast (#990) 2023-10-14 00:51:29 +08:00
Boshen
8c12dff8a3
perf: speed tasks run by using a global allocator 2023-10-11 20:30:30 +08:00
Boshen
c38a00b453
chore(minsize): disable the test as it is broken right now 2023-10-10 15:53:55 +08:00
Boshen
ef8aaa7bf1
feat(minifier): re-enable mangler (#972) 2023-10-10 14:32:58 +08:00
Boshen
817f8c295e
ci: turn off doc tests because they are slow to compile and run (#961) 2023-10-07 17:01:23 +08:00
Don Isaac
027a67d94c
feat(minifier): constant addition expression folding (#882)
Fold constant addition expressions. Handles string concatenation and
addition, both with implicit casting.

For example,
```ts
let x = 1 + 1
let y = "hello " + "world"
```
now becomes
```ts
let x = 2
let y = "hello world"
```

## Extra Goodies
- test(minifier): add `test_snapshot` helper to perform snapshot tests
with `insta`
- up(hir): implement `std::ops::Add` for `NumericValue`
- up(span): impl `TryFrom<Cow<'_, &str>>` for `Atom`
2023-09-11 10:38:35 +08:00
Don Isaac
fd899be4a9
feat(minifier): compress undefined variable declarations (#532) 2023-07-10 10:33:42 +08:00
Wenzhe Wang
a32320646d
feat(minfier): try fold and or operator (#472) 2023-06-28 14:58:55 +08:00
阿良仔
1182985bb0
feat(minifier): implement try_fold_shift (#478)
* feat: implement `try_fold_shift`

* update minsize
2023-06-27 04:44:23 +08:00