-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathREADME.md
219 lines (133 loc) · 4.33 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# 🧶 knitwork
<!-- automd:badges color=yellow codecov -->
[![npm version](https://img.shields.io/npm/v/knitwork?color=yellow)](https://npmjs.com/package/knitwork)
[![npm downloads](https://img.shields.io/npm/dm/knitwork?color=yellow)](https://npmjs.com/package/knitwork)
[![codecov](https://img.shields.io/codecov/c/gh/unjs/knitwork?color=yellow)](https://codecov.io/gh/unjs/knitwork)
<!-- /automd -->
Utilities to generate JavaScript code.
## Install
<!-- automd:pm-install -->
```sh
# ✨ Auto-detect
npx nypm install knitwork
# npm
npm install knitwork
# yarn
yarn add knitwork
# pnpm
pnpm install knitwork
# bun
bun install knitwork
```
<!-- /automd -->
<!-- automd:jsimport cjs cdn -->
**ESM** (Node.js, Bun)
```js
import {} from "knitwork";
```
**CommonJS** (Legacy Node.js)
```js
const {} = require("knitwork");
```
**CDN** (Deno, Bun and Browsers)
```js
import {} from "https://esm.sh/knitwork";
```
<!-- /automd -->
<!-- automd:jsdocs src=./src/index.ts -->
## ESM
### `genDynamicImport(specifier, options)`
Generate an ESM dynamic `import()` statement.
### `genExport(specifier, exports?, options)`
Generate an ESM `export` statement.
### `genImport(specifier, imports?, options)`
Generate an ESM `import` statement.
**Example:**
```js
genImport("pkg", "foo");
// ~> `import foo from "pkg";`
genImport("pkg", ["foo"]);
// ~> `import { foo } from "pkg";`
genImport("pkg", ["a", "b"]);
// ~> `import { a, b } from "pkg`;
genImport("pkg", [{ name: "default", as: "bar" }]);
// ~> `import { default as bar } from "pkg`;
genImport("pkg", [{ name: "foo", as: "bar" }]);
// ~> `import { foo as bar } from "pkg`;
genImport("pkg", "foo", { attributes: { type: "json" } });
// ~> `import foo from "pkg" with { type: "json" };
genExport("pkg", "foo");
// ~> `export foo from "pkg";`
genExport("pkg", ["a", "b"]);
// ~> `export { a, b } from "pkg";`
// export * as bar from "pkg"
genExport("pkg", { name: "*", as: "bar" });
// ~> `export * as bar from "pkg";`
```
### `genTypeImport(specifier, imports, options)`
Generate an ESM `import type` statement.
## Serialization
### `genArrayFromRaw(array, indent, options)`
Serialize an array to a string.
**Example:**
```js
genArrayFromRaw([1, 2, 3])
// ~> `[1, 2, 3]`
```
### `genObjectFromRaw(object, indent, options)`
Serialize an object to a string.
**Example:**
```js
genObjectFromValues({ foo: "bar", test: '() => import("pkg")' })
// ~> `{ foo: bar, test: () => import("pkg") }`
```
### `genObjectFromRawEntries(array, indent, options)`
Serialize an array of key-value pairs to a string.
### `genObjectFromValues(obj, indent, options)`
Serialize an object to a string.
**Example:**
```js
genObjectFromValues({ foo: "bar" })
// ~> `{ foo: "bar" }`
```
## String
### `escapeString(id)`
Escape a string for use in a javascript string.
### `genSafeVariableName(name)`
Generate a safe javascript variable name.
### `genString(input, options)`
Generate a string with double or single quotes and handle escapes.
## Typescript
### `genAugmentation(specifier)`
Generate typescript `declare module` augmentation.
### `genInlineTypeImport(specifier, name, options)`
Generate an typescript `typeof import()` statement for default import.
### `genInterface(name, contents?, options, indent)`
Generate typescript interface.
### `genTypeExport(specifier, imports, options)`
Generate a typescript `export type` statement.
### `genTypeObject(object, indent)`
Generate typescript object type.
<!-- /automd -->
## Contribution
<details>
<summary>Local development</summary>
- Clone this repository
- Install the latest LTS version of [Node.js](https://nodejs.org/en/)
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
- Install dependencies using `bun install`
- Run tests using `bun dev`
</details>
## License
<!-- automd:contributors license=MIT author="pi0,danielroe" -->
Published under the [MIT](https://github.com/unjs/knitwork/blob/main/LICENSE) license.
Made by [@pi0](https://github.com/pi0), [@danielroe](https://github.com/danielroe) and [community](https://github.com/unjs/knitwork/graphs/contributors) 💛
<br><br>
<a href="https://github.com/unjs/knitwork/graphs/contributors">
<img src="https://contrib.rocks/image?repo=unjs/knitwork" />
</a>
<!-- /automd -->
<!-- automd:with-automd -->
---
_🤖 auto updated with [automd](https://automd.unjs.io)_
<!-- /automd -->