@rspack/cli 2.0.0 → 2.0.2

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/dist/exit-hook.js CHANGED
@@ -3,11 +3,42 @@ const asyncCallbacks = new Set();
3
3
  const callbacks = new Set();
4
4
  let isCalled = false;
5
5
  let isRegistered = false;
6
+ async function flushStdio() {
7
+ const flush = (stream)=>new Promise((resolve)=>{
8
+ if (!stream || !stream.writable || stream.writableEnded || stream.destroyed) return void resolve();
9
+ const onError = ()=>{
10
+ stream.off('error', onError);
11
+ resolve();
12
+ };
13
+ stream.once('error', onError);
14
+ try {
15
+ stream.write('', ()=>{
16
+ stream.off('error', onError);
17
+ resolve();
18
+ });
19
+ } catch {
20
+ stream.off('error', onError);
21
+ resolve();
22
+ }
23
+ });
24
+ const timeout = new Promise((resolve)=>{
25
+ setTimeout(resolve, 1000);
26
+ });
27
+ await Promise.race([
28
+ Promise.all([
29
+ flush(node_process.stdout),
30
+ flush(node_process.stderr)
31
+ ]),
32
+ timeout
33
+ ]);
34
+ }
6
35
  async function exit(shouldManuallyExit, isSynchronous, signal) {
7
36
  if (isCalled) return;
8
37
  isCalled = true;
9
38
  if (asyncCallbacks.size > 0 && isSynchronous) console.error("SYNCHRONOUS TERMINATION NOTICE: When explicitly exiting the process via process.exit or via a parent process, asynchronous tasks in your exitHooks will not run. Either remove these tasks, use gracefulExit() instead of process.exit(), or ensure your parent process sends a SIGINT to the process running this code.");
10
- const exitCode = 128 + signal;
39
+ let exitCode = 0;
40
+ if (signal > 0) exitCode = 128 + signal;
41
+ else if ('number' == typeof node_process.exitCode || 'string' == typeof node_process.exitCode) exitCode = node_process.exitCode;
11
42
  const done = (force = false)=>{
12
43
  if (true === force || true === shouldManuallyExit) node_process.exit(exitCode);
13
44
  };
@@ -19,11 +50,12 @@ async function exit(shouldManuallyExit, isSynchronous, signal) {
19
50
  forceAfter = Math.max(forceAfter, wait);
20
51
  promises.push(Promise.resolve(callback(exitCode)));
21
52
  }
22
- const asyncTimer = setTimeout(()=>{
53
+ const asyncTimer = forceAfter > 0 ? setTimeout(()=>{
23
54
  done(true);
24
- }, forceAfter);
55
+ }, forceAfter) : void 0;
25
56
  await Promise.all(promises);
26
57
  clearTimeout(asyncTimer);
58
+ await flushStdio();
27
59
  done();
28
60
  }
