swc-plugin-evaluate-polyfills 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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alexander Zaytsev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# swc-plugin-evaluate-polyfills
|
|
2
|
+
|
|
3
|
+
An SWC plugin that statically evaluates inline polyfill checks at build time.
|
|
4
|
+
|
|
5
|
+
Many libraries include inline polyfills or fallback implementations to support older browsers. When targeting modern environments, these runtime checks add unnecessary bundle weight. This plugin evaluates those conditions at compile time and strips the redundant fallback logic, allowing bundlers and minifiers to effectively dead-code-eliminate (DCE) the unused code.
|
|
6
|
+
|
|
7
|
+
**Input**
|
|
8
|
+
|
|
9
|
+
```javascript
|
|
10
|
+
// A common polyfill pattern bundled in older libraries
|
|
11
|
+
var assign = Object.assign || function(target) {
|
|
12
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
13
|
+
var source = arguments[i];
|
|
14
|
+
for (var key in source) {
|
|
15
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
16
|
+
target[key] = source[key];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return target;
|
|
21
|
+
};
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Output**
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
// Stripped down to native API usage
|
|
28
|
+
var assign = Object.assign;
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
npm install swc-plugin-evaluate-polyfills
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"jsc": {
|
|
42
|
+
"experimental": {
|
|
43
|
+
"plugins": [
|
|
44
|
+
["swc-plugin-evaluate-polyfills", {}]
|
|
45
|
+
]
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Enable `browser: true` to additionally treat `window` as defined:
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
["swc-plugin-evaluate-polyfills", { "browser": true }]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## What it does
|
|
58
|
+
|
|
59
|
+
The plugin statically resolves common polyfill patterns:
|
|
60
|
+
|
|
61
|
+
- **`||` fallback stripping** — `API || function() {}` → `API`
|
|
62
|
+
- **`undefined` / `void 0` comparisons** — `Object.assign == undefined` → `false`
|
|
63
|
+
- **`typeof` checks** — `"function" == typeof Object.assign` → `true`
|
|
64
|
+
- **`in` operator** — `"replaceAll" in String.prototype` → `true`
|
|
65
|
+
- **`!` / `!!` checks** — `!Date.now` → `false`
|
|
66
|
+
- **`if` dead-code elimination** — removes unreachable branches
|
|
67
|
+
- **`&&` / `||` short-circuit** — folds known boolean operands
|
|
68
|
+
- **`Object.prototype.hasOwnProperty.call`** → `Object.hasOwn`
|
|
69
|
+
- **`globalThis` detection** — collapses global-object detection chains
|
|
70
|
+
- **Scope awareness** — never folds shadowed identifiers
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
[MIT](./LICENSE)
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "swc-plugin-evaluate-polyfills",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "An SWC plugin that statically evaluates inline polyfill checks at build time.",
|
|
6
|
+
"main": "target/wasm32-wasip1/release/swc_plugin_evaluate_polyfills.wasm",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./target/wasm32-wasip1/release/swc_plugin_evaluate_polyfills.wasm"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"target/wasm32-wasip1/release/swc_plugin_evaluate_polyfills.wasm"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "cargo build-wasip1 --release",
|
|
15
|
+
"test": "cargo test",
|
|
16
|
+
"prepack": "npm test && npm run build",
|
|
17
|
+
"fixture": "UPDATE=1 cargo test --test fixture",
|
|
18
|
+
"lint": "cargo clippy -- -D warnings"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"@swc/core": "^1.16.2"
|
|
22
|
+
},
|
|
23
|
+
"peerDependenciesMeta": {
|
|
24
|
+
"@swc/core": {
|
|
25
|
+
"optional": true
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"preferUnplugged": true,
|
|
29
|
+
"author": {
|
|
30
|
+
"name": "Alexander Zaytsev",
|
|
31
|
+
"email": "zaytsev126@gmail.com",
|
|
32
|
+
"url": "https://x.com/_shoonia"
|
|
33
|
+
},
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"keywords": [
|
|
36
|
+
"swc-plugin",
|
|
37
|
+
"optimization",
|
|
38
|
+
"minifier",
|
|
39
|
+
"minify",
|
|
40
|
+
"minimize"
|
|
41
|
+
]
|
|
42
|
+
}
|
|
Binary file
|