-
Notifications
You must be signed in to change notification settings - Fork 90
/
types.ts
376 lines (320 loc) · 9.73 KB
/
types.ts
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import type { PackageJson } from "pkg-types";
import type { Hookable } from "hookable";
import type {
RollupOptions as _RollupOptions,
RollupBuild,
OutputOptions,
WatcherOptions,
InputPluginOption,
} from "rollup";
import type { MkdistOptions } from "mkdist";
import type { Schema } from "untyped";
import type { RollupReplaceOptions } from "@rollup/plugin-replace";
import type { RollupAliasOptions } from "@rollup/plugin-alias";
import type { RollupNodeResolveOptions } from "@rollup/plugin-node-resolve";
import type { RollupJsonOptions } from "@rollup/plugin-json";
import type { Options as RollupDtsOptions } from "rollup-plugin-dts";
import type commonjs from "@rollup/plugin-commonjs";
import type { Jiti, JitiOptions } from "jiti";
import type { EsbuildOptions } from "./builders/rollup/plugins/esbuild";
export type RollupCommonJSOptions = Parameters<typeof commonjs>[0] & {};
export interface BaseBuildEntry {
builder?: "untyped" | "rollup" | "mkdist" | "copy";
input: string;
name?: string;
outDir?: string;
declaration?: "compatible" | "node16" | boolean;
}
export interface UntypedBuildEntry extends BaseBuildEntry {
builder: "untyped";
defaults?: Record<string, any>;
}
export interface RollupBuildEntry extends BaseBuildEntry {
builder: "rollup";
}
type _BaseAndMkdist = BaseBuildEntry & MkdistOptions;
export interface MkdistBuildEntry extends _BaseAndMkdist {
builder: "mkdist";
}
export interface CopyBuildEntry extends BaseBuildEntry {
builder: "copy";
pattern?: string | string[];
}
export type BuildEntry =
| BaseBuildEntry
| RollupBuildEntry
| UntypedBuildEntry
| MkdistBuildEntry
| CopyBuildEntry;
export interface RollupBuildOptions {
/**
* If enabled, unbuild generates a CommonJS build in addition to the ESM build.
*/
emitCJS?: boolean;
/**
* Enable experimental active watcher
*
* @experimental
*/
watch?: boolean;
/**
* If enabled, unbuild generates CommonJS polyfills for ESM builds.
*/
cjsBridge?: boolean;
/**
* Preserve dynamic imports as-is
*/
preserveDynamicImports?: boolean;
/**
* Inline dependencies nor explicitly set in "dependencies" or "peerDependencies" or as marked externals to the bundle.
*/
inlineDependencies?: boolean;
/**
* Rollup [Output Options](https://rollupjs.org/configuration-options)
*/
output?: OutputOptions;
/**
* Replace plugin options
* Set to `false` to disable the plugin.
* Read more: [@rollup/plugin-replace](https://www.npmjs.com/package/@rollup/plugin-replace)
*/
replace: RollupReplaceOptions | false;
/**
* Alias plugin options
* Set to `false` to disable the plugin.
* Read more: [@rollup/plugin-alias](https://www.npmjs.com/package/@rollup/plugin-alias)
*/
alias: RollupAliasOptions | false;
/**
* Resolve plugin options
* Set to `false` to disable the plugin.
* Read more: [@rollup/plugin-node-resolve](https://www.npmjs.com/package/@rollup/plugin-node-resolve)
*/
resolve: RollupNodeResolveOptions | false;
/**
* JSON plugin options
* Set to `false` to disable the plugin.
* Read more: [@rollup/plugin-json](https://www.npmjs.com/package/@rollup/plugin-json)
*/
json: RollupJsonOptions | false;
/**
* ESBuild plugin options
* Set to `false` to disable the plugin.
* Read more: [esbuild](https://www.npmjs.com/package/esbuild)
*/
esbuild: EsbuildOptions | false;
/**
* CommonJS plugin options
* Set to `false` to disable the plugin.
* Read more: [@rollup/plugin-commonjs](https://www.npmjs.com/package/@rollup/plugin-commonjs)
*/
commonjs: RollupCommonJSOptions | false;
/**
* DTS plugin options
* Set to `false` to disable the plugin.
* Read more: [rollup-plugin-dts](https://www.npmjs.com/package/rollup-plugin-dts)
*/
dts: RollupDtsOptions;
}
export interface BuildOptions {
/**
* The name of the project.
*/
name: string;
/**
* The root directory of the project.
*/
rootDir: string;
/**
* Build entries.
*/
entries: BuildEntry[];
/**
* Clean the output directory before building.
*/
clean: boolean;
/**
* @experimental
* Generate source mapping file.
*/
sourcemap: boolean;
/**
* Whether to generate declaration files.
* * `compatible` means "src/index.ts" will generate "dist/index.d.mts", "dist/index.d.cts" and "dist/index.d.ts".
* * `node16` means "src/index.ts" will generate "dist/index.d.mts" and "dist/index.d.cts".
* * `true` is equivalent to `compatible`.
* * `false` will disable declaration generation.
* * `undefined` will auto detect based on "package.json". If "package.json" has "types" field, it will be `"compatible"`, otherwise `false`.
*/
declaration?: "compatible" | "node16" | boolean;
/**
* Output directory.
*/
outDir: string;
/**
* Whether to build with JIT stubs.
* Read more: [stubbing](https://antfu.me/posts/publish-esm-and-cjs#stubbing)
*/
stub: boolean;
/**
* Whether to build and actively watch the file changes.
*
* @experimental This feature is experimental and incomplete.
*/
watch: boolean;
/**
* Watch mode options.
*/
watchOptions: WatcherOptions;
/**
* Stub options, where [jiti](https://github.com/unjs/jiti)
* is an object of type `Omit<JitiOptions, "transform" | "onError">`.
*/
stubOptions: { jiti: Omit<JitiOptions, "transform" | "onError"> };
/**
* Used to specify which modules or libraries should be considered external dependencies
* and not included in the final build product.
*/
externals: (string | RegExp)[];
dependencies: string[];
peerDependencies: string[];
devDependencies: string[];
/**
* Create aliases for module imports to reference modules in code using more concise paths.
* Allow you to specify an alias for the module path.
*/
alias: { [find: string]: string };
/**
* Replace the text in the source code with rules.
*/
replace: { [find: string]: string };
/**
* Terminate the build process when a warning appears
*/
failOnWarn?: boolean;
/**
* [Rollup](https://rollupjs.org/configuration-options) Build Options
*/
rollup: RollupBuildOptions;
}
export interface BuildContext {
options: BuildOptions;
pkg: PackageJson;
jiti: Jiti;
buildEntries: {
path: string;
bytes?: number;
exports?: string[];
chunks?: string[];
chunk?: boolean;
modules?: { id: string; bytes: number }[];
}[];
usedImports: Set<string>;
warnings: Set<string>;
hooks: Hookable<BuildHooks>;
}
export type BuildPreset = BuildConfig | (() => BuildConfig);
type DeepPartial<T> = { [P in keyof T]?: DeepPartial<T[P]> };
/**
* In addition to basic `entries`, `presets`, and `hooks`,
* there are also all the properties of `BuildOptions` except for BuildOptions's `entries`.
*/
export interface BuildConfig
extends DeepPartial<Omit<BuildOptions, "entries">> {
/**
* Specify the entry file or entry module during the construction process.
*/
entries?: (BuildEntry | string)[];
/**
* Used to specify the preset build configuration.
*/
preset?: string | BuildPreset;
/**
* Used to define hook functions during the construction process to perform custom operations during specific construction stages.
* This configuration allows you to insert custom logic during the build process to meet specific requirements or perform additional operations.
*/
hooks?: Partial<BuildHooks>;
}
export interface UntypedOutput {
fileName: string;
contents: string;
}
export interface UntypedOutputs {
markdown: UntypedOutput;
schema: UntypedOutput;
defaults: UntypedOutput;
declaration?: UntypedOutput;
}
export interface RollupOptions extends _RollupOptions {
plugins: InputPluginOption[];
}
export interface BuildHooks {
"build:prepare": (ctx: BuildContext) => void | Promise<void>;
"build:before": (ctx: BuildContext) => void | Promise<void>;
"build:done": (ctx: BuildContext) => void | Promise<void>;
"rollup:options": (
ctx: BuildContext,
options: RollupOptions,
) => void | Promise<void>;
"rollup:build": (
ctx: BuildContext,
build: RollupBuild,
) => void | Promise<void>;
"rollup:dts:options": (
ctx: BuildContext,
options: RollupOptions,
) => void | Promise<void>;
"rollup:dts:build": (
ctx: BuildContext,
build: RollupBuild,
) => void | Promise<void>;
"rollup:done": (ctx: BuildContext) => void | Promise<void>;
"mkdist:entries": (
ctx: BuildContext,
entries: MkdistBuildEntry[],
) => void | Promise<void>;
"mkdist:entry:options": (
ctx: BuildContext,
entry: MkdistBuildEntry,
options: MkdistOptions,
) => void | Promise<void>;
"mkdist:entry:build": (
ctx: BuildContext,
entry: MkdistBuildEntry,
output: { writtenFiles: string[] },
) => void | Promise<void>;
"mkdist:done": (ctx: BuildContext) => void | Promise<void>;
"untyped:entries": (
ctx: BuildContext,
entries: UntypedBuildEntry[],
) => void | Promise<void>;
"untyped:entry:options": (
ctx: BuildContext,
entry: UntypedBuildEntry,
options: any,
) => void | Promise<void>;
"untyped:entry:schema": (
ctx: BuildContext,
entry: UntypedBuildEntry,
schema: Schema,
) => void | Promise<void>;
"untyped:entry:outputs": (
ctx: BuildContext,
entry: UntypedBuildEntry,
outputs: UntypedOutputs,
) => void | Promise<void>;
"untyped:done": (ctx: BuildContext) => void | Promise<void>;
"copy:entries": (
ctx: BuildContext,
entries: CopyBuildEntry[],
) => void | Promise<void>;
"copy:done": (ctx: BuildContext) => void | Promise<void>;
}
export function defineBuildConfig(
config: BuildConfig | BuildConfig[],
): BuildConfig[] {
return (Array.isArray(config) ? config : [config]).filter(Boolean);
}
export function definePreset(preset: BuildPreset): BuildPreset {
return preset;
}