29
61
  function addHook(options) {
package/dist/index.d.ts CHANGED
@@ -1,2 +1,301 @@
1
- export { defineConfig, definePlugin, RspackCLI } from './cli';
2
- export * from './types';
1
+ import type { Compiler } from '@rspack/core';
2
+ import { Configuration } from '@rspack/core';
3
+ import type { MultiCompiler } from '@rspack/core';
4
+ import type { MultiRspackOptions } from '@rspack/core';
5
+ import type { MultiStats } from '@rspack/core';
6
+ import type { RspackOptions } from '@rspack/core';
7
+ import type { RspackPluginFunction } from '@rspack/core';
8
+ import type { RspackPluginInstance } from '@rspack/core';
9
+ import type { Stats } from '@rspack/core';
10
+
11
+ declare class CAC extends EventTarget {
12
+ /** The program name to display in help and version message */
13
+ name: string;
14
+ commands: Command[];
15
+ globalCommand: GlobalCommand;
16
+ matchedCommand?: Command;
17
+ matchedCommandName?: string;
18
+ /**
19
+ * Raw CLI arguments
20
+ */
21
+ rawArgs: string[];
22
+ /**
23
+ * Parsed CLI arguments
24
+ */
25
+ args: ParsedArgv["args"];
26
+ /**
27
+ * Parsed CLI options, camelCased
28
+ */
29
+ options: ParsedArgv["options"];
30
+ showHelpOnExit?: boolean;
31
+ showVersionOnExit?: boolean;
32
+ /**
33
+ * @param name The program name to display in help and version message
34
+ */
35
+ constructor(name?: string);
36
+ /**
37
+ * Add a global usage text.
38
+ *
39
+ * This is not used by sub-commands.
40
+ */
41
+ usage(text: string): this;
42
+ /**
43
+ * Add a sub-command
44
+ */
45
+ command(rawName: string, description?: string, config?: CommandConfig): Command;
46
+ /**
47
+ * Add a global CLI option.
48
+ *
49
+ * Which is also applied to sub-commands.
50
+ */
51
+ option(rawName: string, description: string, config?: OptionConfig): this;
52
+ /**
53
+ * Show help message when `-h, --help` flags appear.
54
+ *
55
+ */
56
+ help(callback?: HelpCallback): this;
57
+ /**
58
+ * Show version number when `-v, --version` flags appear.
59
+ *
60
+ */
61
+ version(version: string, customFlags?: string): this;
62
+ /**
63
+ * Add a global example.
64
+ *
65
+ * This example added here will not be used by sub-commands.
66
+ */
67
+ example(example: CommandExample): this;
68
+ /**
69
+ * Output the corresponding help message
70
+ * When a sub-command is matched, output the help message for the command
71
+ * Otherwise output the global one.
72
+ *
73
+ */
74
+ outputHelp(): void;
75
+ /**
76
+ * Output the version number.
77
+ *
78
+ */
79
+ outputVersion(): void;
80
+ private setParsedInfo;
81
+ unsetMatchedCommand(): void;
82
+ /**
83
+ * Parse argv
84
+ */
85
+ parse(argv?: string[], {
86
+ run
87
+ }?: {
88
+ /** Whether to run the action for matched command */run?: boolean | undefined;
89
+ }): ParsedArgv;
90
+ private mri;
91
+ runMatchedCommand(): any;
92
+ }
93
+
94
+ declare class Command {
95
+ rawName: string;
96
+ description: string;
97
+ config: CommandConfig;
98
+ cli: CAC;
99
+ options: Option_2[];
100
+ aliasNames: string[];
101
+ name: string;
102
+ args: CommandArg[];
103
+ commandAction?: (...args: any[]) => any;
104
+ usageText?: string;
105
+ versionNumber?: string;
106
+ examples: CommandExample[];
107
+ helpCallback?: HelpCallback;
108
+ globalCommand?: GlobalCommand;
109
+ constructor(rawName: string, description: string, config: CommandConfig | undefined, cli: CAC);
110
+ usage(text: string): this;
111
+ allowUnknownOptions(): this;
112
+ ignoreOptionDefaultValue(): this;
113
+ version(version: string, customFlags?: string): this;
114
+ example(example: CommandExample): this;
115
+ /**
116
+ * Add a option for this command
117
+ * @param rawName Raw option name(s)
118
+ * @param description Option description
119
+ * @param config Option config
120
+ */
121
+ option(rawName: string, description: string, config?: OptionConfig): this;
122
+ alias(name: string): this;
123
+ action(callback: (...args: any[]) => any): this;
124
+ /**
125
+ * Check if a command name is matched by this command
126
+ * @param name Command name
127
+ */
128
+ isMatched(name: string): boolean;
129
+ get isDefaultCommand(): boolean;
130
+ get isGlobalCommand(): boolean;
131
+ /**
132
+ * Check if an option is registered in this command
133
+ * @param name Option name
134
+ */
135
+ hasOption(name: string): Option_2 | undefined;
136
+ outputHelp(): void;
137
+ outputVersion(): void;
138
+ checkRequiredArgs(): void;
139
+ /**
140
+ * Check if the parsed options contain any unknown options
141
+ *
142
+ * Exit and output error when true
143
+ */
144
+ checkUnknownOptions(): void;
145
+ /**
146
+ * Check if the required string-type options exist
147
+ */
148
+ checkOptionValue(): void;
149
+ /**
150
+ * Check if the number of args is more than expected
151
+ */
152
+ checkUnusedArgs(): void;
153
+ }
154
+
155
+ declare type Command_2 = 'serve' | 'build';
156
+
157
+ declare interface CommandArg {
158
+ required: boolean;
159
+ value: string;
160
+ variadic: boolean;
161
+ }
162
+
163
+ declare interface CommandConfig {
164
+ allowUnknownOptions?: boolean;
165
+ ignoreOptionDefaultValue?: boolean;
166
+ }
167
+
168
+ declare type CommandExample = ((bin: string) => string) | string;
169
+
170
+ declare type CommonOptions = {
171
+ config?: string;
172
+ configName?: string[];
173
+ configLoader?: ConfigLoader;
174
+ env?: Record<string, unknown> | string[];
175
+ nodeEnv?: string;
176
+ };
177
+
178
+ declare type CommonOptionsForBuildAndServe = CommonOptions & {
179
+ devtool?: string | boolean;
180
+ entry?: string[];
181
+ mode?: string;
182
+ outputPath?: string;
183
+ watch?: boolean;
184
+ };
185
+
186
+ declare type ConfigLoader = 'auto' | 'jiti' | 'native';
187
+
188
+ export { Configuration }
189
+
190
+ /**
191
+ * This function helps you to autocomplete configuration types.
192
+ * It accepts a Rspack config object, or a function that returns a config.
193
+ */
194
+ export declare function defineConfig(config: RspackOptions): RspackOptions;
195
+
196
+ export declare function defineConfig(config: MultiRspackOptions): MultiRspackOptions;
197
+
198
+ export declare function defineConfig(config: RspackConfigFn): RspackConfigFn;
199
+
200
+ export declare function defineConfig(config: RspackConfigAsyncFn): RspackConfigAsyncFn;
201
+
202
+ export declare function defineConfig(config: RspackConfigExport): RspackConfigExport;
203
+
204
+ export declare function definePlugin(plugin: RspackPluginFunction): RspackPluginFunction;
205
+
206
+ export declare function definePlugin(plugin: RspackPluginInstance): RspackPluginInstance;
207
+
208
+ declare class GlobalCommand extends Command {
209
+ constructor(cli: CAC);
210
+ }
211
+
212
+ declare type HelpCallback = (sections: HelpSection[]) => void | HelpSection[];
213
+
214
+ declare interface HelpSection {
215
+ title?: string;
216
+ body: string;
217
+ }
218
+
219
+ export declare type LogHandler = (value: any) => void;
220
+
221
+ declare class Option_2 {
222
+ rawName: string;
223
+ description: string;
224
+ /** Option name */
225
+ name: string;
226
+ /** Option name and aliases */
227
+ names: string[];
228
+ isBoolean?: boolean;
229
+ required?: boolean;
230
+ config: OptionConfig;
231
+ negated: boolean;
232
+ constructor(rawName: string, description: string, config?: OptionConfig);
233
+ }
234
+
235
+ declare interface OptionConfig {
236
+ default?: any;
237
+ type?: any[];
238
+ }
239
+
240
+ declare interface ParsedArgv {
241
+ args: ReadonlyArray<string>;
242
+ options: {
243
+ [k: string]: any;
244
+ };
245
+ }
246
+
247
+ export declare class RspackCLI {
248
+ colors: RspackCLIColors;
249
+ program: CAC;
250
+ _actionPromise: Promise<void> | undefined;
251
+ constructor();
252
+ /**
253
+ * Wraps an async action handler so its promise is captured and can be
254
+ * awaited in `run()`. CAC's `parse()` does not await async actions,
255
+ * so without this wrapper, rejections become unhandled.
256
+ */
257
+ wrapAction<T extends (...args: any[]) => Promise<void>>(fn: T): T;
258
+ buildCompilerConfig(options: CommonOptionsForBuildAndServe, rspackCommand: Command_2): Promise<RspackOptions | MultiRspackOptions>;
259
+ createCompiler(config: RspackOptions | MultiRspackOptions, callback?: (e: Error | null, res?: Stats | MultiStats) => void): Promise<MultiCompiler | Compiler | null>;
260
+ private createColors;
261
+ getLogger(): RspackCLILogger;
262
+ run(argv: string[]): Promise<void>;
263
+ private registerCommands;
264
+ private buildConfig;
265
+ loadConfig(options: CommonOptions): Promise<{
266
+ config: RspackOptions | MultiRspackOptions;
267
+ pathMap: WeakMap<RspackOptions, string[]>;
268
+ }>;
269
+ private filterConfig;
270
+ isMultipleCompiler(compiler: Compiler | MultiCompiler): compiler is MultiCompiler;
271
+ isWatch(compiler: Compiler | MultiCompiler): boolean;
272
+ }
273
+
274
+ export declare interface RspackCLIColors {
275
+ isColorSupported: boolean;
276
+ red(text: string): string;
277
+ yellow(text: string): string;
278
+ cyan(text: string): string;
279
+ green(text: string): string;
280
+ }
281
+
282
+ export declare interface RspackCLILogger {
283
+ error: LogHandler;
284
+ warn: LogHandler;
285
+ info: LogHandler;
286
+ success: LogHandler;
287
+ log: LogHandler;
288
+ raw: LogHandler;
289
+ }
290
+
291
+ export declare interface RspackCommand {
292
+ apply(cli: RspackCLI): Promise<void>;
293
+ }
294
+
295
+ declare type RspackConfigAsyncFn = (env: Record<string, any>, argv: Record<string, any>) => Promise<RspackOptions | MultiRspackOptions>;
296
+
297
+ declare type RspackConfigExport = RspackOptions | MultiRspackOptions | RspackConfigFn | RspackConfigAsyncFn;
298
+
299
+ declare type RspackConfigFn = (env: Record<string, any>, argv: Record<string, any>) => RspackOptions | MultiRspackOptions;
300
+
301
+ export { }