-
-
Notifications
You must be signed in to change notification settings - Fork 591
/
types.ts
357 lines (312 loc) · 8.9 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
import webpack, {
EntryOptions,
Stats,
Configuration,
WebpackError,
StatsOptions,
WebpackOptionsNormalized,
Compiler,
MultiCompiler,
Problem,
Argument,
AssetEmittedInfo,
FileCacheOptions,
} from "webpack";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore extraneous import is intended
// eslint-disable-next-line node/no-extraneous-import
import { ClientConfiguration, Configuration as DevServerConfig } from "webpack-dev-server";
import { Colorette } from "colorette";
import { Command, CommandOptions, OptionConstructor, ParseOptions } from "commander";
import { prepare } from "rechoir";
import { stringifyStream } from "@discoveryjs/json-ext";
/**
* Webpack CLI
*/
interface IWebpackCLI {
colors: WebpackCLIColors;
logger: WebpackCLILogger;
isColorSupportChanged: boolean | undefined;
webpack: typeof webpack;
builtInOptionsCache: WebpackCLIBuiltInOption[] | undefined;
program: WebpackCLICommand;
isMultipleCompiler(compiler: WebpackCompiler): compiler is MultiCompiler;
isPromise<T>(value: Promise<T>): value is Promise<T>;
isFunction(value: unknown): value is CallableFunction;
getLogger(): WebpackCLILogger;
createColors(useColors?: boolean): WebpackCLIColors;
toKebabCase: StringFormatter;
capitalizeFirstLetter: StringFormatter;
checkPackageExists(packageName: string): boolean;
getAvailablePackageManagers(): PackageManager[];
getDefaultPackageManager(): PackageManager | undefined;
doInstall(packageName: string, options?: PackageInstallOptions): Promise<string>;
loadJSONFile<T = unknown>(path: Path, handleError: boolean): Promise<T>;
tryRequireThenImport<T = unknown>(module: ModuleName, handleError: boolean): Promise<T>;
makeCommand(
commandOptions: WebpackCLIOptions,
options: WebpackCLICommandOptions,
action: CommandAction,
): Promise<WebpackCLICommand | undefined>;
makeOption(command: WebpackCLICommand, option: WebpackCLIBuiltInOption): void;
run(
args: Parameters<WebpackCLICommand["parseOptions"]>[0],
parseOptions?: ParseOptions,
): Promise<void>;
getBuiltInOptions(): WebpackCLIBuiltInOption[];
loadWebpack(handleError?: boolean): Promise<typeof webpack>;
loadConfig(options: Partial<WebpackDevServerOptions>): Promise<WebpackCLIConfig>;
buildConfig(
config: WebpackCLIConfig,
options: WebpackDevServerOptions,
): Promise<WebpackCLIConfig>;
isValidationError(error: Error): error is WebpackError;
createCompiler(
options: Partial<WebpackDevServerOptions>,
callback?: Callback<[Error | undefined, WebpackCLIStats | undefined]>,
): Promise<WebpackCompiler>;
needWatchStdin(compiler: Compiler | MultiCompiler): boolean;
runWebpack(options: WebpackRunOptions, isWatchCommand: boolean): Promise<void>;
}
interface WebpackCLIColors extends Colorette {
isColorSupported: boolean;
}
interface WebpackCLILogger {
error: LogHandler;
warn: LogHandler;
info: LogHandler;
success: LogHandler;
log: LogHandler;
raw: LogHandler;
}
interface WebpackCLICommandOption extends CommanderOption {
helpLevel?: "minimum" | "verbose";
}
interface WebpackCLIConfig {
options: WebpackConfiguration | WebpackConfiguration[];
path: WeakMap<object, string>;
}
interface WebpackCLICommand extends Command {
pkg: string | undefined;
forHelp: boolean | undefined;
options: WebpackCLICommandOption[];
_args: WebpackCLICommandOption[];
}
interface WebpackCLIStats extends Stats {
presetToOptions?: (item: string | boolean) => StatsOptions;
}
type WebpackCLIMainOption = Pick<
WebpackCLIBuiltInOption,
"description" | "defaultValue" | "multiple"
> & {
flags: string;
type: Set<BooleanConstructor | StringConstructor | NumberConstructor>;
};
interface WebpackCLIOptions extends CommandOptions {
name: string;
alias: string | string[];
description?: string;
usage?: string;
dependencies?: string[];
pkg?: string;
argsDescription?: { [argName: string]: string };
}
type WebpackCLICommandOptions =
| WebpackCLIBuiltInOption[]
| (() => Promise<WebpackCLIBuiltInOption[]>);
interface WebpackCLIBuiltInFlag {
name: string;
alias?: string;
type?: (
value: string,
previous: Record<string, BasicPrimitive | object>,
) => Record<string, BasicPrimitive | object>;
configs?: Partial<FlagConfig>[];
negative?: boolean;
multiple?: boolean;
description: string;
describe?: string;
negatedDescription?: string;
defaultValue?: string;
}
interface WebpackCLIBuiltInOption extends WebpackCLIBuiltInFlag {
hidden?: boolean;
group?: "core";
helpLevel?: "minimum" | "verbose";
}
type WebpackCLIExternalCommandInfo = Pick<WebpackCLIOptions, "name" | "alias" | "description"> & {
pkg: string;
};
/**
* Webpack dev server
*/
type WebpackDevServerOptions = DevServerConfig &
WebpackConfiguration &
ClientConfiguration &
AssetEmittedInfo &
WebpackOptionsNormalized &
FileCacheOptions &
Argv & {
nodeEnv?: "string";
watchOptionsStdin?: boolean;
progress?: boolean | "profile" | undefined;
analyze?: boolean;
prefetch?: string;
json?: boolean;
entry: EntryOptions;
merge?: boolean;
config: string[];
configName?: string[];
argv: Argv;
};
type Callback<T extends unknown[]> = (...args: T) => void;
/**
* Webpack
*/
type WebpackConfiguration = Configuration;
type ConfigOptions = PotentialPromise<WebpackConfiguration | CallableOption>;
type CallableOption = (env: Env | undefined, argv: Argv) => WebpackConfiguration;
type WebpackCompiler = Compiler | MultiCompiler;
type FlagType = boolean | "enum" | "string" | "path" | "number" | "boolean" | "RegExp" | "reset";
type FlagConfig = {
negatedDescription: string;
type: FlagType;
values: FlagType[];
};
type FileSystemCacheOptions = WebpackConfiguration & {
cache: FileCacheOptions & { defaultConfig: string[] };
};
type ProcessedArguments = Record<string, BasicPrimitive | RegExp | (BasicPrimitive | RegExp)[]>;
type MultipleCompilerStatsOptions = StatsOptions & { children: StatsOptions[] };
type CommandAction = Parameters<WebpackCLICommand["action"]>[0];
interface WebpackRunOptions extends WebpackOptionsNormalized {
json?: boolean;
argv?: Argv;
env: Env;
failOnWarnings?: boolean;
}
/**
* Package management
*/
type PackageManager = "pnpm" | "yarn" | "npm";
interface PackageInstallOptions {
preMessage?: () => void;
}
interface BasicPackageJsonContent {
name: string;
version: string;
description: string;
license: string;
}
/**
* Webpack V4
*/
type WebpackV4LegacyStats = Required<WebpackCLIStats>;
interface WebpackV4Compiler extends Compiler {
compiler: Compiler;
}
/**
* Plugins and util types
*/
interface CLIPluginOptions {
configPath?: string;
helpfulOutput: boolean;
hot?: boolean | "only";
progress?: boolean | "profile";
prefetch?: string;
analyze?: boolean;
}
type BasicPrimitive = string | boolean | number;
type Instantiable<InstanceType = unknown, ConstructorParameters extends unknown[] = unknown[]> = {
new (...args: ConstructorParameters): InstanceType;
};
type PotentialPromise<T> = T | Promise<T>;
type ModuleName = string;
type Path = string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type LogHandler = (value: any) => void;
type StringFormatter = (value: string) => string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
interface Argv extends Record<string, any> {
env?: Env;
}
interface Env {
WEBPACK_BUNDLE?: boolean;
WEBPACK_BUILD?: boolean;
WEBPACK_WATCH?: boolean;
WEBPACK_SERVE?: boolean;
WEBPACK_PACKAGE?: string;
WEBPACK_DEV_SERVER_PACKAGE?: string;
}
type DynamicImport<T> = (url: string) => Promise<{ default: T }>;
interface ImportLoaderError extends Error {
code?: string;
}
/**
* External libraries types
*/
type CommanderOption = InstanceType<OptionConstructor>;
interface Rechoir {
prepare: typeof prepare;
}
interface JsonExt {
stringifyStream: typeof stringifyStream;
}
interface RechoirError extends Error {
failures: RechoirError[];
error: Error;
}
interface PromptOptions {
message: string;
defaultResponse: string;
stream: NodeJS.WritableStream;
}
export {
IWebpackCLI,
WebpackCLICommandOption,
WebpackCLIBuiltInOption,
WebpackCLIBuiltInFlag,
WebpackCLIColors,
WebpackCLIStats,
WebpackCLIConfig,
WebpackCLIExternalCommandInfo,
WebpackCLIOptions,
WebpackCLICommand,
WebpackCLICommandOptions,
WebpackCLIMainOption,
WebpackCLILogger,
WebpackV4LegacyStats,
WebpackDevServerOptions,
WebpackRunOptions,
WebpackV4Compiler,
WebpackCompiler,
WebpackConfiguration,
Argv,
Argument,
BasicPrimitive,
BasicPackageJsonContent,
CallableOption,
Callback,
CLIPluginOptions,
CommandAction,
CommanderOption,
CommandOptions,
ConfigOptions,
DynamicImport,
FileSystemCacheOptions,
FlagConfig,
ImportLoaderError,
Instantiable,
JsonExt,
ModuleName,
MultipleCompilerStatsOptions,
PackageInstallOptions,
PackageManager,
Path,
ProcessedArguments,
PromptOptions,
Problem,
PotentialPromise,
Rechoir,
RechoirError,
};