-
Notifications
You must be signed in to change notification settings - Fork 92
/
types.ts
207 lines (175 loc) · 5.33 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
import type { PackageJson } from "pkg-types";
import type { Hookable } from "hookable";
import type { RollupOptions as _RollupOptions, WatcherOptions } from "rollup";
import type { Jiti, JitiOptions } from "jiti";
import type {
RollupBuildEntry,
RollupBuildOptions,
RollupHooks,
RollupOptions,
} from "./builders/rollup/types";
import type { MkdistBuildEntry, MkdistHooks } from "./builders/mkdist/types";
import type { CopyBuildEntry, CopyHooks } from "./builders/copy/types";
import type { UntypedBuildEntry, UntypedHooks } from "./builders/untyped/types";
export interface BaseBuildEntry {
builder?: "untyped" | "rollup" | "mkdist" | "copy";
input: string;
name?: string;
outDir?: string;
declaration?: "compatible" | "node16" | boolean;
}
/** Bundler types */
export type {
RollupBuildEntry,
RollupBuildOptions,
RollupOptions,
} from "./builders/rollup/types";
export type { MkdistBuildEntry } from "./builders/mkdist/types";
export type { CopyBuildEntry } from "./builders/copy/types";
export type {
UntypedBuildEntry,
UntypedOutput,
UntypedOutputs,
} from "./builders/untyped/types";
export type BuildEntry =
| BaseBuildEntry
| RollupBuildEntry
| UntypedBuildEntry
| MkdistBuildEntry
| CopyBuildEntry;
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 BuildHooks
extends CopyHooks,
UntypedHooks,
MkdistHooks,
RollupHooks {
"build:prepare": (ctx: BuildContext) => void | Promise<void>;
"build:before": (ctx: BuildContext) => void | Promise<void>;
"build: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;
}