swc-plugin-minify-catch-param 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# swc-plugin-minify-catch-param
|
|
2
|
+
|
|
3
|
+
SWC Wasm plugin that removes unused simple catch clause parameters.
|
|
4
|
+
|
|
5
|
+
```js
|
|
6
|
+
try {
|
|
7
|
+
// ...
|
|
8
|
+
} catch (error) {
|
|
9
|
+
// ...
|
|
10
|
+
}
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
becomes:
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
try {
|
|
17
|
+
// ...
|
|
18
|
+
} catch {
|
|
19
|
+
// ...
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
The plugin mirrors the Babel plugin behavior for simple identifier catch parameters:
|
|
24
|
+
|
|
25
|
+
- removes only unused identifier parameters
|
|
26
|
+
- preserves used identifier parameters
|
|
27
|
+
- preserves destructuring catch parameters
|
|
28
|
+
- preserves documented conservative cases such as same-name `var` declarations and member-property name matches
|
|
29
|
+
- ignores dynamic string usage such as `eval("console.log(error)")`
|
|
30
|
+
|
|
31
|
+
## Build
|
|
32
|
+
|
|
33
|
+
Install the SWC Wasm target once:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
rustup target add wasm32-wasip1
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Build the plugin:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
cargo build --target wasm32-wasip1 --release
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The generated plugin file is:
|
|
46
|
+
|
|
47
|
+
```text
|
|
48
|
+
target/wasm32-wasip1/release/swc_plugin_minify_catch_param.wasm
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Use With SWC
|
|
52
|
+
|
|
53
|
+
Install SWC in your application:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
npm install --save-dev @swc/core @swc/cli
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Add the plugin to `.swcrc`.
|
|
60
|
+
|
|
61
|
+
For a local build of this repository:
|
|
62
|
+
|
|
63
|
+
```json
|
|
64
|
+
{
|
|
65
|
+
"jsc": {
|
|
66
|
+
"parser": {
|
|
67
|
+
"syntax": "typescript",
|
|
68
|
+
"tsx": true
|
|
69
|
+
},
|
|
70
|
+
"experimental": {
|
|
71
|
+
"plugins": [
|
|
72
|
+
[
|
|
73
|
+
"./target/wasm32-wasip1/release/swc_plugin_minify_catch_param.wasm",
|
|
74
|
+
{}
|
|
75
|
+
]
|
|
76
|
+
]
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Then run SWC:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
npx swc src --out-dir dist
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
If you publish this plugin as an npm package, use the package name instead of the local Wasm path:
|
|
89
|
+
|
|
90
|
+
```json
|
|
91
|
+
{
|
|
92
|
+
"jsc": {
|
|
93
|
+
"experimental": {
|
|
94
|
+
"plugins": [
|
|
95
|
+
["swc-plugin-minify-catch-param", {}]
|
|
96
|
+
]
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Use With Vite
|
|
103
|
+
|
|
104
|
+
Vite does not run arbitrary SWC plugins by default. For React projects, use `@vitejs/plugin-react-swc`, which exposes SWC plugin support through its `plugins` option.
|
|
105
|
+
|
|
106
|
+
Install the Vite React SWC plugin:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
npm install --save-dev @vitejs/plugin-react-swc
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Configure `vite.config.ts` with the locally built Wasm file:
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import { defineConfig } from "vite";
|
|
116
|
+
import react from "@vitejs/plugin-react-swc";
|
|
117
|
+
|
|
118
|
+
export default defineConfig({
|
|
119
|
+
plugins: [
|
|
120
|
+
react({
|
|
121
|
+
plugins: [
|
|
122
|
+
[
|
|
123
|
+
"./target/wasm32-wasip1/release/swc_plugin_minify_catch_param.wasm",
|
|
124
|
+
{},
|
|
125
|
+
],
|
|
126
|
+
],
|
|
127
|
+
}),
|
|
128
|
+
],
|
|
129
|
+
});
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
If the plugin is published to npm, configure it by package name:
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
import { defineConfig } from "vite";
|
|
136
|
+
import react from "@vitejs/plugin-react-swc";
|
|
137
|
+
|
|
138
|
+
export default defineConfig({
|
|
139
|
+
plugins: [
|
|
140
|
+
react({
|
|
141
|
+
plugins: [["swc-plugin-minify-catch-param", {}]],
|
|
142
|
+
}),
|
|
143
|
+
],
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
For non-React Vite projects, use a Vite plugin that delegates transforms to `@swc/core` and accepts normal SWC options, then pass this plugin through `jsc.experimental.plugins`.
|
|
148
|
+
|
|
149
|
+
## Compatibility Notes
|
|
150
|
+
|
|
151
|
+
SWC Wasm plugin compatibility depends on both:
|
|
152
|
+
|
|
153
|
+
- the Rust `swc_core` version used by this crate
|
|
154
|
+
- the `@swc/core` version used by your app or bundler
|
|
155
|
+
|
|
156
|
+
This crate uses `.cargo/config.toml` with `swc_ast_unknown` for modern SWC Wasm plugin compatibility. Prefer `@swc/core` `1.15.0` or newer when possible.
|
|
157
|
+
|
|
158
|
+
## Development
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
cargo fmt --check
|
|
162
|
+
cargo test
|
|
163
|
+
cargo clippy --all-targets -- -D warnings
|
|
164
|
+
cargo build --target wasm32-wasip1 --release
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
The optional smoke test requires a package manager and `@swc/core`:
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
npm install
|
|
171
|
+
npm run test:smoke
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## References
|
|
175
|
+
|
|
176
|
+
- [SWC plugin getting started](https://swc.rs/docs/plugin/ecmascript/getting-started)
|
|
177
|
+
- [SWC Wasm plugin compatibility](https://swc.rs/docs/plugin/ecmascript/compatibility)
|
|
178
|
+
- [Vite plugins: `@vitejs/plugin-react-swc`](https://vite.dev/plugins/)
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "swc-plugin-minify-catch-param",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A SWC plugin to minify unused catch parameters",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "target/wasm32-wasip1/release/swc_plugin_minify_catch_param.wasm",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./target/wasm32-wasip1/release/swc_plugin_minify_catch_param.wasm"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"target/wasm32-wasip1/release/swc_plugin_minify_catch_param.wasm"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "cargo build --target wasm32-wasip1 --release",
|
|
15
|
+
"test:rust": "cargo test",
|
|
16
|
+
"test:smoke": "node tests/smoke/run.mjs",
|
|
17
|
+
"test": "cargo test && npm run build && npm run test:smoke",
|
|
18
|
+
"fixture": "UPDATE=1 cargo test --test fixture"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@swc/core": "^1.16.2"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"@swc/core": "^1.16.2"
|
|
25
|
+
},
|
|
26
|
+
"peerDependenciesMeta": {
|
|
27
|
+
"@swc/core": {
|
|
28
|
+
"optional": true
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"author": {
|
|
32
|
+
"name": "Alexander Zaytsev",
|
|
33
|
+
"email": "zaytsev126@gmail.com",
|
|
34
|
+
"url": "https://x.com/_shoonia"
|
|
35
|
+
},
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"keywords": [
|
|
38
|
+
"swc-plugin"
|
|
39
|
+
]
|
|
40
|
+
}
|
|
Binary file
|