swc-plugin-keys 0.1.1
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 +75 -0
- package/index.d.ts +1 -0
- package/index.js +6 -0
- package/package.json +17 -0
- package/swc_plugin_keys.wasm +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# swc-plugin-keys
|
|
2
|
+
|
|
3
|
+
An SWC plugin that transforms `keys<T>()` calls into compile-time arrays of object keys derived from TypeScript type aliases.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install swc-plugin-keys
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Setup
|
|
12
|
+
|
|
13
|
+
Add the plugin to your SWC config (`.swcrc`, `next.config.js`, or programmatic usage):
|
|
14
|
+
|
|
15
|
+
```json
|
|
16
|
+
{
|
|
17
|
+
"jsc": {
|
|
18
|
+
"experimental": {
|
|
19
|
+
"plugins": [["swc-plugin-keys", {}]]
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
Import `keys` and call it with a type argument. The plugin replaces each call with a string array literal at compile time.
|
|
28
|
+
|
|
29
|
+
### Input
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { keys } from 'swc-plugin-keys';
|
|
33
|
+
|
|
34
|
+
type User = {
|
|
35
|
+
name: string;
|
|
36
|
+
age: number;
|
|
37
|
+
email: string;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const userKeys = keys<User>();
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Output
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
const userKeys = ["name", "age", "email"];
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The import is removed and the `keys<User>()` call is replaced with the actual property names from the type alias.
|
|
50
|
+
|
|
51
|
+
## Configuration
|
|
52
|
+
|
|
53
|
+
### `include`
|
|
54
|
+
|
|
55
|
+
An array of glob patterns. When specified, only files whose path matches at least one pattern will be processed. Files that don't match are passed through untouched, avoiding unnecessary work.
|
|
56
|
+
|
|
57
|
+
```json
|
|
58
|
+
{
|
|
59
|
+
"jsc": {
|
|
60
|
+
"experimental": {
|
|
61
|
+
"plugins": [
|
|
62
|
+
["swc-plugin-keys", { "include": ["src/models/**/*.ts"] }]
|
|
63
|
+
]
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
When `include` is omitted or empty, all files are processed (default behavior).
|
|
70
|
+
|
|
71
|
+
## Limitations
|
|
72
|
+
|
|
73
|
+
- Only resolves type aliases defined as type literals (`type Foo = { ... }`). Interfaces, mapped types, and intersection/union types are not supported.
|
|
74
|
+
- The type alias must be defined in the same file as the `keys()` call.
|
|
75
|
+
- When the type argument cannot be resolved, `keys()` falls back to an empty array `[]`.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function keys<T>(): (keyof T)[];
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "swc-plugin-keys",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "SWC plugin that transforms keys<T>() calls into arrays of object keys derived from TypeScript types",
|
|
5
|
+
"main": "swc_plugin_keys.wasm",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"module": "index.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"prepack": "cargo build-wasip1 --release && cp target/wasm32-wasip1/release/swc_plugin_keys.wasm ."
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"swc_plugin_keys.wasm",
|
|
13
|
+
"index.js",
|
|
14
|
+
"index.d.ts"
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT"
|
|
17
|
+
}
|
|
Binary file
|