@remotex-labs/xbuild 1.5.5 → 1.5.7
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/cli.d.ts +2776 -2
- package/dist/cli.js +1 -1
- package/dist/cli.js.map +3 -3
- package/dist/index.d.ts +2721 -9
- package/dist/index.js +15 -15
- package/dist/index.js.map +4 -4
- package/package.json +8 -8
- package/dist/components/banner.component.d.ts +0 -53
- package/dist/components/colors.component.d.ts +0 -59
- package/dist/components/entry-points.component.d.ts +0 -65
- package/dist/components/package-type.component.d.ts +0 -34
- package/dist/configuration/default.configuration.d.ts +0 -20
- package/dist/configuration/interfaces/configuration.interface.d.ts +0 -420
- package/dist/configuration/parse.configuration.d.ts +0 -28
- package/dist/errors/base.error.d.ts +0 -40
- package/dist/errors/esbuild.error.d.ts +0 -55
- package/dist/errors/interfaces/stack.interface.d.ts +0 -55
- package/dist/errors/stack.error.d.ts +0 -20
- package/dist/errors/types.error.d.ts +0 -26
- package/dist/errors/uncaught.error.d.ts +0 -25
- package/dist/errors/vm-runtime.error.d.ts +0 -61
- package/dist/errors/xbuild.error.d.ts +0 -28
- package/dist/plugins/ifdef.plugin.d.ts +0 -13
- package/dist/plugins/interfaces/plugin.interface.d.ts +0 -16
- package/dist/plugins/macro.plugin.d.ts +0 -93
- package/dist/plugins/resolve-alias.plugin.d.ts +0 -35
- package/dist/providers/configuration.provider.d.ts +0 -75
- package/dist/providers/interfaces/plugins.interfaces.d.ts +0 -56
- package/dist/providers/interfaces/typescript-provider.interface.d.ts +0 -20
- package/dist/providers/plugins.provider.d.ts +0 -226
- package/dist/providers/server.provider.d.ts +0 -208
- package/dist/providers/typescript.provider.d.ts +0 -491
- package/dist/services/build.service.d.ts +0 -338
- package/dist/services/cli.service.d.ts +0 -23
- package/dist/services/interfaces/cli.interface.d.ts +0 -39
- package/dist/services/interfaces/transpiler.interface.d.ts +0 -37
- package/dist/services/process.service.d.ts +0 -55
- package/dist/services/transpiler.service.d.ts +0 -80
- package/dist/services/vm.service.d.ts +0 -37
package/dist/cli.d.ts
CHANGED
|
@@ -1,5 +1,2779 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import type { SourceService } from "@remotex-labs/xmap";
|
|
3
|
+
import type { Loader, OnLoadArgs, BuildResult, OnEndResult, PluginBuild, OnLoadResult, OnResolveArgs, OnResolveResult, BuildOptions, Format, Metafile, Message } from "esbuild";
|
|
4
|
+
import type { IncomingMessage, ServerResponse } from "http";
|
|
5
|
+
import type { Argv } from "yargs";
|
|
6
|
+
import type { ChildProcessWithoutNullStreams } from "child_process";
|
|
7
|
+
import type { ParsedCommandLine } from "typescript";
|
|
8
|
+
import type { Context } from "vm";
|
|
2
9
|
/**
|
|
3
|
-
*
|
|
10
|
+
* Represents an enhanced error type that extends the built-in Error object.
|
|
11
|
+
* This type adds an optional property to store call stack information.
|
|
12
|
+
*
|
|
13
|
+
* @type ErrorType
|
|
14
|
+
*
|
|
15
|
+
* @extends Error
|
|
16
|
+
*
|
|
17
|
+
* @property callStacks - An optional array of call sites
|
|
18
|
+
* captured when the error was created. This can provide additional context
|
|
19
|
+
* regarding the call stack at the time of the error, useful for debugging.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const myError: ErrorType = new Error("Something went wrong!");
|
|
24
|
+
* myError.callStacks = getCallStack(); // Assuming getCallStack captures call sites.
|
|
25
|
+
* console.error(myError);
|
|
26
|
+
* ```
|
|
4
27
|
*/
|
|
5
|
-
export {
|
|
28
|
+
export type ErrorType = Error & {
|
|
29
|
+
callStacks?: Array<NodeJS.CallSite>;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Represents the state of a stack trace, containing information about the error, associated code, and formatted error message.
|
|
33
|
+
*
|
|
34
|
+
* @interface StackTraceStateInterface
|
|
35
|
+
* @property error - The error object with attached `callStacks`.
|
|
36
|
+
* @property blockCode - The block of code (if any) related to the error, or `null` if unavailable.
|
|
37
|
+
* @property formattedError - A formatted string representing the error details.
|
|
38
|
+
*/
|
|
39
|
+
export interface StackTraceStateInterface {
|
|
40
|
+
error: ErrorType & BaseError;
|
|
41
|
+
blockCode: null | string;
|
|
42
|
+
formattedError: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Represents detailed information about a specific frame in the call stack.
|
|
46
|
+
*
|
|
47
|
+
* @interface FrameDetailsInterface
|
|
48
|
+
* @property line - The line number where the frame occurred.
|
|
49
|
+
* @property column - The column number where the frame occurred.
|
|
50
|
+
* @property source - The source file path where the frame occurred.
|
|
51
|
+
* @property functionName - The name of the function being executed at this frame, or an empty string if not available.
|
|
52
|
+
*/
|
|
53
|
+
export interface FrameDetailsInterface {
|
|
54
|
+
line: number;
|
|
55
|
+
column: number;
|
|
56
|
+
source: string;
|
|
57
|
+
functionName: string;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* An enumeration of ANSI color codes used for text formatting in the terminal.
|
|
61
|
+
*
|
|
62
|
+
* These colors can be used to format terminal output with various text colors,
|
|
63
|
+
* including different shades of gray, yellow, and orange, among others.
|
|
64
|
+
*
|
|
65
|
+
* Each color code starts with an ANSI escape sequence (`\u001B`), followed by the color code.
|
|
66
|
+
* The `Reset` option can be used to reset the terminal's text formatting back to the default.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* console.log(Color.BrightPink, 'This is bright pink text', Color.Reset);
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export declare const enum Colors {
|
|
74
|
+
Reset = "\u001B[0m",
|
|
75
|
+
Red = "\u001B[38;5;9m",
|
|
76
|
+
Gray = "\u001B[38;5;243m",
|
|
77
|
+
Cyan = "\u001B[38;5;81m",
|
|
78
|
+
DarkGray = "\u001B[38;5;238m",
|
|
79
|
+
LightCoral = "\u001B[38;5;203m",
|
|
80
|
+
LightOrange = "\u001B[38;5;215m",
|
|
81
|
+
OliveGreen = "\u001B[38;5;149m",
|
|
82
|
+
BurntOrange = "\u001B[38;5;208m",
|
|
83
|
+
LightGoldenrodYellow = "\u001B[38;5;221m",
|
|
84
|
+
LightYellow = "\u001B[38;5;230m",
|
|
85
|
+
CanaryYellow = "\u001B[38;5;227m",
|
|
86
|
+
DeepOrange = "\u001B[38;5;166m",
|
|
87
|
+
LightGray = "\u001B[38;5;252m",
|
|
88
|
+
BrightPink = "\u001B[38;5;197m"
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Formats a message string with the specified ANSI color and optionally resets it after the message.
|
|
92
|
+
*
|
|
93
|
+
* This function applies an ANSI color code to the provided message,
|
|
94
|
+
* and then appends the reset code to ensure that the color formatting doesn't extend beyond the message.
|
|
95
|
+
* It's useful for outputting colored text in a terminal. If color formatting is not desired,
|
|
96
|
+
* the function can return the message unformatted.
|
|
97
|
+
*
|
|
98
|
+
* @param color - The ANSI color code to apply. This is used only if `activeColor` is true.
|
|
99
|
+
* @param msg - The message to be formatted with the specified color.
|
|
100
|
+
* @param activeColor - A boolean flag indicating whether color formatting should be applied. Default is `__ACTIVE_COLOR`.
|
|
101
|
+
*
|
|
102
|
+
* @returns A string with the specified color applied to the message,
|
|
103
|
+
* followed by a reset sequence if `activeColor` is true.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* const coloredMessage = setColor(Colors.LightOrange, 'This is a light orange message');
|
|
108
|
+
* console.log(coloredMessage);
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* const plainMessage = setColor(Colors.LightOrange, 'This is a light orange message', false);
|
|
114
|
+
* console.log(plainMessage); // Output will be without color formatting
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export declare function setColor(color: Colors, msg: string, activeColor?: boolean): string;
|
|
118
|
+
export declare const xBuildLazy: {
|
|
119
|
+
readonly service: SourceService;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Prepares the error stack trace for display.
|
|
123
|
+
*
|
|
124
|
+
* This function overrides the default stack trace preparation to provide a custom format,
|
|
125
|
+
* including enhanced stack trace information and error details.
|
|
126
|
+
*
|
|
127
|
+
* @param error - The error object (Error or BaseError).
|
|
128
|
+
* @param stackEntries - The array of stack entries from the call stack.
|
|
129
|
+
* @returns The formatted stack trace as a string.
|
|
130
|
+
*/
|
|
131
|
+
export declare function formatStackTrace(error: ErrorType & BaseError, stackEntries: Array<NodeJS.CallSite>): string;
|
|
132
|
+
/**
|
|
133
|
+
* A base class for custom errors with enhanced stack trace formatting and source code information.
|
|
134
|
+
*
|
|
135
|
+
* The `BaseError` class extends the native `Error` class, adding functionality to format the error stack
|
|
136
|
+
* trace and include details from a source map service. This is useful for debugging errors in compiled
|
|
137
|
+
* or transpiled code by providing clearer information about the source of the error.
|
|
138
|
+
*/
|
|
139
|
+
export declare abstract class BaseError extends Error {
|
|
140
|
+
readonly sourceMap?: SourceService | undefined;
|
|
141
|
+
callStacks: Array<NodeJS.CallSite>;
|
|
142
|
+
/**
|
|
143
|
+
* Creates a new instance of `BaseError`.
|
|
144
|
+
*
|
|
145
|
+
* This constructor initializes a new `BaseError` instance by setting the error message and formatting
|
|
146
|
+
* the stack trace using the provided source map information. It also ensures the stack trace is maintained
|
|
147
|
+
* correctly by using `Error.captureStackTrace` (if available). The default source map service is used if
|
|
148
|
+
* none is provided.
|
|
149
|
+
*
|
|
150
|
+
* @param message - A descriptive error message to be associated with the error.
|
|
151
|
+
* @param sourceMap - (Optional) The `SourceService` instance used to format and resolve the stack trace.
|
|
152
|
+
* If not provided, the default source map service (`defaultSourceService`) is used.
|
|
153
|
+
*/
|
|
154
|
+
protected constructor(message: string, sourceMap?: SourceService | undefined);
|
|
155
|
+
/**
|
|
156
|
+
* Reformats the error stack trace using source map information.
|
|
157
|
+
*
|
|
158
|
+
* This function enhances the original error stack trace by attempting to map each entry
|
|
159
|
+
* back to its original position in the source file using the provided source map service.
|
|
160
|
+
* If the source map information is not available, it returns the original stack trace.
|
|
161
|
+
*
|
|
162
|
+
* @param error - The original error with stack trace of the error.
|
|
163
|
+
* @returns The reformatted stack trace or the original stack trace if no mapping is available.
|
|
164
|
+
*/
|
|
165
|
+
protected reformatStack(error: ErrorType): string;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Represents an error specific to the xBuild process.
|
|
169
|
+
*
|
|
170
|
+
* The `xBuildError` class extends the `BaseError` class to provide a custom error type for the xBuild system.
|
|
171
|
+
* It includes additional functionality to maintain stack trace information and assigns a specific name to
|
|
172
|
+
* the error, making it easier to identify and handle in different parts of the application.
|
|
173
|
+
*
|
|
174
|
+
* @augments BaseError
|
|
175
|
+
*/
|
|
176
|
+
export declare class xBuildError extends BaseError {
|
|
177
|
+
/**
|
|
178
|
+
* Original error stack
|
|
179
|
+
*/
|
|
180
|
+
originalErrorStack: string | undefined;
|
|
181
|
+
/**
|
|
182
|
+
* Creates an instance of `xBuildError`.
|
|
183
|
+
*
|
|
184
|
+
* @param message - The error message that describes the error. This message is passed to the base class
|
|
185
|
+
* `BaseError` constructor and is used to provide context about the nature of the error.
|
|
186
|
+
* @param options - Optional configuration for the error. This can include additional properties or settings
|
|
187
|
+
* that customize the error's behavior.
|
|
188
|
+
*/
|
|
189
|
+
constructor(message: string, options?: ErrorOptions);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* A custom error class to handle errors occurring within a virtual machine (VM) execution context.
|
|
193
|
+
*
|
|
194
|
+
* The `VMRuntimeError` class extends the native `Error` class and enhances the error with
|
|
195
|
+
* source map information to map stack traces back to the original source. This is particularly
|
|
196
|
+
* useful when debugging errors from code executed in a `vm` or `evalmachine` environment.
|
|
197
|
+
*
|
|
198
|
+
* @param message - The error message describing the error.
|
|
199
|
+
* @param originalError - The original error object thrown from the VM execution.
|
|
200
|
+
* @param sourceMap - The `SourceService` providing source map data to link the error to its original source.
|
|
201
|
+
*
|
|
202
|
+
* @example
|
|
203
|
+
* ```ts
|
|
204
|
+
* try {
|
|
205
|
+
* vm.run(someCode);
|
|
206
|
+
* } catch (error) {
|
|
207
|
+
* throw new VMRuntimeError("VM execution failed", error, sourceMapService);
|
|
208
|
+
* }
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
export declare class VMRuntimeError extends BaseError {
|
|
212
|
+
/**
|
|
213
|
+
* The original error thrown during the VM execution.
|
|
214
|
+
*/
|
|
215
|
+
originalError: Error;
|
|
216
|
+
/**
|
|
217
|
+
* Original error stack
|
|
218
|
+
*/
|
|
219
|
+
originalErrorStack: string | undefined;
|
|
220
|
+
/**
|
|
221
|
+
* Creates a new VMRuntimeError instance.
|
|
222
|
+
*
|
|
223
|
+
* This constructor initializes a new `VMRuntimeError` object, extending the native `Error` class with
|
|
224
|
+
* additional information, including the original error and optional source map data. It also ensures that
|
|
225
|
+
* the stack trace is correctly captured and reformatted using the source map (if provided) to enhance
|
|
226
|
+
* debugging.
|
|
227
|
+
*
|
|
228
|
+
* @param originalError - The original error object that was thrown during the VM execution.
|
|
229
|
+
* @param sourceMap - (Optional) The source map service used to map the error stack trace to its original
|
|
230
|
+
* source code locations. If not provided, this will be `null`.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```ts
|
|
234
|
+
* try {
|
|
235
|
+
* vm.run(code);
|
|
236
|
+
* } catch (error) {
|
|
237
|
+
* throw new VMRuntimeError(error, sourceMapService);
|
|
238
|
+
* }
|
|
239
|
+
* ```
|
|
240
|
+
*/
|
|
241
|
+
constructor(originalError: ErrorType, sourceMap?: SourceService);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Interface for the build state that users can modify.
|
|
245
|
+
*
|
|
246
|
+
* This interface allows users to store and manage any custom data related to the build process.
|
|
247
|
+
*
|
|
248
|
+
* @template T - The type of values that can be stored in the state.
|
|
249
|
+
*/
|
|
250
|
+
export interface BuildState {
|
|
251
|
+
[key: string]: unknown;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* A type that defines the possible return values of a plugin function.
|
|
255
|
+
*
|
|
256
|
+
* The function can return a Promise that resolves to `null` or `void`, or it can return `null` or `void` directly.
|
|
257
|
+
*/
|
|
258
|
+
export type pluginResultType = Promise<null | void> | null | void;
|
|
259
|
+
/**
|
|
260
|
+
* Defines the signature of a function that is called at the end of the build process.
|
|
261
|
+
*
|
|
262
|
+
* @param result - The `BuildResult` object that contains information about the outcome of the build process.
|
|
263
|
+
* @param state - The current build state that users can modify.
|
|
264
|
+
* @returns A `pluginResultType`, which may include asynchronous operations.
|
|
265
|
+
*/
|
|
266
|
+
export type OnEndType = (result: BuildResult, state: BuildState) => pluginResultType | OnEndResult | Promise<OnEndResult>;
|
|
267
|
+
/**
|
|
268
|
+
* Defines the signature of a function that is called at the start of the build process.
|
|
269
|
+
*
|
|
270
|
+
* @param build - The `PluginBuild` object that contains information about the build process and allows modifying build options.
|
|
271
|
+
* @param state - The current build state that users can modify.
|
|
272
|
+
* @returns A `pluginResultType`, which may include asynchronous operations.
|
|
273
|
+
*/
|
|
274
|
+
export type OnStartType = (build: PluginBuild, state: BuildState) => pluginResultType | OnEndResult | Promise<OnEndResult>;
|
|
275
|
+
/**
|
|
276
|
+
* Defines the signature of a function that is called during the resolution of an import path.
|
|
277
|
+
*
|
|
278
|
+
* @param args - The `OnResolveArgs` object, containing information about the file being resolved, such as its path, importer, namespace, etc.
|
|
279
|
+
* @param state - The current build state that users can modify.
|
|
280
|
+
* @returns A `Promise` or a direct `OnResolveResult` which can modify the resolved path, or a `pluginResultType` for
|
|
281
|
+
* performing additional async tasks without altering resolution.
|
|
282
|
+
*/
|
|
283
|
+
export type OnResolveType = (args: OnResolveArgs, state: BuildState) => Promise<OnResolveResult | pluginResultType> | OnResolveResult | pluginResultType;
|
|
284
|
+
/**
|
|
285
|
+
* Defines the signature of a function that is called when a file is loaded.
|
|
286
|
+
*
|
|
287
|
+
* @param content - The content of the file being loaded, as either a `string` or `Uint8Array`.
|
|
288
|
+
* @param loader - The type of loader used for the file, such as `js`, `ts`, `json`, or others. It can also be `undefined`.
|
|
289
|
+
* @param args - The `OnLoadArgs` object, containing information about the file being loaded, such as its path, namespace, etc.
|
|
290
|
+
* @param state - The current build state that users can modify.
|
|
291
|
+
* @returns A `Promise` or direct `OnLoadResult`, which can modify the file content and loader, or a `pluginResultType`
|
|
292
|
+
* for performing additional async tasks without altering the content.
|
|
293
|
+
*/
|
|
294
|
+
export type OnLoadType = (content: string | Uint8Array, loader: Loader | undefined, args: OnLoadArgs, state: BuildState) => Promise<OnLoadResult | pluginResultType> | OnLoadResult | pluginResultType;
|
|
295
|
+
/**
|
|
296
|
+
* Represents the format for specifying entry points in TypeScript declaration generation.
|
|
297
|
+
*
|
|
298
|
+
* This type allows for various formats to specify the entry points from which TypeScript declaration files should be generated.
|
|
299
|
+
* The supported formats are:
|
|
300
|
+
* - `Array<string>`: An array of file paths as strings. Each string represents a path to a TypeScript entry point file.
|
|
301
|
+
* - `Record<string, string>`: An object where each key-value pair represents an entry point.
|
|
302
|
+
*
|
|
303
|
+
* The key is a name or identifier, and the value is the file path to the TypeScript entry point.
|
|
304
|
+
* - `Array<{ in: string, out: string }>`: An array of objects, where each object specifies an input file path (`in`)
|
|
305
|
+
* and an output file path (`out`). This format allows for specifying where each entry point file is located and
|
|
306
|
+
* where its corresponding declaration file should be output.
|
|
307
|
+
*
|
|
308
|
+
* Example usage:
|
|
309
|
+
*
|
|
310
|
+
* ```ts
|
|
311
|
+
* const entryPoints1: EntryPoints = ['src/index.ts', 'src/utils.ts'];
|
|
312
|
+
* const entryPoints2: EntryPoints = { main: 'src/index.ts', utils: 'src/utils.ts' };
|
|
313
|
+
* const entryPoints3: EntryPoints = [{ in: 'src/index.ts', out: 'dist/index.d.ts' }, { in: 'src/utils.ts', out: 'dist/utils.d.ts' }];
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
export type EntryPoints = Array<string> | Record<string, string> | Array<{
|
|
317
|
+
in: string;
|
|
318
|
+
out: string;
|
|
319
|
+
}> | undefined;
|
|
320
|
+
/**
|
|
321
|
+
* Represents a deeply nested partial version of a given type `T`.
|
|
322
|
+
*
|
|
323
|
+
* This type utility allows for partial objects at any level of nesting.
|
|
324
|
+
* It recursively makes all properties optional and applies the same behavior to nested objects.
|
|
325
|
+
*
|
|
326
|
+
* **Example Usage:**
|
|
327
|
+
*
|
|
328
|
+
* ```ts
|
|
329
|
+
* interface User {
|
|
330
|
+
* name: string;
|
|
331
|
+
* address: {
|
|
332
|
+
* street: string;
|
|
333
|
+
* city: string;
|
|
334
|
+
* };
|
|
335
|
+
* }
|
|
336
|
+
*
|
|
337
|
+
* // PartialDeep<User> will allow the following:
|
|
338
|
+
* const partialUser: PartialDeep<User> = {
|
|
339
|
+
* name: 'Alice', // 'name' is optional
|
|
340
|
+
* address: {
|
|
341
|
+
* city: 'Wonderland' // 'street' is optional
|
|
342
|
+
* }
|
|
343
|
+
* };
|
|
344
|
+
* ```
|
|
345
|
+
*
|
|
346
|
+
* @template T - The type to be made partially optional and deeply nested.
|
|
347
|
+
*
|
|
348
|
+
* @typeParam T - The base type to apply the partial transformation.
|
|
349
|
+
*
|
|
350
|
+
* @example
|
|
351
|
+
* ```
|
|
352
|
+
* type MyPartial = PartialDeep<{ a: number; b: { c: string; d: { e: boolean } } }>;
|
|
353
|
+
* // MyPartial will be equivalent to:
|
|
354
|
+
* // {
|
|
355
|
+
* // a?: number;
|
|
356
|
+
* // b?: {
|
|
357
|
+
* // c?: string;
|
|
358
|
+
* // d?: {
|
|
359
|
+
* // e?: boolean;
|
|
360
|
+
* // }
|
|
361
|
+
* // }
|
|
362
|
+
* // }
|
|
363
|
+
* ```
|
|
364
|
+
*/
|
|
365
|
+
export type PartialDeep<T> = {
|
|
366
|
+
[P in keyof T]?: T[P] extends object ? PartialDeep<T[P]> : T[P];
|
|
367
|
+
};
|
|
368
|
+
/**
|
|
369
|
+
* Represents a module with its exports and an optional default export.
|
|
370
|
+
*
|
|
371
|
+
* This interface provides a structure to define and interact with the exports of a module.
|
|
372
|
+
* It includes both named and default exports, where default exports are of a specific type.
|
|
373
|
+
*
|
|
374
|
+
* @interface ModuleInterface
|
|
375
|
+
*
|
|
376
|
+
* @property exports - An object representing the exports of the module.
|
|
377
|
+
* The keys are strings that represent the names of the exports, and the values can be of any type.
|
|
378
|
+
*
|
|
379
|
+
* @property exports[key: string] - A dictionary where each key is a string representing the export name,
|
|
380
|
+
* and the associated value can be of any type.
|
|
381
|
+
*
|
|
382
|
+
* @property [exports.default] - An optional default export.
|
|
383
|
+
* The default export, if present, is of type `ConfigurationInterface`.
|
|
384
|
+
*/
|
|
385
|
+
export interface ModuleInterface {
|
|
386
|
+
/**
|
|
387
|
+
* An object representing the exports of the module.
|
|
388
|
+
* The keys are strings representing export names, and the values can be of any type.
|
|
389
|
+
*
|
|
390
|
+
* @property default - An optional default export of type `ConfigurationInterface`.
|
|
391
|
+
*/
|
|
392
|
+
exports: {
|
|
393
|
+
[key: string]: unknown;
|
|
394
|
+
default?: ConfigurationInterface;
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Configuration options for the serve the build.
|
|
399
|
+
*
|
|
400
|
+
* This object allows you to specify various settings related to the server,
|
|
401
|
+
* such as the port, host, SSL/TLS certificates, and request handling functions.
|
|
402
|
+
*
|
|
403
|
+
* @example
|
|
404
|
+
* ```ts
|
|
405
|
+
* const serverConfig = {
|
|
406
|
+
* serve: {
|
|
407
|
+
* active: true,
|
|
408
|
+
* port: 8080,
|
|
409
|
+
* host: 'localhost',
|
|
410
|
+
* keyfile: '/path/to/ssl/keyfile.pem',
|
|
411
|
+
* certfile: '/path/to/ssl/certfile.pem',
|
|
412
|
+
* onStart: () => {
|
|
413
|
+
* console.log('Server started');
|
|
414
|
+
* }
|
|
415
|
+
* onRequest: (req, res, next) => {
|
|
416
|
+
* console.log('Server request received');
|
|
417
|
+
* next();
|
|
418
|
+
* }
|
|
419
|
+
* }
|
|
420
|
+
* };
|
|
421
|
+
* ```
|
|
422
|
+
*
|
|
423
|
+
* @public
|
|
424
|
+
*/
|
|
425
|
+
export interface Serve {
|
|
426
|
+
port: number;
|
|
427
|
+
host: string;
|
|
428
|
+
active: boolean;
|
|
429
|
+
keyfile?: string;
|
|
430
|
+
certfile?: string;
|
|
431
|
+
onRequest?: (req: IncomingMessage, res: ServerResponse, next: () => void) => void;
|
|
432
|
+
onStart?: () => void;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Defines the lifecycle hooks used in the plugin system.
|
|
436
|
+
*
|
|
437
|
+
* This interface specifies the types for various hooks that can be registered
|
|
438
|
+
* to customize the behavior of the build process. Each hook corresponds to a
|
|
439
|
+
* specific stage in the lifecycle of an esbuild operation.
|
|
440
|
+
*
|
|
441
|
+
* @interface hooks
|
|
442
|
+
*
|
|
443
|
+
* @property onEnd - A hook function that is called after the build process completes.
|
|
444
|
+
* This allows for post-processing or cleanup tasks.
|
|
445
|
+
* @property onLoad - A hook function that is called when esbuild attempts to load a module.
|
|
446
|
+
* It can be used to modify the contents of the loaded module.
|
|
447
|
+
* @property onStart - A hook function that is called before the build process starts.
|
|
448
|
+
* This is useful for initialization tasks or logging.
|
|
449
|
+
* @property onResolve - A hook function that is called when esbuild attempts to resolve a module path.
|
|
450
|
+
* It can be used to customize module resolution behavior.
|
|
451
|
+
*
|
|
452
|
+
* @example
|
|
453
|
+
* ```ts
|
|
454
|
+
* const myHooks: hooks = {
|
|
455
|
+
* onEnd: async (result) => {
|
|
456
|
+
* console.log('Build finished:', result);
|
|
457
|
+
* },
|
|
458
|
+
* onLoad: async (contents, loader, args) => {
|
|
459
|
+
* // Modify contents if necessary
|
|
460
|
+
* return { contents, loader };
|
|
461
|
+
* },
|
|
462
|
+
* onStart: async (build) => {
|
|
463
|
+
* console.log('Build started:', build);
|
|
464
|
+
* },
|
|
465
|
+
* onResolve: async (args) => {
|
|
466
|
+
* if (args.path === 'my-module') {
|
|
467
|
+
* return { path: './src/my-module.ts' };
|
|
468
|
+
* }
|
|
469
|
+
* return null;
|
|
470
|
+
* }
|
|
471
|
+
* };
|
|
472
|
+
* ```
|
|
473
|
+
*
|
|
474
|
+
* @see OnEndType
|
|
475
|
+
* @see OnLoadType
|
|
476
|
+
* @see OnStartType
|
|
477
|
+
* @see OnResolveType
|
|
478
|
+
*/
|
|
479
|
+
export interface hooks {
|
|
480
|
+
onEnd: OnEndType;
|
|
481
|
+
onLoad: OnLoadType;
|
|
482
|
+
onStart: OnStartType;
|
|
483
|
+
onSuccess: OnEndType;
|
|
484
|
+
onResolve: OnResolveType;
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Represents the configuration options for the build and development process.
|
|
488
|
+
*
|
|
489
|
+
* This interface defines various settings that control how the application is built and run, including development mode,
|
|
490
|
+
* file watching, TypeScript declaration generation, error handling, TypeScript type checking, and esbuild bundler options.
|
|
491
|
+
*
|
|
492
|
+
* @example
|
|
493
|
+
* ```ts
|
|
494
|
+
* const config: ConfigurationInterface = {
|
|
495
|
+
* dev: true,
|
|
496
|
+
* watch: true,
|
|
497
|
+
* declaration: true,
|
|
498
|
+
* buildOnError: false,
|
|
499
|
+
* noTypeChecker: false,
|
|
500
|
+
* esbuild: {
|
|
501
|
+
* entryPoints: ['./src/index.ts'],
|
|
502
|
+
* bundle: true,
|
|
503
|
+
* minify: true,
|
|
504
|
+
* target: 'es2020'
|
|
505
|
+
* },
|
|
506
|
+
* hooks: {
|
|
507
|
+
* onStart: async (build) => {
|
|
508
|
+
* console.log('Build started');
|
|
509
|
+
* },
|
|
510
|
+
* onEnd: async (result) => {
|
|
511
|
+
* console.log('Build finished:', result);
|
|
512
|
+
* }
|
|
513
|
+
* }
|
|
514
|
+
* };
|
|
515
|
+
* ```
|
|
516
|
+
*
|
|
517
|
+
* In this example, the configuration sets the application to development mode with file watching enabled,
|
|
518
|
+
* generates TypeScript declaration files, continues building on TypeScript errors, and includes esbuild options for bundling and minification.
|
|
519
|
+
* Additionally, custom hooks are provided to log messages at the start and end of the build process.
|
|
520
|
+
*
|
|
521
|
+
* @public
|
|
522
|
+
* @category Configuration
|
|
523
|
+
*/
|
|
524
|
+
export interface ConfigurationInterface {
|
|
525
|
+
/**
|
|
526
|
+
* Build and run entryPoint for development
|
|
527
|
+
*/
|
|
528
|
+
dev: boolean | Array<string>;
|
|
529
|
+
/**
|
|
530
|
+
* Enables watching for file changes during development.
|
|
531
|
+
*/
|
|
532
|
+
watch: boolean;
|
|
533
|
+
/**
|
|
534
|
+
* The directory where the generated `package.json` file will be saved,
|
|
535
|
+
* indicating the module type (`"commonjs"` or `"module"`).
|
|
536
|
+
*
|
|
537
|
+
* - If the format is `esm`, the `package.json` file will contain `"type": "module"`.
|
|
538
|
+
* - If the format is `cjs`, the `package.json` file will contain `"type": "commonjs"`.
|
|
539
|
+
*
|
|
540
|
+
* If this field is not set (`undefined`), the `package.json` file will be saved in the
|
|
541
|
+
* `outdir` specified in the esbuild configuration.
|
|
542
|
+
*
|
|
543
|
+
* Example:
|
|
544
|
+
*
|
|
545
|
+
* ```ts
|
|
546
|
+
* {
|
|
547
|
+
* esbuild: {
|
|
548
|
+
* outdir: 'dist',
|
|
549
|
+
* format: 'esm'
|
|
550
|
+
* },
|
|
551
|
+
* moduleTypeOutDir: 'custom/dist'
|
|
552
|
+
* }
|
|
553
|
+
* // This will create 'custom/dist/package.json' with the content: {"type": "module"}
|
|
554
|
+
*
|
|
555
|
+
* // If moduleTypeOutDir is not provided:
|
|
556
|
+
* {
|
|
557
|
+
* esbuild: {
|
|
558
|
+
* outdir: 'dist',
|
|
559
|
+
* format: 'cjs'
|
|
560
|
+
* }
|
|
561
|
+
* }
|
|
562
|
+
* // This will create 'dist/package.json' with the content: {"type": "commonjs"}
|
|
563
|
+
* ```
|
|
564
|
+
*/
|
|
565
|
+
moduleTypeOutDir?: string;
|
|
566
|
+
/**
|
|
567
|
+
* Generates TypeScript declaration files.
|
|
568
|
+
*/
|
|
569
|
+
declaration: boolean;
|
|
570
|
+
/**
|
|
571
|
+
* Bundle declaration file
|
|
572
|
+
*/
|
|
573
|
+
bundleDeclaration: boolean;
|
|
574
|
+
/**
|
|
575
|
+
* Overrides the output directory for TypeScript declaration files (.d.ts).
|
|
576
|
+
*
|
|
577
|
+
* If this option is not set, the output directory specified in the `outDir`
|
|
578
|
+
* field of your `tsconfig.json` will be used.
|
|
579
|
+
* This allows for custom control
|
|
580
|
+
* over where the declaration files are emitted, separate from the main
|
|
581
|
+
* output directory for compiled JavaScript files.
|
|
582
|
+
*
|
|
583
|
+
* @default The `outDir` from `tsconfig.json` will be used if this is not provided.
|
|
584
|
+
*/
|
|
585
|
+
declarationOutDir?: string;
|
|
586
|
+
/**
|
|
587
|
+
* Continues building even if TypeScript type errors are present.
|
|
588
|
+
*/
|
|
589
|
+
buildOnError: boolean;
|
|
590
|
+
/**
|
|
591
|
+
* Skips TypeScript type checking.
|
|
592
|
+
*/
|
|
593
|
+
noTypeChecker: boolean;
|
|
594
|
+
/**
|
|
595
|
+
* Options for the esbuild bundler.
|
|
596
|
+
*/
|
|
597
|
+
esbuild: BuildOptions;
|
|
598
|
+
/**
|
|
599
|
+
* Option for the serve the build over http/s
|
|
600
|
+
*/
|
|
601
|
+
serve: Serve;
|
|
602
|
+
/**
|
|
603
|
+
* lifecycle hooks to customize the build process.
|
|
604
|
+
*
|
|
605
|
+
* This property allows you to provide implementations for various hooks defined in the `hooks` interface.
|
|
606
|
+
* Using `Partial<hooks>` means you can specify only the hooks you want to implement,
|
|
607
|
+
* while the others will default to `undefined`.
|
|
608
|
+
*/
|
|
609
|
+
hooks?: Partial<hooks>;
|
|
610
|
+
/**
|
|
611
|
+
* A dictionary of define options for the build process.
|
|
612
|
+
*
|
|
613
|
+
* This property allows you to specify global constants that can be replaced during the build process.
|
|
614
|
+
* Each key-value pair in the `define` object represents a constant where the key is the name of the
|
|
615
|
+
* constant, and the value is the string to replace it with. This is particularly useful for feature flags,
|
|
616
|
+
* environment-specific configurations, or any other value that you may want to define at compile time.
|
|
617
|
+
*
|
|
618
|
+
* @example
|
|
619
|
+
* ```ts
|
|
620
|
+
* const config: ConfigurationInterface = {
|
|
621
|
+
* dev: true,
|
|
622
|
+
* define: {
|
|
623
|
+
* 'process.env.NODE_ENV': 'development',
|
|
624
|
+
* 'API_URL': 'https://api.example.com'
|
|
625
|
+
* }
|
|
626
|
+
* };
|
|
627
|
+
* ```
|
|
628
|
+
*
|
|
629
|
+
* In this example, the constants `process.env.NODE_ENV` and `API_URL` will be replaced with their
|
|
630
|
+
* corresponding values during the build, making it easy to manage different configurations across
|
|
631
|
+
* various environments.
|
|
632
|
+
*
|
|
633
|
+
* @public
|
|
634
|
+
*/
|
|
635
|
+
define: Record<string, unknown>;
|
|
636
|
+
/** Documentation: https://esbuild.github.io/api/#banner */
|
|
637
|
+
banner?: {
|
|
638
|
+
[type: string]: string | (() => string);
|
|
639
|
+
};
|
|
640
|
+
/** Documentation: https://esbuild.github.io/api/#footer */
|
|
641
|
+
footer?: {
|
|
642
|
+
[type: string]: string | (() => string);
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
interface ExportedConfigurationInterface extends ConfigurationInterface {
|
|
646
|
+
/**
|
|
647
|
+
* Options for the esbuild bundler.
|
|
648
|
+
*/
|
|
649
|
+
esbuild: Omit<BuildOptions, 'plugins' | 'define'>;
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* Type alias for a partial configuration object.
|
|
653
|
+
*
|
|
654
|
+
* This type represents a configuration where all properties of the
|
|
655
|
+
* `ConfigurationInterface` are optional. It allows for flexible configuration
|
|
656
|
+
* objects where only a subset of properties need to be specified.
|
|
657
|
+
*/
|
|
658
|
+
export type xBuildConfig = PartialDeep<ExportedConfigurationInterface>;
|
|
659
|
+
/**
|
|
660
|
+
* Represents a partially deep configuration type based on the `ConfigurationInterface`.
|
|
661
|
+
*
|
|
662
|
+
* This type is used to define configurations that may have some properties
|
|
663
|
+
* missing or undefined. It leverages the `PartialDeep` utility type to allow
|
|
664
|
+
* for flexibility in configuration management.
|
|
665
|
+
*/
|
|
666
|
+
export type PartialDeepConfigurationsType = PartialDeep<ConfigurationInterface>;
|
|
667
|
+
/**
|
|
668
|
+
* Defines the possible types for configurations.
|
|
669
|
+
*
|
|
670
|
+
* This type can either be a single instance of `PartialDeepConfigurationsType`
|
|
671
|
+
* or an array of such instances. This flexibility allows for configurations
|
|
672
|
+
* to be specified as a single object or as multiple objects, enabling
|
|
673
|
+
* support for various build setups.
|
|
674
|
+
*
|
|
675
|
+
* @example
|
|
676
|
+
* ```ts
|
|
677
|
+
* // A single configuration object
|
|
678
|
+
* const config: ConfigurationsType = {
|
|
679
|
+
* esbuild: {
|
|
680
|
+
* bundle: true,
|
|
681
|
+
* outdir: 'dist'
|
|
682
|
+
* }
|
|
683
|
+
* };
|
|
684
|
+
* ```
|
|
685
|
+
*
|
|
686
|
+
* @example
|
|
687
|
+
* ```ts
|
|
688
|
+
* // An array of configuration objects
|
|
689
|
+
* const configs: ConfigurationsType = [
|
|
690
|
+
* {
|
|
691
|
+
* esbuild: {
|
|
692
|
+
* bundle: true,
|
|
693
|
+
* outdir: 'dist/esm'
|
|
694
|
+
* }
|
|
695
|
+
* },
|
|
696
|
+
* {
|
|
697
|
+
* esbuild: {
|
|
698
|
+
* bundle: false,
|
|
699
|
+
* outdir: 'dist/cjs',
|
|
700
|
+
* declaration: false,
|
|
701
|
+
* noTypeChecker: true
|
|
702
|
+
* }
|
|
703
|
+
* }
|
|
704
|
+
* ];
|
|
705
|
+
* ```
|
|
706
|
+
*/
|
|
707
|
+
export type ConfigurationsType = PartialDeepConfigurationsType | Array<PartialDeepConfigurationsType>;
|
|
708
|
+
/**
|
|
709
|
+
* Interface representing the command-line arguments for the build tool.
|
|
710
|
+
*
|
|
711
|
+
* @interface ArgvInterface
|
|
712
|
+
* @property typeCheck - Flag indicating if the tool should perform type checking only.
|
|
713
|
+
* @property node - Flag indicating if the build is intended for Node.js environment.
|
|
714
|
+
* @property file - The entry file(s) to build.
|
|
715
|
+
* @property dev - List of development-related options for the build.
|
|
716
|
+
* @property debug - List of debugging-related options for the build.
|
|
717
|
+
* @property serve - Flag indicating if an HTTP server should be started for the build folder.
|
|
718
|
+
* @property outdir - The output directory for the build files.
|
|
719
|
+
* @property declaration - Flag indicating if TypeScript declaration files should be generated.
|
|
720
|
+
* @property watch - Flag indicating if the build should watch for file changes.
|
|
721
|
+
* @property config - Path to the build configuration file (JavaScript or TypeScript).
|
|
722
|
+
* @property tsconfig - Path to the TypeScript configuration file to use.
|
|
723
|
+
* @property minify - Flag indicating if the code should be minified.
|
|
724
|
+
* @property bundle - Flag indicating if the code should be bundled.
|
|
725
|
+
* @property format - Defines the formats for the build output.
|
|
726
|
+
*/
|
|
727
|
+
export interface ArgvInterface {
|
|
728
|
+
typeCheck: boolean;
|
|
729
|
+
node: boolean;
|
|
730
|
+
file: string;
|
|
731
|
+
dev: Array<string>;
|
|
732
|
+
debug: Array<string>;
|
|
733
|
+
serve: boolean;
|
|
734
|
+
outdir: string;
|
|
735
|
+
declaration: boolean;
|
|
736
|
+
watch: boolean;
|
|
737
|
+
config: string;
|
|
738
|
+
tsconfig: string;
|
|
739
|
+
minify: boolean;
|
|
740
|
+
bundle: boolean;
|
|
741
|
+
format: Format;
|
|
742
|
+
}
|
|
743
|
+
/**
|
|
744
|
+
* Parses command-line arguments into an `ArgvInterface` object using `yargs`.
|
|
745
|
+
*
|
|
746
|
+
* This function configures `yargs` to handle various build-related options for a JavaScript and TypeScript toolchain.
|
|
747
|
+
* It returns an object that adheres to the `ArgvInterface` structure based on the parsed arguments.
|
|
748
|
+
*
|
|
749
|
+
* @param argv - An array of command-line arguments (e.g., `process.argv`).
|
|
750
|
+
* @returns An object representing the parsed command-line arguments.
|
|
751
|
+
*
|
|
752
|
+
* @see {@link ArgvInterface} for the structure of the returned object.
|
|
753
|
+
*
|
|
754
|
+
* @example
|
|
755
|
+
* // Example usage:
|
|
756
|
+
* const args = argvParser(process.argv);
|
|
757
|
+
* console.log(args.file); // Output: the file to build
|
|
758
|
+
* console.log(args.dev); // Output: true or false based on the --dev flag
|
|
759
|
+
*/
|
|
760
|
+
export declare function argvParser(argv: Array<string>): Argv<ArgvInterface>;
|
|
761
|
+
/**
|
|
762
|
+
* The `BuildStateInterface` extends the `BuildState` interface to include additional properties related to the build
|
|
763
|
+
* process, specifically for handling `ifdef` conditions and function removals in macros.
|
|
764
|
+
*
|
|
765
|
+
* @interface BuildStateInterface
|
|
766
|
+
*/
|
|
767
|
+
export interface BuildStateInterface extends BuildState {
|
|
768
|
+
ifdef: Array<string>;
|
|
769
|
+
macros: {
|
|
770
|
+
removeFunctions: Set<string>;
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
/**
|
|
774
|
+
* Spawns a new Node.js process to execute the provided JavaScript file, with optional debugging support.
|
|
775
|
+
*
|
|
776
|
+
* This function creates a new Node.js process to run the specified JavaScript file with source map support enabled.
|
|
777
|
+
* It optionally starts the process in debug mode, which allows WebStorm or other debuggers to attach to the process.
|
|
778
|
+
* The output and error streams of the spawned process are captured and logged to the console.
|
|
779
|
+
*
|
|
780
|
+
* @param filePath - The path to the JavaScript file to execute.
|
|
781
|
+
* @param debug - A boolean flag to enable debugging. If `true`, the process will be started with the `--inspect-brk` option,
|
|
782
|
+
* which opens a debugger on `0.0.0.0:9229`, allowing external debuggers to attach.
|
|
783
|
+
*
|
|
784
|
+
* @returns A `ChildProcessWithoutNullStreams` object representing the spawned process.
|
|
785
|
+
* This object allows interaction with the process, including capturing its output and error streams.
|
|
786
|
+
*
|
|
787
|
+
* @remarks
|
|
788
|
+
* - The `--enable-source-maps` flag is used to enable source map support, which allows better debugging by mapping
|
|
789
|
+
* errors and stack traces to the original source code.
|
|
790
|
+
* - If `debug` is `true`, the `--inspect-brk=0.0.0.0:9229` flag is added, starting the process in debug mode and pausing
|
|
791
|
+
* execution until a debugger is attached.
|
|
792
|
+
* - The output (`stdout`) and error (`stderr`) streams of the spawned process are logged to the console.
|
|
793
|
+
* - The function returns a `ChildProcessWithoutNullStreams` object that can be used to interact with the spawned process,
|
|
794
|
+
* such as handling its termination or sending input.
|
|
795
|
+
*
|
|
796
|
+
* @throws Error Throws an error if the Node.js process fails to start or if there are issues with the provided file path.
|
|
797
|
+
*
|
|
798
|
+
* @example
|
|
799
|
+
* ```ts
|
|
800
|
+
* import { spawn } from '@services/process.service';
|
|
801
|
+
*
|
|
802
|
+
* // Run without debugging
|
|
803
|
+
* const process = spawn('./path/to/script.js', false);
|
|
804
|
+
*
|
|
805
|
+
* process.on('close', (code) => {
|
|
806
|
+
* console.log(`Process exited with code ${code}`);
|
|
807
|
+
* });
|
|
808
|
+
*
|
|
809
|
+
* // Run with debugging enabled
|
|
810
|
+
* const debugProcess = spawn('./path/to/script.js', true);
|
|
811
|
+
*
|
|
812
|
+
* debugProcess.on('close', (code) => {
|
|
813
|
+
* console.log(`Debug process exited with code ${code}`);
|
|
814
|
+
* });
|
|
815
|
+
* ```
|
|
816
|
+
*
|
|
817
|
+
* In these examples, the `spawn` function is used to execute a JavaScript file, once in normal mode and once with debugging enabled.
|
|
818
|
+
* The process's exit code is logged when the process completes.
|
|
819
|
+
*
|
|
820
|
+
* @public
|
|
821
|
+
* @category Services
|
|
822
|
+
*/
|
|
823
|
+
export declare function spawn(filePath: string, debug?: boolean): ChildProcessWithoutNullStreams;
|
|
824
|
+
/**
|
|
825
|
+
* Represents the result of transpiling a TypeScript file.
|
|
826
|
+
*
|
|
827
|
+
* This interface defines the structure of the output returned from a TypeScript transpilation process,
|
|
828
|
+
* including the transpiled JavaScript code and the associated source map.
|
|
829
|
+
*
|
|
830
|
+
* @property code - The transpiled JavaScript code generated from the TypeScript file.
|
|
831
|
+
* @property sourceMap - The source map associated with the transpiled JavaScript code.
|
|
832
|
+
*
|
|
833
|
+
* @remarks
|
|
834
|
+
* - The `code` property contains the JavaScript code after TypeScript transpilation.
|
|
835
|
+
* - The `sourceMap` property provides the source map that maps the transpiled JavaScript code back to the original TypeScript source.
|
|
836
|
+
* - The source map is useful for debugging as it allows developers to trace errors in the generated JavaScript back to the original TypeScript code.
|
|
837
|
+
*
|
|
838
|
+
* @example
|
|
839
|
+
* ```typescript
|
|
840
|
+
* import { transpileFileInterface } from './transpileFileInterface';
|
|
841
|
+
*
|
|
842
|
+
* const result: transpileFileInterface = {
|
|
843
|
+
* code: 'console.log("Hello, world!");',
|
|
844
|
+
* sourceMap: 'version: 3\nfile: out.js\nsources: ["file.ts"]\n'
|
|
845
|
+
* };
|
|
846
|
+
*
|
|
847
|
+
* console.log(result.code); // Output: console.log("Hello, world!");
|
|
848
|
+
* console.log(result.sourceMap); // Output: version: 3\nfile: out.js\nsources: ["file.ts"]\n
|
|
849
|
+
* ```
|
|
850
|
+
*
|
|
851
|
+
* In this example, the `transpileFileInterface` is used to represent the result of transpiling a TypeScript file.
|
|
852
|
+
* The `code` contains the JavaScript code, while the `sourceMap` provides the mapping information for debugging purposes.
|
|
853
|
+
*
|
|
854
|
+
* @public
|
|
855
|
+
* @category Interfaces
|
|
856
|
+
*/
|
|
857
|
+
export interface transpileFileInterface {
|
|
858
|
+
code: string;
|
|
859
|
+
sourceMap: string;
|
|
860
|
+
}
|
|
861
|
+
/**
|
|
862
|
+
* Default build options for esbuild bundler in RemoteX framework.
|
|
863
|
+
*
|
|
864
|
+
* These options are used to configure how esbuild processes and bundles the TypeScript
|
|
865
|
+
* files for the RemoteX testing framework.
|
|
866
|
+
*
|
|
867
|
+
* @public
|
|
868
|
+
* @category Configuration
|
|
869
|
+
*/
|
|
870
|
+
export declare const defaultBuildOptions: BuildOptions;
|
|
871
|
+
/**
|
|
872
|
+
* Extracts the source map from the provided data string and returns the modified code and source map separately.
|
|
873
|
+
*
|
|
874
|
+
* This function searches for the inline source map in the data string using a regular expression, removes the
|
|
875
|
+
* source map comment from the data string, and returns an object containing the code without the source map
|
|
876
|
+
* comment and the extracted source map.
|
|
877
|
+
*
|
|
878
|
+
* @param dataString - The string containing the transpiled code with an inline source map.
|
|
879
|
+
* @returns An object containing the modified code without the source map comment and the extracted source map.
|
|
880
|
+
* @throws Error -Throws an error if the source map URL is not found in the data string.
|
|
881
|
+
*
|
|
882
|
+
* @public
|
|
883
|
+
*/
|
|
884
|
+
export declare function extractSourceMap(dataString: string): transpileFileInterface;
|
|
885
|
+
/**
|
|
886
|
+
* Transpiles a TypeScript file and extracts the source map.
|
|
887
|
+
*
|
|
888
|
+
* This function uses esbuild to transpile the specified TypeScript file based on provided build options,
|
|
889
|
+
* and then extracts the source map from the transpiled code.
|
|
890
|
+
*
|
|
891
|
+
* @param filePath - The path to the TypeScript file to be transpiled.
|
|
892
|
+
* @param buildOptions - Optional build options to override the default build options.
|
|
893
|
+
* @returns A promise that resolves to an object containing the transpiled code and the extracted source map.
|
|
894
|
+
* @throws Error - Throws an error if the build process fails or the source map extraction fails.
|
|
895
|
+
*
|
|
896
|
+
* @public
|
|
897
|
+
* @category Services
|
|
898
|
+
*/
|
|
899
|
+
export declare function transpileFile(filePath: string, buildOptions?: BuildOptions): Promise<transpileFileInterface>;
|
|
900
|
+
/**
|
|
901
|
+
* The `analyzeDependencies` function analyzes the dependencies of a given entry point for a specified platform.
|
|
902
|
+
* It performs a bundling operation and generates a metafile that contains detailed information about the
|
|
903
|
+
* dependencies involved in the build process.
|
|
904
|
+
* This is typically used to inspect the external packages and modules
|
|
905
|
+
* that the entry point depends on.
|
|
906
|
+
*
|
|
907
|
+
* - **Input**:
|
|
908
|
+
* - `entryPoint`: A string or array of strings representing the entry points for the build.
|
|
909
|
+
* This defines the starting point(s) for the bundling process.
|
|
910
|
+
* - `platform`: An optional parameter that specifies the platform to target for the build.
|
|
911
|
+
* Default is `'browser'`.
|
|
912
|
+
*
|
|
913
|
+
* - **Output**: A `Promise` that resolves to an object containing:
|
|
914
|
+
* - The `BuildResult` from the bundling process.
|
|
915
|
+
* - A `metafile`, which contains detailed metadata about the build, including the dependencies analyzed.
|
|
916
|
+
*
|
|
917
|
+
* ## Example:
|
|
918
|
+
*
|
|
919
|
+
* ```ts
|
|
920
|
+
* const result = await analyzeDependencies(['src/index.ts']);
|
|
921
|
+
* console.log(result.metafile); // { inputs: { 'src/index.ts': { ... } }, outputs: { ... } }
|
|
922
|
+
*
|
|
923
|
+
* const nodeResult = await analyzeDependencies(['src/server.ts'], 'node');
|
|
924
|
+
* console.log(nodeResult.metafile); // { inputs: { 'src/server.ts': { ... } }, outputs: { ... } }
|
|
925
|
+
* ```
|
|
926
|
+
*
|
|
927
|
+
* @param entryPoint - The entry point(s) to be analyzed.
|
|
928
|
+
* @param platform - The target platform for the build.
|
|
929
|
+
* @returns A `Promise` that resolves to a `BuildResult` object along with a `metafile` containing dependency details.
|
|
930
|
+
* @throws Error If the build process fails for any reason.
|
|
931
|
+
*/
|
|
932
|
+
export declare function analyzeDependencies(entryPoint: EntryPoints, platform?: BuildOptions['platform']): Promise<BuildResult & {
|
|
933
|
+
metafile: Metafile;
|
|
934
|
+
}>;
|
|
935
|
+
/**
|
|
936
|
+
* The `collectFunctionNames` function analyzes the provided TypeScript code and collects the names of functions
|
|
937
|
+
* that should be removed based on specific conditions. The function searches for function declarations and variable
|
|
938
|
+
* declarations where the function name or variable is prefixed with `$$`, and adds these function names to the
|
|
939
|
+
* `removeFunctions` set in the provided `state` object.
|
|
940
|
+
*
|
|
941
|
+
* - **Input**:
|
|
942
|
+
* - `code`: A string containing the TypeScript code to be analyzed.
|
|
943
|
+
* - `state`: An object representing the current build state, specifically the `mocks` state, which includes
|
|
944
|
+
* a `removeFunctions` set that will hold the names of functions that need to be removed.
|
|
945
|
+
*
|
|
946
|
+
* - **Output**: The function does not return any value. Instead, it modifies the `removeFunctions` set inside the
|
|
947
|
+
* `state` object by adding function names that meet the criteria for removal.
|
|
948
|
+
*
|
|
949
|
+
* ## Error Handling:
|
|
950
|
+
* - The function does not explicitly handle errors. If invalid TypeScript code is provided, `ts.createSourceFile`
|
|
951
|
+
* may throw an error, which should be handled by the caller if necessary.
|
|
952
|
+
*
|
|
953
|
+
* @param code - The TypeScript code as a string that will be analyzed.
|
|
954
|
+
* @param state - The build state containing the `removeFunctions` set to store the names of functions to be removed.
|
|
955
|
+
* @returns `void` - The function modifies the `state` directly and does not return a value.
|
|
956
|
+
*/
|
|
957
|
+
export declare function collectFunctionNames(code: string, state: BuildStateInterface['macros']): void;
|
|
958
|
+
/**
|
|
959
|
+
* The `collectDeclaredFunctions` function processes the provided `meta` metafile and reads each file's contents
|
|
960
|
+
* to find function declarations within preprocessor directives. It uses regular expressions to match `// ifdef` and
|
|
961
|
+
* `// endif` blocks in the code and collects the function names from the code inside the `ifdef` block, based on the
|
|
962
|
+
* `define` configuration in the `config` object. If the condition defined in the `ifdef` is not met (i.e., not defined
|
|
963
|
+
* in the `config.define`), the function names found inside the block will be collected and added to the `removeFunctions`
|
|
964
|
+
* set in the `state` object.
|
|
965
|
+
*
|
|
966
|
+
* - **Input**:
|
|
967
|
+
* - `meta`: The `Metafile` object that contains the input files. The keys are file paths, and the values contain
|
|
968
|
+
* metadata about those files.
|
|
969
|
+
* - `config`: The configuration object containing a `define` field, which is an object of conditions that may be used
|
|
970
|
+
* in the `ifdef` blocks. If a condition in an `ifdef` block is not defined in `config.define`, the functions in
|
|
971
|
+
* that block will be collected.
|
|
972
|
+
* - `state`: The build state, specifically the `mocks` state, which includes a `removeFunctions` set that stores
|
|
973
|
+
* function names to be removed.
|
|
974
|
+
*
|
|
975
|
+
* - **Output**: This function does not return a value. It modifies the `removeFunctions` set within the provided `state`
|
|
976
|
+
* object by adding the names of functions found inside unprocessed `ifdef` blocks.
|
|
977
|
+
*
|
|
978
|
+
* ## Error Handling:
|
|
979
|
+
* - If a file cannot be read due to a filesystem error, the function will throw an error.
|
|
980
|
+
* - If the provided `meta` or `config` is malformed, it may result in runtime errors. The caller should ensure valid input.
|
|
981
|
+
*
|
|
982
|
+
* @param meta - The `Metafile` object containing the list of input files and their metadata.
|
|
983
|
+
* @param config - The configuration object that defines conditions used in `ifdef` blocks.
|
|
984
|
+
* @param state - The build state containing the `removeFunctions` set to store function names to be removed.
|
|
985
|
+
* @returns `void` - The function modifies the `state` directly and does not return a value.
|
|
986
|
+
*/
|
|
987
|
+
export declare function collectDeclaredFunctions(meta: Metafile, config: ConfigurationInterface, state: BuildStateInterface['macros']): Promise<void>;
|
|
988
|
+
/**
|
|
989
|
+
* The `parseMacros` function processes TypeScript or JavaScript files to transform macros defined within the content.
|
|
990
|
+
* It ensures that the build state is initialized if necessary, analyzes file dependencies, collects declared functions
|
|
991
|
+
* that are marked for removal, and applies transformations to the source code based on the macros.
|
|
992
|
+
* If the file's extension is not `.ts` or `.js`, the function returns `undefined`. Otherwise, it transforms the code
|
|
993
|
+
* and returns the result in the specified loader format.
|
|
994
|
+
*
|
|
995
|
+
* - **Input**:
|
|
996
|
+
* - `content`: The content of the file as a string or `Uint8Array` to be parsed.
|
|
997
|
+
* - `loader`: A string representing the loader type for transforming the code (e.g., `'ts'`, `'js'`).
|
|
998
|
+
* - `args`: The `OnLoadArgs` object containing metadata for the current loading process, including the file path.
|
|
999
|
+
* - `state`: The build state containing the `mocks` object, which includes a `removeFunctions` set that tracks
|
|
1000
|
+
* functions to be removed.
|
|
1001
|
+
* - `config`: The configuration object that defines how macros should be handled (e.g., conditions for macro processing).
|
|
1002
|
+
*
|
|
1003
|
+
* - **Output**: A `Promise` that resolves to an `OnLoadResult`, `pluginResultType`, or `undefined`. If the file is
|
|
1004
|
+
* of type `.ts` or `.js`, the transformed code is returned in the specified loader format (e.g., `'ts'`). If the file
|
|
1005
|
+
* extension is not recognized, the function returns `undefined`.
|
|
1006
|
+
*
|
|
1007
|
+
* ## Error Handling:
|
|
1008
|
+
* - If the file path does not end with `.ts` or `.js`, the function returns `undefined`.
|
|
1009
|
+
* - If `state.mocks` is not initialized, it will be set up by analyzing the file dependencies and collecting declared functions.
|
|
1010
|
+
* - If any errors occur during the analysis, function collection, or transformation, the function may throw an error.
|
|
1011
|
+
*
|
|
1012
|
+
* @param content - The content of the file as a string or `Uint8Array` to be parsed.
|
|
1013
|
+
* @param loader - The loader type for transforming the code (e.g., `'ts'` or `'js'`).
|
|
1014
|
+
* @param args - The `OnLoadArgs` containing metadata, including the file path.
|
|
1015
|
+
* @param state - The build state that includes `mocks` with the `removeFunctions` set.
|
|
1016
|
+
* @param config - The configuration object defining how macros should be handled.
|
|
1017
|
+
* @returns A `Promise` that resolves to the transformed code (`OnLoadResult` or `pluginResultType`), or `undefined`
|
|
1018
|
+
* if the file is not of type `.ts` or `.js`.
|
|
1019
|
+
*/
|
|
1020
|
+
export declare function parseMacros(content: string | Uint8Array, loader: Loader | undefined, args: OnLoadArgs, state: BuildStateInterface, config: ConfigurationInterface): Promise<OnLoadResult | pluginResultType | undefined>;
|
|
1021
|
+
/**
|
|
1022
|
+
* Represents an error that occurs during the esbuild process.
|
|
1023
|
+
*
|
|
1024
|
+
* This class extends the base error class to provide specific error handling for esbuild-related issues.
|
|
1025
|
+
* It captures the error message and maintains the proper stack trace, allowing for easier debugging
|
|
1026
|
+
* and identification of errors that occur during the build process.
|
|
1027
|
+
*
|
|
1028
|
+
* @class esBuildError
|
|
1029
|
+
* @extends BaseError
|
|
1030
|
+
*/
|
|
1031
|
+
export declare class esBuildError extends BaseError {
|
|
1032
|
+
originalErrorStack?: string;
|
|
1033
|
+
/**
|
|
1034
|
+
* Creates an instance of the EsbuildError class.
|
|
1035
|
+
*
|
|
1036
|
+
* @param message - An object containing the error message. The `text` property is used to initialize
|
|
1037
|
+
* the base error class with a descriptive message about the error encountered during the esbuild process.
|
|
1038
|
+
*/
|
|
1039
|
+
constructor(message: Message);
|
|
1040
|
+
/**
|
|
1041
|
+
* Generates a formatted error message with highlighted code.
|
|
1042
|
+
*
|
|
1043
|
+
* @param message - An esbuild Message object containing error information.
|
|
1044
|
+
* @returns A formatted string of the error message.
|
|
1045
|
+
*/
|
|
1046
|
+
private generateFormattedError;
|
|
1047
|
+
/**
|
|
1048
|
+
* Reads code from a file if it exists.
|
|
1049
|
+
*
|
|
1050
|
+
* @param path - The file path to read from.
|
|
1051
|
+
* @returns Array of lines if file exists, otherwise null.
|
|
1052
|
+
*/
|
|
1053
|
+
private readCode;
|
|
1054
|
+
/**
|
|
1055
|
+
* Formats a code snippet with highlighted errors.
|
|
1056
|
+
*
|
|
1057
|
+
* @param code - Array of code lines.
|
|
1058
|
+
* @param location - The error location within the file.
|
|
1059
|
+
* @returns A formatted and highlighted code snippet string.
|
|
1060
|
+
*/
|
|
1061
|
+
private formatCodeSnippet;
|
|
1062
|
+
/**
|
|
1063
|
+
* Applies color to a given text if colors are enabled.
|
|
1064
|
+
*
|
|
1065
|
+
* @param color - The color code.
|
|
1066
|
+
* @param text - The text to colorize.
|
|
1067
|
+
* @returns The colorized text if colors are active, otherwise plain text.
|
|
1068
|
+
*/
|
|
1069
|
+
private applyColor;
|
|
1070
|
+
}
|
|
1071
|
+
/**
|
|
1072
|
+
* ASCII Logo and Version Information
|
|
1073
|
+
*
|
|
1074
|
+
* @remarks
|
|
1075
|
+
* The `asciiLogo` constant stores an ASCII representation of the project logo
|
|
1076
|
+
* that will be displayed in the banner. This banner is rendered in a formatted
|
|
1077
|
+
* string in the `bannerComponent` function.
|
|
1078
|
+
*
|
|
1079
|
+
* The `cleanScreen` constant contains an ANSI escape code to clear the terminal screen.
|
|
1080
|
+
*/
|
|
1081
|
+
export declare const asciiLogo = "\n ______ _ _ _\n | ___ \\ (_) | | |\n__ _| |_/ /_ _ _| | __| |\n\\ \\/ / ___ \\ | | | | |/ _` |\n > <| |_/ / |_| | | | (_| |\n/_/\\_\\____/ \\__,_|_|_|\\__,_|\n";
|
|
1082
|
+
export declare const cleanScreen = "\u001Bc";
|
|
1083
|
+
/**
|
|
1084
|
+
* Renders the banner with the ASCII logo and version information.
|
|
1085
|
+
*
|
|
1086
|
+
* This function constructs and returns a formatted banner string that includes an ASCII logo and the version number.
|
|
1087
|
+
* The colors used for the ASCII logo and version number can be enabled or disabled based on the `activeColor` parameter.
|
|
1088
|
+
* If color formatting is enabled, the ASCII logo will be rendered in burnt orange, and the version number will be in bright pink.
|
|
1089
|
+
*
|
|
1090
|
+
* @param activeColor - A boolean flag indicating whether ANSI color formatting should be applied. Default is `__ACTIVE_COLOR`.
|
|
1091
|
+
*
|
|
1092
|
+
* @returns A formatted string containing the ASCII logo, version number, and ANSI color codes if `activeColor` is `true`.
|
|
1093
|
+
*
|
|
1094
|
+
* @remarks
|
|
1095
|
+
* The `bannerComponent` function clears the terminal screen, applies color formatting if enabled, and displays
|
|
1096
|
+
* the ASCII logo and version number. The version number is retrieved from the global `__VERSION` variable, and
|
|
1097
|
+
* the colors are reset after the text is rendered.
|
|
1098
|
+
*
|
|
1099
|
+
* @example
|
|
1100
|
+
* ```ts
|
|
1101
|
+
* console.log(bannerComponent());
|
|
1102
|
+
* ```
|
|
1103
|
+
*
|
|
1104
|
+
* This will output the banner to the console with the ASCII logo, version, and colors.
|
|
1105
|
+
*
|
|
1106
|
+
* @example
|
|
1107
|
+
* ```ts
|
|
1108
|
+
* console.log(bannerComponent(false));
|
|
1109
|
+
* ```
|
|
1110
|
+
*
|
|
1111
|
+
* This will output the banner to the console with the ASCII logo and version number without color formatting.
|
|
1112
|
+
*
|
|
1113
|
+
* @public
|
|
1114
|
+
*/
|
|
1115
|
+
export declare function bannerComponent(activeColor?: boolean): string;
|
|
1116
|
+
/**
|
|
1117
|
+
* A formatted string prefix used for logging build-related messages.
|
|
1118
|
+
* // todo optimize this
|
|
1119
|
+
*/
|
|
1120
|
+
export declare function prefix(): string;
|
|
1121
|
+
/**
|
|
1122
|
+
* Manages the HTTP or HTTPS server based on the provided configuration.
|
|
1123
|
+
*
|
|
1124
|
+
* The `ServerProvider` class initializes and starts either an HTTP or HTTPS server based on whether SSL certificates
|
|
1125
|
+
* are provided. It handles incoming requests, serves static files, and lists directory contents with appropriate
|
|
1126
|
+
* icons and colors.
|
|
1127
|
+
*
|
|
1128
|
+
* @class
|
|
1129
|
+
*/
|
|
1130
|
+
export declare class ServerProvider {
|
|
1131
|
+
/**
|
|
1132
|
+
* Root dir to serve
|
|
1133
|
+
*/
|
|
1134
|
+
private readonly rootDir;
|
|
1135
|
+
/**
|
|
1136
|
+
* Indicates whether the server is configured to use HTTPS.
|
|
1137
|
+
*/
|
|
1138
|
+
private readonly isHttps;
|
|
1139
|
+
/**
|
|
1140
|
+
* The server configuration object, including SSL certificate paths and other settings.
|
|
1141
|
+
*/
|
|
1142
|
+
private readonly config;
|
|
1143
|
+
/**
|
|
1144
|
+
* Creates an instance of ServerProvider.
|
|
1145
|
+
*
|
|
1146
|
+
* @param config - The server configuration object, including port number, SSL certificate paths, and an optional request handler.
|
|
1147
|
+
* @param dir - The root directory from which to serve files.
|
|
1148
|
+
*
|
|
1149
|
+
* @example
|
|
1150
|
+
* ```ts
|
|
1151
|
+
* import { ServerProvider } from './server-provider';
|
|
1152
|
+
*
|
|
1153
|
+
* const serverConfig = {
|
|
1154
|
+
* port: 8080,
|
|
1155
|
+
* keyfile: './path/to/keyfile',
|
|
1156
|
+
* certfile: './path/to/certfile',
|
|
1157
|
+
* onRequest: (req, res, next) => { /* custom request handling *\/ }
|
|
1158
|
+
* };
|
|
1159
|
+
* const provider = new ServerProvider(serverConfig, './public');
|
|
1160
|
+
* provider.start();
|
|
1161
|
+
* ```
|
|
1162
|
+
*
|
|
1163
|
+
* This example shows how to create an instance of `ServerProvider` and start the server.
|
|
1164
|
+
*/
|
|
1165
|
+
constructor(config: Serve, dir: string);
|
|
1166
|
+
/**
|
|
1167
|
+
* Starts the server based on the configuration.
|
|
1168
|
+
* If SSL certificates are provided and valid, an HTTPS server is started. Otherwise, an HTTP server is started.
|
|
1169
|
+
*
|
|
1170
|
+
* @example
|
|
1171
|
+
* ```ts
|
|
1172
|
+
* provider.start();
|
|
1173
|
+
* ```
|
|
1174
|
+
*
|
|
1175
|
+
* This example demonstrates how to start the server. It will either start an HTTP or HTTPS server based on the configuration.
|
|
1176
|
+
*/
|
|
1177
|
+
start(): void;
|
|
1178
|
+
/**
|
|
1179
|
+
* Starts an HTTP server.
|
|
1180
|
+
* This method creates an HTTP server that listens on the configured port and handles incoming requests.
|
|
1181
|
+
*
|
|
1182
|
+
* @example
|
|
1183
|
+
* ```ts
|
|
1184
|
+
* provider.startHttpServer();
|
|
1185
|
+
* ```
|
|
1186
|
+
*
|
|
1187
|
+
* This example shows how the `startHttpServer` method is used internally to start an HTTP server.
|
|
1188
|
+
*/
|
|
1189
|
+
private startHttpServer;
|
|
1190
|
+
/**
|
|
1191
|
+
* Starts an HTTPS server.
|
|
1192
|
+
*
|
|
1193
|
+
* This method creates an HTTPS server with SSL/TLS certificates, listens on the configured port, and handles incoming requests.
|
|
1194
|
+
*
|
|
1195
|
+
* @example
|
|
1196
|
+
* ```ts
|
|
1197
|
+
* provider.startHttpsServer();
|
|
1198
|
+
* ```
|
|
1199
|
+
*
|
|
1200
|
+
* This example shows how the `startHttpsServer` method is used internally to start an HTTPS server.
|
|
1201
|
+
*/
|
|
1202
|
+
private startHttpsServer;
|
|
1203
|
+
/**
|
|
1204
|
+
* Handles incoming requests.
|
|
1205
|
+
*
|
|
1206
|
+
* This method checks if a custom request handler is provided in the configuration. If so, it uses the custom handler.
|
|
1207
|
+
* Otherwise, it delegates to the default request handler.
|
|
1208
|
+
*
|
|
1209
|
+
* @param req - The incoming request object.
|
|
1210
|
+
* @param res - The response object.
|
|
1211
|
+
* @param defaultHandler - The default handler functions to be called if no custom handler is provided.
|
|
1212
|
+
*
|
|
1213
|
+
* @example
|
|
1214
|
+
* ```ts
|
|
1215
|
+
* // This method is used internally to handle requests
|
|
1216
|
+
* ```
|
|
1217
|
+
*/
|
|
1218
|
+
private handleRequest;
|
|
1219
|
+
/**
|
|
1220
|
+
* Returns the MIME type for a given file extension.
|
|
1221
|
+
*
|
|
1222
|
+
* This method maps file extensions to their corresponding MIME types.
|
|
1223
|
+
*
|
|
1224
|
+
* @param ext - The file extension.
|
|
1225
|
+
* @returns The MIME type associated with the file extension.
|
|
1226
|
+
*
|
|
1227
|
+
* @example
|
|
1228
|
+
* ```ts
|
|
1229
|
+
* const mimeType = provider.getContentType('html');
|
|
1230
|
+
* console.log(mimeType); // 'text/html'
|
|
1231
|
+
* ```
|
|
1232
|
+
*/
|
|
1233
|
+
private getContentType;
|
|
1234
|
+
/**
|
|
1235
|
+
* Handles the default response for requests, serving files or directories.
|
|
1236
|
+
*
|
|
1237
|
+
* This method serves the content of files or directories. If the request is for a directory, it lists the contents with
|
|
1238
|
+
* appropriate icons and colors.
|
|
1239
|
+
*
|
|
1240
|
+
* @param req - The incoming request object.
|
|
1241
|
+
* @param res - The response object.
|
|
1242
|
+
*
|
|
1243
|
+
* @returns A promise that resolves when the response is sent.
|
|
1244
|
+
*
|
|
1245
|
+
* @throws Throws an error if the file or directory cannot be accessed.
|
|
1246
|
+
*
|
|
1247
|
+
* @example
|
|
1248
|
+
* ```ts
|
|
1249
|
+
* // This method is used internally to handle file and directory responses
|
|
1250
|
+
* ```
|
|
1251
|
+
*/
|
|
1252
|
+
private defaultResponse;
|
|
1253
|
+
/**
|
|
1254
|
+
* promisifyStat the `fs.stat` method.
|
|
1255
|
+
*
|
|
1256
|
+
* Converts the `fs.stat` callback-based method to return a promise.
|
|
1257
|
+
*
|
|
1258
|
+
* @param path - The file or directory path.
|
|
1259
|
+
* @returns A promise that resolves with the file statistics.
|
|
1260
|
+
*
|
|
1261
|
+
* @example
|
|
1262
|
+
* ```ts
|
|
1263
|
+
* const stats = await provider.promisifyStat('./path/to/file');
|
|
1264
|
+
* console.log(stats.isFile()); // true or false
|
|
1265
|
+
* ```
|
|
1266
|
+
*/
|
|
1267
|
+
private promisifyStat;
|
|
1268
|
+
/**
|
|
1269
|
+
* Handles directory listings.
|
|
1270
|
+
*
|
|
1271
|
+
* Reads the contents of a directory and generates an HTML response with file icons and colors.
|
|
1272
|
+
*
|
|
1273
|
+
* @param fullPath - The full path to the directory.
|
|
1274
|
+
* @param requestPath - The request path for generating relative links.
|
|
1275
|
+
* @param res - The response object.
|
|
1276
|
+
*
|
|
1277
|
+
* @example
|
|
1278
|
+
* ```ts
|
|
1279
|
+
* // This method is used internally to handle directory listings
|
|
1280
|
+
* ```
|
|
1281
|
+
*/
|
|
1282
|
+
private handleDirectory;
|
|
1283
|
+
/**
|
|
1284
|
+
* Handles file responses.
|
|
1285
|
+
*
|
|
1286
|
+
* Reads and serves the content of a file.
|
|
1287
|
+
*
|
|
1288
|
+
* @param fullPath - The full path to the file.
|
|
1289
|
+
* @param res - The response object.
|
|
1290
|
+
*
|
|
1291
|
+
* @example
|
|
1292
|
+
* ```ts
|
|
1293
|
+
* // This method is used internally to handle file responses
|
|
1294
|
+
* ```
|
|
1295
|
+
*/
|
|
1296
|
+
private handleFile;
|
|
1297
|
+
/**
|
|
1298
|
+
* Sends a 404 Not Found response.
|
|
1299
|
+
*
|
|
1300
|
+
* @param res - The response object.
|
|
1301
|
+
*
|
|
1302
|
+
* @example
|
|
1303
|
+
* ```ts
|
|
1304
|
+
* provider.sendNotFound(response);
|
|
1305
|
+
* ```
|
|
1306
|
+
*
|
|
1307
|
+
* This example demonstrates how to send a 404 response using the `sendNotFound` method.
|
|
1308
|
+
*/
|
|
1309
|
+
private sendNotFound;
|
|
1310
|
+
/**
|
|
1311
|
+
* Sends an error response.
|
|
1312
|
+
*
|
|
1313
|
+
* @param res - The response object.
|
|
1314
|
+
* @param error - The error object.
|
|
1315
|
+
*
|
|
1316
|
+
* @example
|
|
1317
|
+
* ```ts
|
|
1318
|
+
* provider.sendError(response, new Error('Some error'));
|
|
1319
|
+
* ```
|
|
1320
|
+
*
|
|
1321
|
+
* This example shows how to send an error response using the `sendError` method.
|
|
1322
|
+
*/
|
|
1323
|
+
private sendError;
|
|
1324
|
+
}
|
|
1325
|
+
/**
|
|
1326
|
+
* Plugin provider for esbuild that registers hooks for lifecycle events such as onStart, onEnd, onResolve, and onLoad.
|
|
1327
|
+
* This class allows dynamic behavior by registering multiple hooks for different stages of the build process.
|
|
1328
|
+
*/
|
|
1329
|
+
export declare class PluginsProvider {
|
|
1330
|
+
/**
|
|
1331
|
+
* Holds the build state that hooks can modify.
|
|
1332
|
+
*/
|
|
1333
|
+
private buildState;
|
|
1334
|
+
/**
|
|
1335
|
+
* Holds the registered hooks for the `onEnd` lifecycle event.
|
|
1336
|
+
* This array contains functions that are called after the build process completes.
|
|
1337
|
+
*/
|
|
1338
|
+
private onEndHooks;
|
|
1339
|
+
/**
|
|
1340
|
+
* Holds the registered hooks for the `onSuccess` lifecycle event.
|
|
1341
|
+
* This array contains functions that are called after the build success finish.
|
|
1342
|
+
*/
|
|
1343
|
+
private onSuccess;
|
|
1344
|
+
/**
|
|
1345
|
+
* Holds the registered hooks for the `onLoad` lifecycle event.
|
|
1346
|
+
* This array contains functions that are called when esbuild attempts to load a module.
|
|
1347
|
+
*/
|
|
1348
|
+
private onLoadHooks;
|
|
1349
|
+
/**
|
|
1350
|
+
* Holds the registered hooks for the `onStart` lifecycle event.
|
|
1351
|
+
* This array contains functions that are called before the build process starts.
|
|
1352
|
+
*/
|
|
1353
|
+
private onStartHooks;
|
|
1354
|
+
/**
|
|
1355
|
+
* Holds the registered hooks for the `onResolve` lifecycle event.
|
|
1356
|
+
* This array contains functions that are called when esbuild attempts to resolve a module path.
|
|
1357
|
+
*/
|
|
1358
|
+
private onResolveHooks;
|
|
1359
|
+
/**
|
|
1360
|
+
* Registers a hook function for the `onStart` lifecycle event.
|
|
1361
|
+
* The hook will be called before the build process starts.
|
|
1362
|
+
*
|
|
1363
|
+
* @param fn - A function of type `OnStartType` that will be executed when the build process starts.
|
|
1364
|
+
*
|
|
1365
|
+
* @example
|
|
1366
|
+
* ```ts
|
|
1367
|
+
* pluginProvider.registerOnStart(async (build) => {
|
|
1368
|
+
* console.log('Build started:', build);
|
|
1369
|
+
* });
|
|
1370
|
+
* ```
|
|
1371
|
+
*/
|
|
1372
|
+
registerOnStart(fn: OnStartType | undefined): void;
|
|
1373
|
+
/**
|
|
1374
|
+
* Registers a hook function for the `onEnd` lifecycle event.
|
|
1375
|
+
* The hook will be called after the build process completes.
|
|
1376
|
+
*
|
|
1377
|
+
* @param fn - A function of type `OnEndType` that will be executed after the build completes.
|
|
1378
|
+
*
|
|
1379
|
+
* @example
|
|
1380
|
+
* ```ts
|
|
1381
|
+
* pluginProvider.registerOnEnd(async (result) => {
|
|
1382
|
+
* console.log('Build finished:', result);
|
|
1383
|
+
* });
|
|
1384
|
+
* ```
|
|
1385
|
+
*/
|
|
1386
|
+
registerOnEnd(fn: OnEndType | undefined): void;
|
|
1387
|
+
/**
|
|
1388
|
+
* Registers a hook function for the `onSuccess` lifecycle event.
|
|
1389
|
+
* The hook will be called after the build success completes.
|
|
1390
|
+
*
|
|
1391
|
+
* @param fn - A function of type `OnEndType` that will be executed after the build completes.
|
|
1392
|
+
*
|
|
1393
|
+
* @example
|
|
1394
|
+
* ```ts
|
|
1395
|
+
* pluginProvider.registerOnSuccess(async (result) => {
|
|
1396
|
+
* console.log('Build Success finished:', result);
|
|
1397
|
+
* });
|
|
1398
|
+
* ```
|
|
1399
|
+
*/
|
|
1400
|
+
registerOnSuccess(fn: OnEndType | undefined): void;
|
|
1401
|
+
/**
|
|
1402
|
+
* Registers a hook function for the `onResolve` lifecycle event.
|
|
1403
|
+
* The hook will be called when esbuild attempts to resolve a module path.
|
|
1404
|
+
*
|
|
1405
|
+
* @param fn - A function of type `OnResolveType` that will be executed during module resolution.
|
|
1406
|
+
*
|
|
1407
|
+
* @example
|
|
1408
|
+
* ```ts
|
|
1409
|
+
* pluginProvider.registerOnResolve(async (args) => {
|
|
1410
|
+
* if (args.path === 'my-module') {
|
|
1411
|
+
* return { path: './src/my-module.ts' };
|
|
1412
|
+
* }
|
|
1413
|
+
* return null;
|
|
1414
|
+
* });
|
|
1415
|
+
* ```
|
|
1416
|
+
*/
|
|
1417
|
+
registerOnResolve(fn: OnResolveType | undefined): void;
|
|
1418
|
+
/**
|
|
1419
|
+
* Registers a hook function for the `onLoad` lifecycle event.
|
|
1420
|
+
* The hook will be called when esbuild attempts to load a module.
|
|
1421
|
+
*
|
|
1422
|
+
* @param fn - A function of type `OnLoadType` that will be executed during module loading.
|
|
1423
|
+
*
|
|
1424
|
+
* @example
|
|
1425
|
+
* ```ts
|
|
1426
|
+
* pluginProvider.registerOnLoad(async (contents, loader, args) => {
|
|
1427
|
+
* if (args.path.endsWith('.json')) {
|
|
1428
|
+
* return { contents: JSON.stringify({ key: 'value' }), loader: 'json' };
|
|
1429
|
+
* }
|
|
1430
|
+
* return null;
|
|
1431
|
+
* });
|
|
1432
|
+
* ```
|
|
1433
|
+
*/
|
|
1434
|
+
registerOnLoad(fn: OnLoadType | undefined): void;
|
|
1435
|
+
/**
|
|
1436
|
+
* Registers esbuild plugin hooks and sets up the middleware plugin.
|
|
1437
|
+
*
|
|
1438
|
+
* This function defines the setup for an esbuild plugin, enabling hooks for various lifecycle events:
|
|
1439
|
+
* onStart, onEnd, onResolve, and onLoad. It ensures that hooks registered by the user are called at
|
|
1440
|
+
* the appropriate stages of the build process.
|
|
1441
|
+
*
|
|
1442
|
+
* @returns An object with the plugin configuration that can be passed to esbuild's `plugins` array.
|
|
1443
|
+
* The configuration includes the plugin name and setup function.
|
|
1444
|
+
*
|
|
1445
|
+
* @example
|
|
1446
|
+
* ```ts
|
|
1447
|
+
* // Example usage with esbuild:
|
|
1448
|
+
* const esbuild = require('esbuild');
|
|
1449
|
+
* const pluginProvider = new PluginsProvider();
|
|
1450
|
+
*
|
|
1451
|
+
* esbuild.build({
|
|
1452
|
+
* entryPoints: ['./src/index.ts'],
|
|
1453
|
+
* bundle: true,
|
|
1454
|
+
* plugins: [pluginProvider.setup()],
|
|
1455
|
+
* }).catch(() => process.exit(1));
|
|
1456
|
+
* ```
|
|
1457
|
+
*/
|
|
1458
|
+
setup(): {
|
|
1459
|
+
name: string;
|
|
1460
|
+
setup: (build: PluginBuild) => void;
|
|
1461
|
+
};
|
|
1462
|
+
/**
|
|
1463
|
+
* Executes all registered onStart hooks.
|
|
1464
|
+
*
|
|
1465
|
+
* This function is called when the build process starts and invokes each hook registered via
|
|
1466
|
+
* `registerOnStart`. Hooks can perform actions such as initializing tasks, logging, or setting
|
|
1467
|
+
* up build conditions.
|
|
1468
|
+
*
|
|
1469
|
+
* @param build - The esbuild `PluginBuild` object that represents the current build process.
|
|
1470
|
+
*
|
|
1471
|
+
* @returns A promise that resolves when all hooks have been executed.
|
|
1472
|
+
*
|
|
1473
|
+
* @example
|
|
1474
|
+
* ```ts
|
|
1475
|
+
* // Registering an onStart hook
|
|
1476
|
+
* pluginProvider.registerOnStart(async (build) => {
|
|
1477
|
+
* console.log('Build started:', build);
|
|
1478
|
+
* });
|
|
1479
|
+
* ```
|
|
1480
|
+
*/
|
|
1481
|
+
private handleOnStart;
|
|
1482
|
+
/**
|
|
1483
|
+
* Executes all registered onEnd hooks after the build finishes.
|
|
1484
|
+
*
|
|
1485
|
+
* This function is called after the build process completes and invokes each hook registered via
|
|
1486
|
+
* `registerOnEnd`. Hooks can be used to process the build results, such as performing analysis or cleanup.
|
|
1487
|
+
*
|
|
1488
|
+
* @param buildResult - The build buildResult object provided by esbuild, containing details about the build process.
|
|
1489
|
+
*
|
|
1490
|
+
* @returns A promise that resolves when all hooks have been executed.
|
|
1491
|
+
*
|
|
1492
|
+
* @example
|
|
1493
|
+
* ```ts
|
|
1494
|
+
* // Registering an onEnd hook
|
|
1495
|
+
* pluginProvider.registerOnEnd(async (buildResult) => {
|
|
1496
|
+
* console.log('Build completed:', buildResult);
|
|
1497
|
+
* });
|
|
1498
|
+
* ```
|
|
1499
|
+
*/
|
|
1500
|
+
private handleOnEnd;
|
|
1501
|
+
/**
|
|
1502
|
+
* Resolves module imports using registered onResolve hooks.
|
|
1503
|
+
*
|
|
1504
|
+
* This function is called whenever esbuild attempts to resolve a module path. It iterates over all registered
|
|
1505
|
+
* onResolve hooks and merges their results. If no hook resolves a path, `null` is returned.
|
|
1506
|
+
*
|
|
1507
|
+
* @param args - The esbuild `OnResolveArgs` object containing information about the module being resolved.
|
|
1508
|
+
*
|
|
1509
|
+
* @returns A promise that resolves to an `OnResolveResult` containing the resolved path, or `null` if no path is found.
|
|
1510
|
+
*
|
|
1511
|
+
* @example
|
|
1512
|
+
* ```ts
|
|
1513
|
+
* // Registering an onResolve hook
|
|
1514
|
+
* pluginProvider.registerOnResolve(async (args) => {
|
|
1515
|
+
* if (args.path === 'my-module') {
|
|
1516
|
+
* return { path: './src/my-module.ts' };
|
|
1517
|
+
* }
|
|
1518
|
+
* return null;
|
|
1519
|
+
* });
|
|
1520
|
+
* ```
|
|
1521
|
+
*/
|
|
1522
|
+
private handleOnResolve;
|
|
1523
|
+
/**
|
|
1524
|
+
* Loads module contents using registered onLoad hooks.
|
|
1525
|
+
*
|
|
1526
|
+
* This function is called when esbuild attempts to load a module. It reads the module contents and then
|
|
1527
|
+
* processes it through all registered onLoad hooks. The hooks can modify the contents and loader type.
|
|
1528
|
+
*
|
|
1529
|
+
* @param args - The esbuild `OnLoadArgs` object containing information about the module being loaded.
|
|
1530
|
+
*
|
|
1531
|
+
* @returns A promise that resolves to an `OnLoadResult` containing the module contents and loader, or `null` if no contents are loaded.
|
|
1532
|
+
*
|
|
1533
|
+
* @example
|
|
1534
|
+
* ```ts
|
|
1535
|
+
* // Registering an onLoad hook
|
|
1536
|
+
* pluginProvider.registerOnLoad(async (contents, loader, args) => {
|
|
1537
|
+
* if (args.path.endsWith('.json')) {
|
|
1538
|
+
* return { contents: JSON.stringify({ key: 'value' }), loader: 'json' };
|
|
1539
|
+
* }
|
|
1540
|
+
* return null;
|
|
1541
|
+
* });
|
|
1542
|
+
* ```
|
|
1543
|
+
*/
|
|
1544
|
+
private handleOnLoad;
|
|
1545
|
+
}
|
|
1546
|
+
/**
|
|
1547
|
+
* Parses and filters content based on conditional directives.
|
|
1548
|
+
*
|
|
1549
|
+
* This function processes the given code contents and removes sections that
|
|
1550
|
+
* are conditionally compiled based on the provided `defines` object.
|
|
1551
|
+
*
|
|
1552
|
+
* @param contents - The code contents to be processed.
|
|
1553
|
+
* @param defines - An object containing conditional
|
|
1554
|
+
* definitions. Keys are condition names, and values are their definitions.
|
|
1555
|
+
* @returns The processed code contents with conditional blocks removed
|
|
1556
|
+
* according to the `defines` object.
|
|
1557
|
+
*/
|
|
1558
|
+
export declare function parseIfDefConditionals(contents: string, defines: Record<string, unknown>): string;
|
|
1559
|
+
/**
|
|
1560
|
+
* Resolves path aliases in the provided content based on the specified paths and root directory.
|
|
1561
|
+
*
|
|
1562
|
+
* This function takes a string of content and replaces occurrences of defined path alias keys
|
|
1563
|
+
* with their corresponding relative paths derived from the specified source file and root directory.
|
|
1564
|
+
* It ensures that the resulting paths are relative to the directory of the source file and formatted
|
|
1565
|
+
* correctly for use in a JavaScript/TypeScript environment.
|
|
1566
|
+
*
|
|
1567
|
+
* Example:
|
|
1568
|
+
* Given the following inputs:
|
|
1569
|
+
* ```ts
|
|
1570
|
+
* const content = "import { foo } from '@core/foo';";
|
|
1571
|
+
* const sourceFile = "/project/src/index.ts";
|
|
1572
|
+
* const paths = {
|
|
1573
|
+
* '@core/': 'src/core',
|
|
1574
|
+
* '@utils/': 'src/utils'
|
|
1575
|
+
* };
|
|
1576
|
+
* const rootDir = "/project";
|
|
1577
|
+
* ```
|
|
1578
|
+
* The function will replace `@core/foo` with a relative path based on the source file's location,
|
|
1579
|
+
* potentially resulting in:
|
|
1580
|
+
* ```ts
|
|
1581
|
+
* const content = "import { foo } from './core/foo';";
|
|
1582
|
+
* ```
|
|
1583
|
+
*
|
|
1584
|
+
* @param content - The content in which path aliases need to be resolved.
|
|
1585
|
+
* @param sourceFile - The path of the source file from which relative paths will be calculated.
|
|
1586
|
+
* @param paths - An object mapping path alias keys to their corresponding paths.
|
|
1587
|
+
* @param esm - A flag indicating whether ESM is enabled.
|
|
1588
|
+
* @returns The updated content with resolved path aliases.
|
|
1589
|
+
*/
|
|
1590
|
+
export declare function resolveAliasPlugin(content: string, sourceFile: string, paths: Record<string, string>, esm: boolean): string;
|
|
1591
|
+
/**
|
|
1592
|
+
* Represents TypeScript node types that can have modifiers applied to them.
|
|
1593
|
+
* This union type includes all declaration types that support TypeScript modifiers
|
|
1594
|
+
* like export, default, abstract, public, private, etc.
|
|
1595
|
+
*
|
|
1596
|
+
* @see ts.ClassDeclaration
|
|
1597
|
+
* @see ts.InterfaceDeclaration
|
|
1598
|
+
* @see ts.EnumDeclaration
|
|
1599
|
+
* @see ts.FunctionDeclaration
|
|
1600
|
+
* @see ts.TypeAliasDeclaration
|
|
1601
|
+
* @see ts.VariableStatement
|
|
1602
|
+
* @see ts.ModuleDeclaration
|
|
1603
|
+
*
|
|
1604
|
+
* @since 1.5.5
|
|
1605
|
+
*/
|
|
1606
|
+
export type NodeWithModifiersType = ts.ClassDeclaration | ts.InterfaceDeclaration | ts.EnumDeclaration | ts.FunctionDeclaration | ts.TypeAliasDeclaration | ts.VariableStatement | ts.ModuleDeclaration;
|
|
1607
|
+
/**
|
|
1608
|
+
* Custom error class to represent type-related errors.
|
|
1609
|
+
*
|
|
1610
|
+
* This class extends the built-in `Error` class to provide more specific
|
|
1611
|
+
* error handling for issues related to types. It can be used to distinguish
|
|
1612
|
+
* errors that occur due to type mismatches or other type-related problems
|
|
1613
|
+
* in your application.
|
|
1614
|
+
*
|
|
1615
|
+
* @example
|
|
1616
|
+
* ```ts
|
|
1617
|
+
* throw new TypesError('Invalid type encountered.');
|
|
1618
|
+
* ```
|
|
1619
|
+
*
|
|
1620
|
+
* @augments Error
|
|
1621
|
+
*/
|
|
1622
|
+
export declare class TypesError extends Error {
|
|
1623
|
+
/**
|
|
1624
|
+
* Creates an instance of `TypesError`.
|
|
1625
|
+
*
|
|
1626
|
+
* @param message - A human-readable message providing details about the error.
|
|
1627
|
+
* @param options - Optional configuration for the error, such as a `cause` (ECMAScript 2022+).
|
|
1628
|
+
*/
|
|
1629
|
+
constructor(message?: string, options?: {
|
|
1630
|
+
cause?: Error;
|
|
1631
|
+
});
|
|
1632
|
+
}
|
|
1633
|
+
/**
|
|
1634
|
+
* Provides TypeScript-related utilities such as type-checking, generating declaration files,
|
|
1635
|
+
* and transforming module specifiers into relative paths based on the output directory.
|
|
1636
|
+
*
|
|
1637
|
+
* The `TypescriptProvider` class enables users to type-check TypeScript projects, generate
|
|
1638
|
+
* declaration files, and transform import/export paths into relative paths that match the output directory.
|
|
1639
|
+
* It uses the TypeScript Compiler API to achieve these transformations.
|
|
1640
|
+
*
|
|
1641
|
+
* @class
|
|
1642
|
+
* @example
|
|
1643
|
+
* ```ts
|
|
1644
|
+
* const tsProvider = new TypescriptProvider(parsedConfig, './dist');
|
|
1645
|
+
* tsProvider.typeCheck(entryPoints);
|
|
1646
|
+
* tsProvider.generateDeclarations(entryPoints);
|
|
1647
|
+
* ```
|
|
1648
|
+
*/
|
|
1649
|
+
export declare class TypeScriptProvider {
|
|
1650
|
+
tsConfig: ts.ParsedCommandLine;
|
|
1651
|
+
private outDir;
|
|
1652
|
+
private activeColor;
|
|
1653
|
+
/**
|
|
1654
|
+
* Compiler options for configuring TypeScript compilation.
|
|
1655
|
+
*/
|
|
1656
|
+
readonly options: ts.CompilerOptions;
|
|
1657
|
+
/**
|
|
1658
|
+
* Creates an instance of `TypescriptProvider` with the given TypeScript configuration and output directory.
|
|
1659
|
+
*
|
|
1660
|
+
* This constructor initializes a `TypescriptProvider` with the provided TypeScript configuration
|
|
1661
|
+
* and the directory where output files (such as declaration files) will be stored.
|
|
1662
|
+
* Additionally, it accepts a flag to control whether ANSI color formatting should be applied in output messages.
|
|
1663
|
+
*
|
|
1664
|
+
* @param tsConfig - The parsed TypeScript configuration object.
|
|
1665
|
+
* @param outDir - The directory where output files (such as declaration files) will be stored.
|
|
1666
|
+
* @param activeColor - A boolean flag indicating whether ANSI color formatting should be applied in output messages.
|
|
1667
|
+
* Default is `true`.
|
|
1668
|
+
*
|
|
1669
|
+
* @example
|
|
1670
|
+
* ```ts
|
|
1671
|
+
* const tsProvider = new TypescriptProvider(parsedConfig, './dist');
|
|
1672
|
+
* ```
|
|
1673
|
+
*
|
|
1674
|
+
* @example
|
|
1675
|
+
* ```ts
|
|
1676
|
+
* const tsProvider = new TypescriptProvider(parsedConfig, './dist', false);
|
|
1677
|
+
* // This instance will not apply ANSI color formatting in output messages
|
|
1678
|
+
* ```
|
|
1679
|
+
*/
|
|
1680
|
+
constructor(tsConfig: ts.ParsedCommandLine, outDir: string, activeColor?: boolean);
|
|
1681
|
+
/**
|
|
1682
|
+
* Performs type-checking on the specified entry points without emitting any files.
|
|
1683
|
+
*
|
|
1684
|
+
* This method compiles the provided TypeScript files to check for type errors and other diagnostics without
|
|
1685
|
+
* generating any output files. It ensures that the code adheres to TypeScript's type constraints and reports
|
|
1686
|
+
* any issues found during the type-checking process.
|
|
1687
|
+
*
|
|
1688
|
+
* @param allowError - A boolean flag indicating whether to throw an error if diagnostics are present. If set to
|
|
1689
|
+
* `true`, errors are logged but not thrown, allowing the process to continue. Default to `false`, which throws
|
|
1690
|
+
* an error if diagnostics are encountered.
|
|
1691
|
+
*
|
|
1692
|
+
* @returns void
|
|
1693
|
+
*
|
|
1694
|
+
* @throws Throws an error if type-checking fails due to TypeScript diagnostics and `allowError` is `false`.
|
|
1695
|
+
* The error indicates that the type-checking process has encountered issues.
|
|
1696
|
+
*
|
|
1697
|
+
* @example
|
|
1698
|
+
* ```ts
|
|
1699
|
+
* // Type-check files and handle any diagnostics
|
|
1700
|
+
* tsProvider.typeCheck(['src/index.ts', 'src/app.ts']);
|
|
1701
|
+
* ```
|
|
1702
|
+
*/
|
|
1703
|
+
typeCheck(allowError?: boolean): void;
|
|
1704
|
+
/**
|
|
1705
|
+
* Generates declaration files (.d.ts) for bundle entry points.
|
|
1706
|
+
*
|
|
1707
|
+
* @param entryPoints - Map of output declaration file paths to their corresponding input source files
|
|
1708
|
+
* @param noTypeChecker - When true, skips type checking during generation (default: false)
|
|
1709
|
+
* @param allowError - When true, continues generation even if type errors are found (default: false)
|
|
1710
|
+
*
|
|
1711
|
+
* @throws Error - If type errors are found and allowError is false
|
|
1712
|
+
*
|
|
1713
|
+
* @remarks
|
|
1714
|
+
* This method creates TypeScript declaration files for each entry point specified in the record.
|
|
1715
|
+
* For each entry point:
|
|
1716
|
+
*
|
|
1717
|
+
* 1. Create a TypeScript program with input file
|
|
1718
|
+
* 2. Configure the program to emit declaration files only
|
|
1719
|
+
* 3. Applies custom transformers to clean up declarations
|
|
1720
|
+
* 4. Performs type checking (unless disabled)
|
|
1721
|
+
* 5. Emits the declaration file to the specified output path
|
|
1722
|
+
*
|
|
1723
|
+
* The generated declarations are processed by the cleanupDeclarations transformer,
|
|
1724
|
+
* which removes export modifiers and performs another necessary cleanup.
|
|
1725
|
+
*
|
|
1726
|
+
* Type errors will normally cause the process to fail unless allowError is set to true.
|
|
1727
|
+
*
|
|
1728
|
+
* @example
|
|
1729
|
+
* ```ts
|
|
1730
|
+
* // Generate declarations for multiple entry points
|
|
1731
|
+
* generator.generateBundleDeclarations({
|
|
1732
|
+
* './dist/index.d.ts': './src/index.ts',
|
|
1733
|
+
* './dist/core.d.ts': './src/core.ts'
|
|
1734
|
+
* });
|
|
1735
|
+
* ```
|
|
1736
|
+
*
|
|
1737
|
+
* @since 1.5.5
|
|
1738
|
+
*/
|
|
1739
|
+
generateBundleDeclarations(entryPoints: Record<string, string>, noTypeChecker?: boolean, allowError?: boolean): void;
|
|
1740
|
+
/**
|
|
1741
|
+
* Generates TypeScript declaration files (`.d.ts`) for the specified entry points.
|
|
1742
|
+
*
|
|
1743
|
+
* This method compiles the provided TypeScript files and emits only the declaration files, without generating
|
|
1744
|
+
* JavaScript code. It applies a custom transformer to modify module specifiers to relative paths based on the
|
|
1745
|
+
* output directory. This ensures that the generated declaration files are accurate and the module paths are
|
|
1746
|
+
* aligned with the project's build structure.
|
|
1747
|
+
*
|
|
1748
|
+
* @param entryPoints - Map of output declaration file paths to their corresponding input source files
|
|
1749
|
+
* @param noTypeChecker - Skips TypeScript type checking.
|
|
1750
|
+
* @param allowError - A boolean flag indicating whether to throw an error if diagnostics are present. If set to
|
|
1751
|
+
* `true`, errors are logged but not thrown, allowing the process to continue. Default to `false`, which throws
|
|
1752
|
+
* an error if diagnostics are encountered.
|
|
1753
|
+
*
|
|
1754
|
+
* @returns void
|
|
1755
|
+
*
|
|
1756
|
+
* @example
|
|
1757
|
+
* ```ts
|
|
1758
|
+
* // Generate declaration files for specific entry points
|
|
1759
|
+
* tsProvider.generateDeclarations(['src/index.ts', 'src/app.ts']);
|
|
1760
|
+
* ```
|
|
1761
|
+
*/
|
|
1762
|
+
generateDeclarations(entryPoints: Record<string, string>, noTypeChecker?: boolean, allowError?: boolean): void;
|
|
1763
|
+
/**
|
|
1764
|
+
* Checks if the provided node is an import or export declaration.
|
|
1765
|
+
*
|
|
1766
|
+
* @param node - A TypeScript AST node to check.
|
|
1767
|
+
*
|
|
1768
|
+
* @returns `true` if the node is either an `ImportDeclaration` or `ExportDeclaration`; otherwise, `false`.
|
|
1769
|
+
*
|
|
1770
|
+
* @example
|
|
1771
|
+
* ```ts
|
|
1772
|
+
* const isDeclaration = tsProvider.isImportOrExportDeclaration(node);
|
|
1773
|
+
* console.log(isDeclaration); // true or false
|
|
1774
|
+
* ```
|
|
1775
|
+
*/
|
|
1776
|
+
private isImportOrExportDeclaration;
|
|
1777
|
+
/**
|
|
1778
|
+
* Checks if the provided node has a string literal as its module specifier.
|
|
1779
|
+
*
|
|
1780
|
+
* @param node - A TypeScript AST node to check.
|
|
1781
|
+
*
|
|
1782
|
+
* @returns `true` if the node has a string literal module specifier; otherwise, `undefined`.
|
|
1783
|
+
*
|
|
1784
|
+
* @example
|
|
1785
|
+
* ```ts
|
|
1786
|
+
* const hasModuleSpecifier = tsProvider.hasStringLiteralModuleSpecifier(importNode);
|
|
1787
|
+
* console.log(hasModuleSpecifier); // true or undefined
|
|
1788
|
+
* ```
|
|
1789
|
+
*/
|
|
1790
|
+
private hasStringLiteralModuleSpecifier;
|
|
1791
|
+
/**
|
|
1792
|
+
* Resolves the module file name based on the module specifier and TypeScript compiler options.
|
|
1793
|
+
*
|
|
1794
|
+
* @param specifierText - The module specifier text (e.g., `'./module'`).
|
|
1795
|
+
* @param options - The TypeScript compiler options.
|
|
1796
|
+
*
|
|
1797
|
+
* @returns The resolved file path of the module or `undefined` if the module cannot be resolved.
|
|
1798
|
+
*
|
|
1799
|
+
* @example
|
|
1800
|
+
* ```ts
|
|
1801
|
+
* const resolvedPath = tsProvider.resolveModuleFileName('./module', compilerOptions);
|
|
1802
|
+
* console.log(resolvedPath); // './dist/module.js'
|
|
1803
|
+
* ```
|
|
1804
|
+
*/
|
|
1805
|
+
private resolveModuleFileName;
|
|
1806
|
+
/**
|
|
1807
|
+
* Computes the relative path from the source file to the resolved target file.
|
|
1808
|
+
*
|
|
1809
|
+
* @param sourceFile - The absolute path of the source file.
|
|
1810
|
+
* @param resolvedTargetFile - The absolute path of the resolved target file.
|
|
1811
|
+
*
|
|
1812
|
+
* @returns A relative path from the source file to the target file.
|
|
1813
|
+
*
|
|
1814
|
+
* @example
|
|
1815
|
+
* ```ts
|
|
1816
|
+
* const relativePath = tsProvider.getRelativePathToOutDir('./src/index.ts', './dist/module.js');
|
|
1817
|
+
* console.log(relativePath); // './module.js'
|
|
1818
|
+
* ```
|
|
1819
|
+
*/
|
|
1820
|
+
private getRelativePathToOutDir;
|
|
1821
|
+
/**
|
|
1822
|
+
* Updates the module specifier of an import or export declaration to a relative path based on the output directory.
|
|
1823
|
+
*
|
|
1824
|
+
* This method takes a TypeScript `ImportDeclaration` or `ExportDeclaration` node and updates its module
|
|
1825
|
+
* specifier to a relative path that matches the output directory. It uses the TypeScript compiler options
|
|
1826
|
+
* to resolve the full path of the module and converts it into a relative path from the source file.
|
|
1827
|
+
*
|
|
1828
|
+
* This is useful when transforming module specifiers to ensure the generated declaration files (`.d.ts`)
|
|
1829
|
+
* have correct paths when files are moved to the output directory.
|
|
1830
|
+
*
|
|
1831
|
+
* @param node - The TypeScript `ImportDeclaration` or `ExportDeclaration` node whose module specifier needs to be updated.
|
|
1832
|
+
* @param sourceFile - The absolute path of the source file containing the node.
|
|
1833
|
+
*
|
|
1834
|
+
* @returns The updated `ImportDeclaration` or `ExportDeclaration` node with the new relative module specifier.
|
|
1835
|
+
*
|
|
1836
|
+
* @example
|
|
1837
|
+
* ```ts
|
|
1838
|
+
* const updatedNode = tsProvider.updateModuleSpecifier(importNode, './src/index.ts');
|
|
1839
|
+
* console.log(updatedNode.moduleSpecifier.text); // './module.js'
|
|
1840
|
+
* ```
|
|
1841
|
+
*/
|
|
1842
|
+
private updateModuleSpecifier;
|
|
1843
|
+
/**
|
|
1844
|
+
* Creates a visitor function that transforms `ImportDeclaration` and `ExportDeclaration` nodes by updating
|
|
1845
|
+
* their module specifiers to relative paths based on the output directory.
|
|
1846
|
+
*
|
|
1847
|
+
* This method returns a visitor function, which is used to traverse and transform the nodes in a TypeScript
|
|
1848
|
+
* `SourceFile`. The visitor identifies `ImportDeclaration` and `ExportDeclaration` nodes with module specifiers
|
|
1849
|
+
* that are string literals. For these nodes, the module specifiers are resolved to their corresponding file paths
|
|
1850
|
+
* and updated to relative paths that align with the output directory.
|
|
1851
|
+
*
|
|
1852
|
+
* The visitor is designed to recursively visit all nodes in the `SourceFile`, transforming only the relevant
|
|
1853
|
+
* import and export declarations while leaving other nodes unchanged.
|
|
1854
|
+
*
|
|
1855
|
+
* @param sourceFile - The TypeScript `SourceFile` that will be traversed by the visitor.
|
|
1856
|
+
* @param context - The transformation context provided by the TypeScript compiler, used for visiting nodes.
|
|
1857
|
+
*
|
|
1858
|
+
* @returns A visitor function that processes the nodes in the source file, updating module specifiers as needed.
|
|
1859
|
+
*
|
|
1860
|
+
* @example
|
|
1861
|
+
* ```ts
|
|
1862
|
+
* const visitor = tsProvider.createVisitor(sourceFile, context);
|
|
1863
|
+
* const transformedNode = visitor(importNode);
|
|
1864
|
+
* console.log(transformedNode); // ImportDeclaration with updated module specifier
|
|
1865
|
+
* ```
|
|
1866
|
+
*/
|
|
1867
|
+
private createVisitor;
|
|
1868
|
+
/**
|
|
1869
|
+
* Creates a custom transformer factory for TypeScript that processes `SourceFile` nodes.
|
|
1870
|
+
*
|
|
1871
|
+
* This method returns a custom transformer factory function which generates a transformer that can be used
|
|
1872
|
+
* during the TypeScript compilation process. The transformer specifically processes `SourceFile` nodes to
|
|
1873
|
+
* apply custom transformations and does not alter `Bundle` nodes.
|
|
1874
|
+
*
|
|
1875
|
+
* The factory function generates a transformer that performs the following:
|
|
1876
|
+
* - **Transforming `SourceFile` nodes**: Uses the `createVisitor` method to visit and transform all nodes in
|
|
1877
|
+
* a `SourceFile`. This allows custom modifications or analysis of TypeScript source files.
|
|
1878
|
+
* - **No transformation for `Bundle` nodes**: The `transformBundle` method returns the bundle unchanged, as
|
|
1879
|
+
* no specific transformation is needed for bundles in this implementation.
|
|
1880
|
+
*
|
|
1881
|
+
* This transformer factory is used primarily for customizing TypeScript file transformations, such as updating
|
|
1882
|
+
* module specifiers or other source-level adjustments during the compilation process.
|
|
1883
|
+
*
|
|
1884
|
+
* @returns A custom transformer factory function that produces a `CustomTransformer` object. This object
|
|
1885
|
+
* implements the `transformSourceFile` and `transformBundle` methods.
|
|
1886
|
+
*
|
|
1887
|
+
* @example
|
|
1888
|
+
* ```ts
|
|
1889
|
+
* const transformerFactory = tsProvider.createTransformerFactory();
|
|
1890
|
+
* const emitResult = program.emit(undefined, undefined, undefined, true, {
|
|
1891
|
+
* after: [transformerFactory]
|
|
1892
|
+
* });
|
|
1893
|
+
* ```
|
|
1894
|
+
*/
|
|
1895
|
+
private createTransformerFactory;
|
|
1896
|
+
/**
|
|
1897
|
+
* Handles and logs TypeScript diagnostics, providing detailed error messages with file and position information.
|
|
1898
|
+
*
|
|
1899
|
+
* This method processes an array of TypeScript diagnostics, printing formatted error messages to the console.
|
|
1900
|
+
* If a diagnostic is associated with a specific file and position, the error message includes the filename,
|
|
1901
|
+
* line, and character information. Colors are applied to highlight different parts of the message, such as
|
|
1902
|
+
* file paths, positions, error messages, and error codes.
|
|
1903
|
+
*
|
|
1904
|
+
* If the `allowError` flag is `false`, the method throws an error when diagnostics are present, indicating
|
|
1905
|
+
* that type checking has failed. If `allowError` is `true`, errors are logged but not thrown, allowing the process
|
|
1906
|
+
* to continue despite the diagnostics.
|
|
1907
|
+
*
|
|
1908
|
+
* @param diagnostics - An array of readonly `Diagnostic` objects returned by the TypeScript compiler.
|
|
1909
|
+
* These diagnostics contain information about errors or warnings that occurred during compilation.
|
|
1910
|
+
*
|
|
1911
|
+
* @param allowError - A boolean flag that determines whether the method should throw an error when diagnostics
|
|
1912
|
+
* are encountered. If set to `true`, the method logs diagnostics but does not throw an error. Defaults to `false`.
|
|
1913
|
+
*
|
|
1914
|
+
* @throws Will throw an error if diagnostics are present and `allowError` is set to `false`. The error
|
|
1915
|
+
* indicates that type checking has failed.
|
|
1916
|
+
*
|
|
1917
|
+
* @example
|
|
1918
|
+
* ```ts
|
|
1919
|
+
* const diagnostics = program.getSemanticDiagnostics();
|
|
1920
|
+
* handleDiagnostics(diagnostics, false); // Throws an error if any diagnostics exist.
|
|
1921
|
+
* ```
|
|
1922
|
+
*/
|
|
1923
|
+
private handleDiagnostics;
|
|
1924
|
+
/**
|
|
1925
|
+
* Type guard to check if a node is one that can have modifiers.
|
|
1926
|
+
*
|
|
1927
|
+
* @param node - The TypeScript node to check
|
|
1928
|
+
* @returns True if the node is a type that can have modifiers, false otherwise
|
|
1929
|
+
*
|
|
1930
|
+
* @remarks
|
|
1931
|
+
* This type guard function identifies node types that can potentially have
|
|
1932
|
+
* modifiers like 'export', 'default', etc. It's used in the AST transformation
|
|
1933
|
+
* process to identify nodes where export modifiers need to be removed.
|
|
1934
|
+
*
|
|
1935
|
+
* The supported node types include:
|
|
1936
|
+
* - Class declarations
|
|
1937
|
+
* - Interface declarations
|
|
1938
|
+
* - Enum declarations
|
|
1939
|
+
* - Function declarations
|
|
1940
|
+
* - Type alias declarations
|
|
1941
|
+
* - Variable statements
|
|
1942
|
+
* - Module declarations
|
|
1943
|
+
*
|
|
1944
|
+
* @see NodeWithModifiersType
|
|
1945
|
+
* @see ts.Node
|
|
1946
|
+
*
|
|
1947
|
+
* @since 1.5.5
|
|
1948
|
+
*/
|
|
1949
|
+
private isNodeWithModifiers;
|
|
1950
|
+
/**
|
|
1951
|
+
* Filters out export and default modifiers from an array of TypeScript modifiers.
|
|
1952
|
+
*
|
|
1953
|
+
* @param modifiers - Optional array of TypeScript modifiers to process
|
|
1954
|
+
* @returns A new array without export/default modifiers, or undefined if no modifiers remain
|
|
1955
|
+
*
|
|
1956
|
+
* @remarks
|
|
1957
|
+
* This helper method is used when transforming nodes to remove export-related functionality.
|
|
1958
|
+
* It returns undefined instead of an empty array when no modifiers remain to ensure
|
|
1959
|
+
* proper TypeScript AST structure.
|
|
1960
|
+
*
|
|
1961
|
+
* @example
|
|
1962
|
+
* ```ts
|
|
1963
|
+
* // Input: [export, const]
|
|
1964
|
+
* // Output: [const]
|
|
1965
|
+
*
|
|
1966
|
+
* // Input: [export]
|
|
1967
|
+
* // Output: undefined
|
|
1968
|
+
* ```
|
|
1969
|
+
*
|
|
1970
|
+
* @see ts.Modifier
|
|
1971
|
+
* @see ts.SyntaxKind.ExportKeyword
|
|
1972
|
+
* @see ts.SyntaxKind.DefaultKeyword
|
|
1973
|
+
*
|
|
1974
|
+
* @since 1.5.5
|
|
1975
|
+
*/
|
|
1976
|
+
private removeExportModifiers;
|
|
1977
|
+
/**
|
|
1978
|
+
* Updates a node by removing its export modifiers.
|
|
1979
|
+
*
|
|
1980
|
+
* @param node - A TypeScript node that can have modifiers
|
|
1981
|
+
* @returns A new node with export and default modifiers removed
|
|
1982
|
+
*
|
|
1983
|
+
* @remarks
|
|
1984
|
+
* This method dynamically determines the node type and applies the appropriate
|
|
1985
|
+
* update function from the nodeUpdaters record.
|
|
1986
|
+
*
|
|
1987
|
+
* It works by:
|
|
1988
|
+
* 1. First, removing export modifiers using removeExportModifiers method
|
|
1989
|
+
* 2. Identifying the node type by matching it against TypeScript's type guards
|
|
1990
|
+
* 3. Applying the corresponding updater function from nodeUpdaters
|
|
1991
|
+
* 4. Falling back to returning the original node if no updater is found
|
|
1992
|
+
*
|
|
1993
|
+
* The dynamic approach avoids repetitive code for each node type while still
|
|
1994
|
+
* maintaining type safety through TypeScript's built-in type guards.
|
|
1995
|
+
*
|
|
1996
|
+
* @see nodeUpdaters
|
|
1997
|
+
* @see removeExportModifiers
|
|
1998
|
+
* @see NodeWithModifiersType
|
|
1999
|
+
*
|
|
2000
|
+
* @since 1.5.5
|
|
2001
|
+
*/
|
|
2002
|
+
private updateNodeWithoutExports;
|
|
2003
|
+
/**
|
|
2004
|
+
* Recursively visits and transforms nodes in the TypeScript AST.
|
|
2005
|
+
*
|
|
2006
|
+
* @param context - The transformation context provided by TypeScript
|
|
2007
|
+
* @param node - The current node being visited
|
|
2008
|
+
* @returns A transformed node with export modifiers removed where applicable
|
|
2009
|
+
*
|
|
2010
|
+
* @remarks
|
|
2011
|
+
* This method is the core of the AST transformation process.
|
|
2012
|
+
*
|
|
2013
|
+
* It:
|
|
2014
|
+
* 1. Checks if the current node is one that can have modifiers
|
|
2015
|
+
* 2. If so, checks if it has any export or default modifiers
|
|
2016
|
+
* 3. If export modifiers are found, remove them using updateNodeWithoutExports
|
|
2017
|
+
* 4. Recursively visit all child nodes regardless of whether the current node was modified
|
|
2018
|
+
*
|
|
2019
|
+
* The recursion ensures that all nodes in the entire syntax tree are processed,
|
|
2020
|
+
* resulting in a complete transformation that removes all export modifiers.
|
|
2021
|
+
*
|
|
2022
|
+
* @see isNodeWithModifiers
|
|
2023
|
+
* @see updateNodeWithoutExports
|
|
2024
|
+
* @see ts.visitEachChild
|
|
2025
|
+
*
|
|
2026
|
+
* @since 1.5.5
|
|
2027
|
+
*/
|
|
2028
|
+
private visitNode;
|
|
2029
|
+
/**
|
|
2030
|
+
* Processes top-level TypeScript statements by handling imports, exports, and module declarations
|
|
2031
|
+
* during transformation. This method filters internal imports, removes exports, and flattens module
|
|
2032
|
+
* declarations to generate optimized output.
|
|
2033
|
+
*
|
|
2034
|
+
* @param node - The TypeScript statement being processed
|
|
2035
|
+
* @param importMap - Collection of import clauses organized by module specifier
|
|
2036
|
+
* @param sourceFiles - Array of internal source file paths that should be excluded
|
|
2037
|
+
* @param context - TypeScript transformation context for node factory operations
|
|
2038
|
+
* @returns Array of transformed statements or empty array if the statement is removed
|
|
2039
|
+
*
|
|
2040
|
+
* @throws TypesError - When encountering unsupported node types
|
|
2041
|
+
*
|
|
2042
|
+
* @remarks
|
|
2043
|
+
* The transformation logic handles several specific cases:
|
|
2044
|
+
* - Import declarations: Collects external imports and filters internal ones
|
|
2045
|
+
* - Import equals declarations: Removes them from output
|
|
2046
|
+
* - Export declarations: Removes them entirely
|
|
2047
|
+
* - Module declarations: Unwraps content from declare modules, preserves others
|
|
2048
|
+
*
|
|
2049
|
+
* @example
|
|
2050
|
+
* ```ts
|
|
2051
|
+
* const result = this.visitTopLevelStatement(
|
|
2052
|
+
* sourceFile.statements[0],
|
|
2053
|
+
* new Map<string, Array<ts.ImportClause>>(),
|
|
2054
|
+
* ['./internal-module'],
|
|
2055
|
+
* transformContext
|
|
2056
|
+
* );
|
|
2057
|
+
* ```
|
|
2058
|
+
*
|
|
2059
|
+
* @see ts.Statement
|
|
2060
|
+
* @see ts.ImportDeclaration
|
|
2061
|
+
* @see ts.ModuleDeclaration
|
|
2062
|
+
*
|
|
2063
|
+
* @since 1.5.5
|
|
2064
|
+
*/
|
|
2065
|
+
private visitTopLevelStatement;
|
|
2066
|
+
/**
|
|
2067
|
+
* Transforms a TypeScript source file by processing all its top-level statements and
|
|
2068
|
+
* updating the source file with the processed statements while preserving metadata.
|
|
2069
|
+
*
|
|
2070
|
+
* @param sourceFile - The TypeScript source file to transform
|
|
2071
|
+
* @param importMap - Collection mapping module specifiers to their import clauses
|
|
2072
|
+
* @param sourceFiles - Array of source file paths used to identify internal modules
|
|
2073
|
+
* @param context - The TypeScript transformation context
|
|
2074
|
+
* @returns A new TypeScript source file with transformed statements
|
|
2075
|
+
*
|
|
2076
|
+
* @remarks
|
|
2077
|
+
* This method flattens all source file statements by passing them through the
|
|
2078
|
+
* visitTopLevelStatement processor, then creates an updated source file that
|
|
2079
|
+
* preserves all original metadata including declaration status and references.
|
|
2080
|
+
*
|
|
2081
|
+
* @example
|
|
2082
|
+
* ```ts
|
|
2083
|
+
* const transformedFile = this.visitSourceFile(
|
|
2084
|
+
* program.getSourceFile("main.ts"),
|
|
2085
|
+
* new Map<string, Array<ts.ImportClause>>(),
|
|
2086
|
+
* ["./src/internal.ts"],
|
|
2087
|
+
* transformationContext
|
|
2088
|
+
* );
|
|
2089
|
+
* ```
|
|
2090
|
+
*
|
|
2091
|
+
* @throws TypesError - When transformation encounters incompatible node types
|
|
2092
|
+
*
|
|
2093
|
+
* @see ts.SourceFile
|
|
2094
|
+
* @see ts.TransformationContext
|
|
2095
|
+
*
|
|
2096
|
+
* @since 1.5.5
|
|
2097
|
+
*/
|
|
2098
|
+
private visitSourceFile;
|
|
2099
|
+
/**
|
|
2100
|
+
* Merges multiple TypeScript import clauses into a single consolidated import clause,
|
|
2101
|
+
* handling default imports, named imports, and namespace imports.
|
|
2102
|
+
*
|
|
2103
|
+
* @param importClauses - Array of import clauses to merge
|
|
2104
|
+
* @param isTypeOnly - Whether to force the resulting import to be type-only, defaults to true
|
|
2105
|
+
* @returns A consolidated import clause or undefined if no import clauses provided
|
|
2106
|
+
*
|
|
2107
|
+
* @throws TypesError - When encountering incompatible import clause structures
|
|
2108
|
+
*
|
|
2109
|
+
* @remarks
|
|
2110
|
+
* The merging process follows these rules:
|
|
2111
|
+
* - Uses the first default import encountered
|
|
2112
|
+
* - Collects all unique named imports across all clauses
|
|
2113
|
+
* - Uses the first namespace import encountered
|
|
2114
|
+
* - Handles type-only status according to the isTypeOnly parameter or source clauses
|
|
2115
|
+
* - Returns undefined if the input array is empty
|
|
2116
|
+
* - Returns the original clause if there's only one and not forcing type-only
|
|
2117
|
+
*
|
|
2118
|
+
* @example
|
|
2119
|
+
* ```ts
|
|
2120
|
+
* const imports = [
|
|
2121
|
+
* ts.factory.createImportClause(false, ts.factory.createIdentifier("default1"), undefined),
|
|
2122
|
+
* ts.factory.createImportClause(
|
|
2123
|
+
* false,
|
|
2124
|
+
* undefined,
|
|
2125
|
+
* ts.factory.createNamedImports([
|
|
2126
|
+
* ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier("named1"))
|
|
2127
|
+
* ])
|
|
2128
|
+
* )
|
|
2129
|
+
* ];
|
|
2130
|
+
* const mergedImport = this.mergeImportClauses(imports, true);
|
|
2131
|
+
* ```
|
|
2132
|
+
*
|
|
2133
|
+
* @see ts.ImportClause
|
|
2134
|
+
* @see ts.NamedImports
|
|
2135
|
+
* @see ts.NamespaceImport
|
|
2136
|
+
*
|
|
2137
|
+
* @since 1.5.6
|
|
2138
|
+
*/
|
|
2139
|
+
private mergeImportClauses;
|
|
2140
|
+
/**
|
|
2141
|
+
* Creates a transformer factory that cleans up declarations in TypeScript bundles.
|
|
2142
|
+
* This transformer extracts external imports from each source file in the bundle
|
|
2143
|
+
* and consolidates them into a single imports file at the beginning of the bundle.
|
|
2144
|
+
*
|
|
2145
|
+
* @returns A transformer factory function that transforms TypeScript bundles
|
|
2146
|
+
*
|
|
2147
|
+
* @throws Error - When the input is not a bundle but a single source file
|
|
2148
|
+
*
|
|
2149
|
+
* @remarks
|
|
2150
|
+
* The transformer processes each source file in the bundle, extracts external imports,
|
|
2151
|
+
* and creates a new bundle with all imports consolidated at the beginning.
|
|
2152
|
+
*
|
|
2153
|
+
* @example
|
|
2154
|
+
* ```ts
|
|
2155
|
+
* const transformer = myClass.cleanupDeclarations();
|
|
2156
|
+
* const result = ts.transform(bundle, [transformer]);
|
|
2157
|
+
* ```
|
|
2158
|
+
*
|
|
2159
|
+
* @see ts.TransformerFactory
|
|
2160
|
+
* @see ts.Bundle
|
|
2161
|
+
*
|
|
2162
|
+
* @since 1.5.5
|
|
2163
|
+
*/
|
|
2164
|
+
private cleanupDeclarations;
|
|
2165
|
+
}
|
|
2166
|
+
/**
|
|
2167
|
+
* The default configuration options for the build.
|
|
2168
|
+
*
|
|
2169
|
+
* @example
|
|
2170
|
+
* ```ts
|
|
2171
|
+
* import { defaultConfiguration } from '@configuration/default-configuration';
|
|
2172
|
+
*
|
|
2173
|
+
* console.log(defaultConfiguration);
|
|
2174
|
+
* ```
|
|
2175
|
+
*
|
|
2176
|
+
* In this example, the `defaultConfiguration` is imported and logged to the console to view the default settings.
|
|
2177
|
+
*
|
|
2178
|
+
* @public
|
|
2179
|
+
* @category Configuration
|
|
2180
|
+
*/
|
|
2181
|
+
export declare const defaultConfiguration: ConfigurationInterface;
|
|
2182
|
+
/**
|
|
2183
|
+
* Executes JavaScript code within a sandboxed environment using Node.js's `vm` module.
|
|
2184
|
+
*
|
|
2185
|
+
* @param code - The JavaScript code to be executed within the sandbox.
|
|
2186
|
+
* @param sandbox - An optional context object to be used as the global scope for the executed code.
|
|
2187
|
+
*
|
|
2188
|
+
* @returns The result of executing the provided code within the sandboxed environment.
|
|
2189
|
+
*
|
|
2190
|
+
* @remarks
|
|
2191
|
+
* The `sandboxExecute` function creates a new `Script` instance with the provided code and
|
|
2192
|
+
* runs it within a sandboxed context using the `createContext` function from the `vm` module.
|
|
2193
|
+
* This approach ensures that the executed code is isolated from the rest of the application,
|
|
2194
|
+
* mitigating potential security risks.
|
|
2195
|
+
*
|
|
2196
|
+
* The `sandbox` parameter allows you to provide a custom context or global object for the
|
|
2197
|
+
* sandboxed code. If not provided, an empty context is used. The function also supports
|
|
2198
|
+
* breaking execution on interrupt signals (e.g., Ctrl+C) with the `breakOnSigint` option.
|
|
2199
|
+
*
|
|
2200
|
+
* @throws Error Throws an error if the code cannot be compiled or executed within the context.
|
|
2201
|
+
*
|
|
2202
|
+
* @example
|
|
2203
|
+
* ```ts
|
|
2204
|
+
* const result = sandboxExecute('return 2 + 2;', { myGlobal: 10 });
|
|
2205
|
+
* console.log(result); // Output: 4
|
|
2206
|
+
* ```
|
|
2207
|
+
*
|
|
2208
|
+
* In this example, the `sandboxExecute` function runs a simple JavaScript expression and returns
|
|
2209
|
+
* the result. The `sandbox` parameter is provided with an empty object in this case.
|
|
2210
|
+
*
|
|
2211
|
+
* @public
|
|
2212
|
+
* @category Services
|
|
2213
|
+
*/
|
|
2214
|
+
export declare function sandboxExecute(code: string, sandbox?: Context): any;
|
|
2215
|
+
/**
|
|
2216
|
+
* Parses a configuration file and returns a wrapped `ConfigurationInterface` object.
|
|
2217
|
+
*
|
|
2218
|
+
* This function reads the specified configuration file, transpiles it to a CommonJS format, and then executes it
|
|
2219
|
+
* in a sandbox environment. The exported configuration object is wrapped so that any functions it contains will
|
|
2220
|
+
* have sourcemap information attached to errors thrown during their execution.
|
|
2221
|
+
*
|
|
2222
|
+
* The wrapping of functions helps in debugging by associating errors with their source maps.
|
|
2223
|
+
*
|
|
2224
|
+
* @param file - The path to the configuration file that needs to be parsed and transpiled.
|
|
2225
|
+
*
|
|
2226
|
+
* @returns A promise that resolves to the parsed and transpiled `ConfigurationInterface` object.
|
|
2227
|
+
* This object has its functions wrapped to attach sourcemap information to any errors thrown.
|
|
2228
|
+
*
|
|
2229
|
+
* @throws Will throw an error if the transpilation or execution of the configuration file fails.
|
|
2230
|
+
* The thrown error will have sourcemap information attached if available.
|
|
2231
|
+
|
|
2232
|
+
* @example
|
|
2233
|
+
* ```ts
|
|
2234
|
+
* const config = await parseConfigurationFile('./config.jet.ts');
|
|
2235
|
+
* console.log(config);
|
|
2236
|
+
* ```
|
|
2237
|
+
*/
|
|
2238
|
+
export declare function parseConfigurationFile(file: string): Promise<ConfigurationInterface>;
|
|
2239
|
+
/**
|
|
2240
|
+
* Reads and parses the TypeScript configuration file.
|
|
2241
|
+
*
|
|
2242
|
+
* @param options - A `BuildOptions` object that may contain a custom `tsconfig` path.
|
|
2243
|
+
* @returns A `ParsedCommandLine` object representing the parsed TypeScript configuration.
|
|
2244
|
+
* @throws xBuildError - Throws an error if the configuration file contains syntax errors.
|
|
2245
|
+
*/
|
|
2246
|
+
export declare function tsConfiguration(options: BuildOptions): ParsedCommandLine;
|
|
2247
|
+
/**
|
|
2248
|
+
* Merges user configurations with CLI configurations and default settings
|
|
2249
|
+
* to produce a final configuration object for the build process.
|
|
2250
|
+
* This function handles both single and multiple user configurations,
|
|
2251
|
+
* allowing for flexible configuration merging.
|
|
2252
|
+
*
|
|
2253
|
+
* @param userConfig - An array or a single object of type `PartialDeepConfigurationsType`
|
|
2254
|
+
* representing the user's configurations to merge. If a single object
|
|
2255
|
+
* is provided, it is wrapped in an array for processing.
|
|
2256
|
+
* @param cliConfig - An optional object of type `PartialDeepConfigurationsType` representing
|
|
2257
|
+
* the CLI configurations to merge with the user configurations. Defaults to an empty object.
|
|
2258
|
+
* @returns An array of `ConfigurationInterface` objects, each representing a merged configuration.
|
|
2259
|
+
*
|
|
2260
|
+
* @throws xBuildError Throws an error if the `entryPoints` property in the merged configuration is undefined.
|
|
2261
|
+
* This ensures that the configuration is valid and complete for further processing.
|
|
2262
|
+
*
|
|
2263
|
+
* @example
|
|
2264
|
+
* ```ts
|
|
2265
|
+
* import { configuration } from './configuration';
|
|
2266
|
+
*
|
|
2267
|
+
* const userConfigs = [
|
|
2268
|
+
* { esbuild: { entryPoints: ['src/index.ts'] } },
|
|
2269
|
+
* { serve: { port: 3000 } }
|
|
2270
|
+
* ];
|
|
2271
|
+
* const cliConfigs = { esbuild: { minify: true } };
|
|
2272
|
+
*
|
|
2273
|
+
* const finalConfigs = await configuration(userConfigs, cliConfigs);
|
|
2274
|
+
* console.log('Merged Configuration:', finalConfigs);
|
|
2275
|
+
* ```
|
|
2276
|
+
*/
|
|
2277
|
+
export declare function configuration(userConfig: Array<PartialDeepConfigurationsType> | PartialDeepConfigurationsType, cliConfig?: PartialDeepConfigurationsType): Promise<ConfigurationInterface[]>;
|
|
2278
|
+
/**
|
|
2279
|
+
* Merges CLI arguments with a configuration file to produce a final configuration object.
|
|
2280
|
+
* This function reads the specified configuration file and merges its contents with
|
|
2281
|
+
* the CLI arguments provided. The resulting configuration will be validated to ensure
|
|
2282
|
+
* that required properties, such as `entryPoints`, are defined.
|
|
2283
|
+
*
|
|
2284
|
+
* @param configFile - The path to the configuration file to read and merge with CLI arguments.
|
|
2285
|
+
* @param cli - An instance of `Argv<ArgvInterface>` containing CLI arguments and options.
|
|
2286
|
+
* @returns A promise that resolves to an array of `ConfigurationInterface` objects, representing
|
|
2287
|
+
* the final merged configuration.
|
|
2288
|
+
* @throws Error Throws an error if the `entryPoints` property in the final configuration is undefined.
|
|
2289
|
+
* This ensures that the configuration is valid for further processing.
|
|
2290
|
+
*
|
|
2291
|
+
* @example
|
|
2292
|
+
* ```ts
|
|
2293
|
+
* import { cliConfiguration } from './cli-configuration';
|
|
2294
|
+
*
|
|
2295
|
+
* const configFilePath = './config.json';
|
|
2296
|
+
* const cliArgs = argv(); // Assuming `argv` is a function that retrieves CLI arguments
|
|
2297
|
+
*
|
|
2298
|
+
* cliConfiguration(configFilePath, cliArgs).then((finalConfig) => {
|
|
2299
|
+
* console.log('Final configuration: ', finalConfig);
|
|
2300
|
+
* }).catch((error) => {
|
|
2301
|
+
* console.error('Error loading configuration:', error);
|
|
2302
|
+
* });
|
|
2303
|
+
* ```
|
|
2304
|
+
*/
|
|
2305
|
+
export declare function cliConfiguration(configFile: string, cli: Argv<ArgvInterface>): Promise<Array<ConfigurationInterface>>;
|
|
2306
|
+
/**
|
|
2307
|
+
* Maps an array of file paths to an object where the keys are filenames (without extensions)
|
|
2308
|
+
* and the values are the corresponding file paths.
|
|
2309
|
+
*
|
|
2310
|
+
* Each key in the resulting object is derived from the filename by removing the file extension.
|
|
2311
|
+
* For example, given a file path `src/index.ts`, the key in the resulting object will be `src/index`.
|
|
2312
|
+
*
|
|
2313
|
+
* @param filePaths - An array of file paths to map. Each file path should be a string.
|
|
2314
|
+
* @returns An object where the keys are filenames (without extensions) and the values are the corresponding file paths.
|
|
2315
|
+
*
|
|
2316
|
+
* @example
|
|
2317
|
+
* ```ts
|
|
2318
|
+
* const filePaths = ['src/index.ts', 'src/utils.ts'];
|
|
2319
|
+
* const result = mapFilePathsToNames(filePaths);
|
|
2320
|
+
* console.log(result);
|
|
2321
|
+
* // Output: {
|
|
2322
|
+
* // 'src/index': 'src/index.ts',
|
|
2323
|
+
* // 'src/utils': 'src/utils.ts'
|
|
2324
|
+
* // }
|
|
2325
|
+
* ```
|
|
2326
|
+
*/
|
|
2327
|
+
export declare function mapFilePathsToNames(filePaths: Array<string>): Record<string, string>;
|
|
2328
|
+
/**
|
|
2329
|
+
* Extracts and returns an object mapping output file paths to input file paths from the provided `EntryPoints` object.
|
|
2330
|
+
*
|
|
2331
|
+
* This function handles multiple formats of entry points, including:
|
|
2332
|
+
* - An array of strings representing file paths.
|
|
2333
|
+
* - An array of objects containing `in` and `out` properties, where `in` is the input file path and `out` is the output file path.
|
|
2334
|
+
* - A `Record<string, string>` where the keys represent input file paths and the values represent output file paths.
|
|
2335
|
+
*
|
|
2336
|
+
* Depending on the format, the function constructs an object with the output file paths as keys and the input file paths as values.
|
|
2337
|
+
* If the output path is not available, the filename (without extension) is used as the key.
|
|
2338
|
+
*
|
|
2339
|
+
* If a regular object with string keys and values (not in the supported formats) is provided, it will be returned as is.
|
|
2340
|
+
*
|
|
2341
|
+
* @param entryPoints - The entry points to extract from, which can be in different formats: an array of strings,
|
|
2342
|
+
* an array of objects with `in` and `out` properties, or a `Record<string, string>`.
|
|
2343
|
+
*
|
|
2344
|
+
* @returns An object mapping output file paths to input file paths, or filename (without extension) to file path.
|
|
2345
|
+
*
|
|
2346
|
+
* @throws Will throw an `Error` if the entry points format is unsupported.
|
|
2347
|
+
*
|
|
2348
|
+
* @example
|
|
2349
|
+
* ```ts
|
|
2350
|
+
* const entryPoints = extractEntryPoints(['src/index.ts', 'src/utils.ts']);
|
|
2351
|
+
* console.log(entryPoints); // { 'index': 'src/index.ts', 'utils': 'src/utils.ts' }
|
|
2352
|
+
* ```
|
|
2353
|
+
*
|
|
2354
|
+
* @example
|
|
2355
|
+
* ```ts
|
|
2356
|
+
* const entryPoints = extractEntryPoints([{ in: 'src/index.ts', out: 'dist/index.js' }]);
|
|
2357
|
+
* console.log(entryPoints); // { 'dist/index.js': 'src/index.ts' }
|
|
2358
|
+
* ```
|
|
2359
|
+
*
|
|
2360
|
+
* @example
|
|
2361
|
+
* ```ts
|
|
2362
|
+
* const entryPoints = extractEntryPoints({ index: 'src/index.ts', index2: 'dist/index2.js' });
|
|
2363
|
+
* console.log(entryPoints); // { index: 'src/index.ts', index2: 'dist/index2.js' }
|
|
2364
|
+
* ```
|
|
2365
|
+
*/
|
|
2366
|
+
export declare function extractEntryPoints(entryPoints: EntryPoints): Record<string, string>;
|
|
2367
|
+
/**
|
|
2368
|
+
* Generates a `package.json` file with the appropriate `type` field
|
|
2369
|
+
* based on the format specified in the configuration.
|
|
2370
|
+
*
|
|
2371
|
+
* - If the format is `esm`, the `type` will be set to `"module"`.
|
|
2372
|
+
* - If the format is `cjs`, the `type` will be set to `"commonjs"`.
|
|
2373
|
+
*
|
|
2374
|
+
* The function will ensure that the specified output directory exists, and if it doesn't,
|
|
2375
|
+
* it will create the necessary directories before writing the `package.json` file.
|
|
2376
|
+
*
|
|
2377
|
+
* @param config - The build configuration object containing
|
|
2378
|
+
* esbuild-related settings, such as the format (`format`).
|
|
2379
|
+
*
|
|
2380
|
+
* - `config.esbuild.format`: The module format, either `'esm'` or `'cjs'`, that determines the `type` field.
|
|
2381
|
+
*
|
|
2382
|
+
* @throws Will throw an error if there is a problem creating the directory or writing the file.
|
|
2383
|
+
*
|
|
2384
|
+
* Example usage:
|
|
2385
|
+
*
|
|
2386
|
+
* ```ts
|
|
2387
|
+
* const config = {
|
|
2388
|
+
* esbuild: {
|
|
2389
|
+
* format: 'esm'
|
|
2390
|
+
* }
|
|
2391
|
+
* };
|
|
2392
|
+
* packageTypeComponent(config);
|
|
2393
|
+
* // This will create 'dist/package.json' with the content: {"type": "module"}
|
|
2394
|
+
* ```
|
|
2395
|
+
*/
|
|
2396
|
+
export declare function packageTypeComponent(config: ConfigurationInterface): void;
|
|
2397
|
+
/**
|
|
2398
|
+
* Manages the build process for a TypeScript project using esbuild.
|
|
2399
|
+
*
|
|
2400
|
+
* The `BuildService` class orchestrates the build process, including TypeScript compilation, handling of build errors,
|
|
2401
|
+
* and lifecycle management of the build. It can operate in various modes, such as watching for file changes or running
|
|
2402
|
+
* in development mode. It also provides functionality for spawning development processes and processing entry points.
|
|
2403
|
+
*
|
|
2404
|
+
* @remarks
|
|
2405
|
+
* - The build process can be configured using the provided `ConfigurationInterface`.
|
|
2406
|
+
* - Errors related to TypeScript are handled separately and are not logged by default.
|
|
2407
|
+
* - The class supports various build modes, including watch mode and development mode, and handles different scenarios
|
|
2408
|
+
* based on the configuration.
|
|
2409
|
+
*
|
|
2410
|
+
* @public
|
|
2411
|
+
* @category Services
|
|
2412
|
+
*/
|
|
2413
|
+
export declare class BuildService {
|
|
2414
|
+
private config;
|
|
2415
|
+
/**
|
|
2416
|
+
* Provides TypeScript-related functionality for the build process.
|
|
2417
|
+
*/
|
|
2418
|
+
readonly typeScriptProvider: TypeScriptProvider;
|
|
2419
|
+
/**
|
|
2420
|
+
* Keeps track of active development processes spawned during the build.
|
|
2421
|
+
* This property holds an array of `ChildProcessWithoutNullStreams` instances that represent Node.js processes spawned
|
|
2422
|
+
* for running development tasks. These processes are used to handle development builds or runtime tasks and are managed
|
|
2423
|
+
* by the `BuildService` class to ensure they are properly started and stopped.
|
|
2424
|
+
*
|
|
2425
|
+
* @remarks
|
|
2426
|
+
* - The array is populated when development processes are spawned, such as when specific development files are
|
|
2427
|
+
* processed or when running in development mode.
|
|
2428
|
+
* - The processes are terminated gracefully at the end of the build to avoid leaving orphaned processes running.
|
|
2429
|
+
* - It is important to manage these processes correctly to avoid resource leaks and ensure proper cleanup.
|
|
2430
|
+
*
|
|
2431
|
+
* @see ChildProcessWithoutNullStreams
|
|
2432
|
+
*/
|
|
2433
|
+
private activePossess;
|
|
2434
|
+
/**
|
|
2435
|
+
* Plugin provider
|
|
2436
|
+
*
|
|
2437
|
+
* @private
|
|
2438
|
+
*/
|
|
2439
|
+
private pluginsProvider;
|
|
2440
|
+
/**
|
|
2441
|
+
* Initializes the build service with the provided configuration.
|
|
2442
|
+
*
|
|
2443
|
+
* The constructor configures the TypeScript provider, suppresses esbuild logging,
|
|
2444
|
+
* sets up development modes, and registers the necessary plugins.
|
|
2445
|
+
*
|
|
2446
|
+
* Declaration files will be output based on the following order of precedence:
|
|
2447
|
+
* 1. If `declarationOutDir` is set in the configuration, it will be used.
|
|
2448
|
+
* 2. If `declarationOutDir` is not provided, it will use the `outDir` value from the tsconfig.
|
|
2449
|
+
* 3. If neither of the above is available, it falls back to using the `outdir` specified in the esbuild configuration.
|
|
2450
|
+
*
|
|
2451
|
+
* @param config - The configuration object for the build process, including esbuild and TypeScript settings.
|
|
2452
|
+
*/
|
|
2453
|
+
constructor(config: ConfigurationInterface);
|
|
2454
|
+
/**
|
|
2455
|
+
* Executes the build process.
|
|
2456
|
+
* This method performs the build and handles any errors that occur during the execution.
|
|
2457
|
+
* If watching or development mode is enabled in the configuration, it starts watching for changes
|
|
2458
|
+
* to automatically rebuild as needed.
|
|
2459
|
+
* The method logs errors that are not related to TypeScript
|
|
2460
|
+
* compilation issues.
|
|
2461
|
+
*
|
|
2462
|
+
* @returns A promise that resolves with a `BuildResult` when the build process is complete,
|
|
2463
|
+
* or `undefined` if an error occurs during execution.
|
|
2464
|
+
*
|
|
2465
|
+
* @throws Error Throws an error if the build process encounters issues that are not related
|
|
2466
|
+
* to TypeScript. Such errors are logged, but the method does not rethrow them.
|
|
2467
|
+
*
|
|
2468
|
+
* @example
|
|
2469
|
+
* ```ts
|
|
2470
|
+
* import { BuildService } from './build-service';
|
|
2471
|
+
*
|
|
2472
|
+
* const buildService = new BuildService(config);
|
|
2473
|
+
* buildService.run().then(() => {
|
|
2474
|
+
* console.log('Build process completed successfully.');
|
|
2475
|
+
* }).catch((error) => {
|
|
2476
|
+
* console.error('Build process failed:', error);
|
|
2477
|
+
* });
|
|
2478
|
+
* ```
|
|
2479
|
+
*
|
|
2480
|
+
* In this example, the `run` method is invoked to execute the build process. It handles both successful
|
|
2481
|
+
* completion and logs any encountered errors, allowing the user to understand the outcome of the build.
|
|
2482
|
+
*/
|
|
2483
|
+
run(): Promise<BuildResult | void>;
|
|
2484
|
+
/**
|
|
2485
|
+
* Runs the build process in debug mode for the specified entry points.
|
|
2486
|
+
* This method temporarily disables development and watch mode, initiates the build process, and spawns development processes
|
|
2487
|
+
* for the specified entry points. If any errors occur during the build, they are handled appropriately.
|
|
2488
|
+
*
|
|
2489
|
+
* @param entryPoints - An array of entry point file names for which the development processes will be spawned.
|
|
2490
|
+
* These entry points are matched against the build output files.
|
|
2491
|
+
*
|
|
2492
|
+
* @returns A `Promise<void>` that resolves when the build and process spawning have completed.
|
|
2493
|
+
*
|
|
2494
|
+
* @throws Handles any build-related errors using the `handleErrors` method.
|
|
2495
|
+
*
|
|
2496
|
+
* @remarks
|
|
2497
|
+
* - The `config.dev` and `config.watch` settings are temporarily disabled to prevent development mode or file watching during the build.
|
|
2498
|
+
* - The `build()` method is called to generate the necessary build outputs.
|
|
2499
|
+
* - The `spawnDev` method is then invoked to spawn processes for the matching entry points.
|
|
2500
|
+
* - If any errors occur during the build, they are caught and passed to the `handleErrors` method.
|
|
2501
|
+
*
|
|
2502
|
+
* @example
|
|
2503
|
+
* ```ts
|
|
2504
|
+
* const entryPoints = ['index', 'main'];
|
|
2505
|
+
* await this.runDebug(entryPoints);
|
|
2506
|
+
* ```
|
|
2507
|
+
*
|
|
2508
|
+
* In this example, the `runDebug` method runs the build process and spawns development processes for `index` and `main`.
|
|
2509
|
+
*
|
|
2510
|
+
* @public
|
|
2511
|
+
*/
|
|
2512
|
+
runDebug(entryPoints: Array<string>): Promise<void>;
|
|
2513
|
+
/**
|
|
2514
|
+
* Serves the project and watches for changes.
|
|
2515
|
+
* This method starts the development server using the `ServerProvider`, builds the project using esbuild,
|
|
2516
|
+
* and watches for file changes to automatically rebuild as needed. It initializes the server and invokes
|
|
2517
|
+
* the build process, enabling continuous development mode.
|
|
2518
|
+
*
|
|
2519
|
+
* @returns A promise that resolves when the server is started and the build process is complete.
|
|
2520
|
+
*
|
|
2521
|
+
* @throws This method catches any errors thrown during the build process and handles them using the
|
|
2522
|
+
* `handleErrors` method.
|
|
2523
|
+
*
|
|
2524
|
+
* @example
|
|
2525
|
+
* ```ts
|
|
2526
|
+
* const buildService = new BuildService(config);
|
|
2527
|
+
* buildService.serve().then(() => {
|
|
2528
|
+
* console.log('Server is running and watching for changes.');
|
|
2529
|
+
* }).catch((error) => {
|
|
2530
|
+
* console.error('Failed to start the server:', error);
|
|
2531
|
+
* });
|
|
2532
|
+
* ```
|
|
2533
|
+
*
|
|
2534
|
+
* In this example, the `serve` method starts the server and watches for changes. If an error occurs during
|
|
2535
|
+
* the build or server startup, it is handled and logged.
|
|
2536
|
+
*/
|
|
2537
|
+
serve(): Promise<void>;
|
|
2538
|
+
/**
|
|
2539
|
+
* Executes a provided asynchronous callback function within a try-catch block.
|
|
2540
|
+
* This method ensures that any errors thrown during the execution of the callback
|
|
2541
|
+
* are properly handled and logged. If the error appears to be an `esbuild`-related
|
|
2542
|
+
* `OnEndResult` error with an array of errors, it avoids redundant logging.
|
|
2543
|
+
* Otherwise, it wraps the error in a `VMRuntimeError` and logs the stack trace.
|
|
2544
|
+
*
|
|
2545
|
+
* @template T - The return type of the callback function, allowing flexibility
|
|
2546
|
+
* in the expected result type. Defaults to `BuildResult`.
|
|
2547
|
+
*
|
|
2548
|
+
* @param callback - A function that returns a `Promise<T>`, which is executed asynchronously.
|
|
2549
|
+
* The callback is wrapped in error handling logic to catch and process any exceptions.
|
|
2550
|
+
*
|
|
2551
|
+
* @returns A `Promise<T | void>` that resolves with the result of the callback function if successful,
|
|
2552
|
+
* or `void` if an error was thrown and handled. This allows for optional chaining on the return value.
|
|
2553
|
+
*
|
|
2554
|
+
* @throws This method does not throw explicitly but will log an error message if an exception is caught
|
|
2555
|
+
* and is not an `esbuild`-related error. The error stack is logged via `VMRuntimeError` for non-esbuild errors.
|
|
2556
|
+
*
|
|
2557
|
+
* @example
|
|
2558
|
+
* ```ts
|
|
2559
|
+
* await execute(async () => {
|
|
2560
|
+
* // Perform some asynchronous operation here
|
|
2561
|
+
* return someResult;
|
|
2562
|
+
* });
|
|
2563
|
+
* ```
|
|
2564
|
+
*/
|
|
2565
|
+
private execute;
|
|
2566
|
+
/**
|
|
2567
|
+
* Configures the development mode by ensuring that `config.dev` is set properly.
|
|
2568
|
+
*/
|
|
2569
|
+
private configureDevelopmentMode;
|
|
2570
|
+
/**
|
|
2571
|
+
* Sets up the plugin's provider and registers the plugin hooks.
|
|
2572
|
+
*/
|
|
2573
|
+
private setupPlugins;
|
|
2574
|
+
/**
|
|
2575
|
+
* Registers the plugin hooks for start, end, and load events.
|
|
2576
|
+
*
|
|
2577
|
+
* @param paths - The resolved path aliases.
|
|
2578
|
+
* @param rootDir - The root directory for resolving paths.
|
|
2579
|
+
*/
|
|
2580
|
+
private registerPluginHooks;
|
|
2581
|
+
/**
|
|
2582
|
+
* Generates a path alias object from the TypeScript provider's path options.
|
|
2583
|
+
* This method processes the `paths` property from the TypeScript provider's options,
|
|
2584
|
+
* which is expected to be an object where each key represents a path alias pattern,
|
|
2585
|
+
* and the corresponding value is an array of paths. The method removes any wildcard
|
|
2586
|
+
* characters (`*`) from both the keys and the first values of the arrays. It also
|
|
2587
|
+
* resolves the paths relative to the specified `rootDir`, returning a simplified
|
|
2588
|
+
* object that maps the cleaned keys to their respective paths.
|
|
2589
|
+
*
|
|
2590
|
+
* The resolved paths will be formatted to use a relative path notation.
|
|
2591
|
+
*
|
|
2592
|
+
* Example:
|
|
2593
|
+
* Given the following paths:
|
|
2594
|
+
* ```ts
|
|
2595
|
+
* {
|
|
2596
|
+
* '@core/*': ['src/core/*'],
|
|
2597
|
+
* '@utils/*': ['src/utils/*']
|
|
2598
|
+
* }
|
|
2599
|
+
* ```
|
|
2600
|
+
* And assuming `rootDir` is set to the base directory of your project, the method
|
|
2601
|
+
* will return:
|
|
2602
|
+
* ```ts
|
|
2603
|
+
* {
|
|
2604
|
+
* '@core/': './core/',
|
|
2605
|
+
* '@utils/': './utils/'
|
|
2606
|
+
* }
|
|
2607
|
+
* ```
|
|
2608
|
+
*
|
|
2609
|
+
* @param rootDir - The root directory to resolve paths against.
|
|
2610
|
+
* @returns An object mapping cleaned path aliases to their respective resolved paths.
|
|
2611
|
+
*/
|
|
2612
|
+
private generatePathAlias;
|
|
2613
|
+
/**
|
|
2614
|
+
* Handles errors during the build process.
|
|
2615
|
+
* This method processes and logs errors that occur during the esbuild process. It specifically filters out
|
|
2616
|
+
* errors related to TypeScript (`TypesError`) to prevent them from being logged, while logging all other errors
|
|
2617
|
+
* to the console. The error object is assumed to contain a list of messages, each with detailed information.
|
|
2618
|
+
*
|
|
2619
|
+
* @param esbuildError - The error object returned by esbuild, which is expected to contain an array of
|
|
2620
|
+
* error messages.
|
|
2621
|
+
*
|
|
2622
|
+
* @private
|
|
2623
|
+
*
|
|
2624
|
+
* @remarks
|
|
2625
|
+
* - TypeScript errors (denoted as `TypesError`) are skipped and not logged.
|
|
2626
|
+
* - Other errors are logged to the console with their text descriptions.
|
|
2627
|
+
*
|
|
2628
|
+
* @example
|
|
2629
|
+
* ```ts
|
|
2630
|
+
* try {
|
|
2631
|
+
* await buildService.run();
|
|
2632
|
+
* } catch (esbuildError) {
|
|
2633
|
+
* buildService.handleErrors(esbuildError);
|
|
2634
|
+
* }
|
|
2635
|
+
* ```
|
|
2636
|
+
*
|
|
2637
|
+
* In this example, if an error occurs during the build process, the `handleErrors` method is used to
|
|
2638
|
+
* process and log the errors.
|
|
2639
|
+
*/
|
|
2640
|
+
private handleErrors;
|
|
2641
|
+
/**
|
|
2642
|
+
* Injects a configuration object (banner or footer) into the `esbuild` options.
|
|
2643
|
+
* This method will update the `esbuild` object by adding or modifying the `banner` or `footer`
|
|
2644
|
+
* property based on the provided configuration.
|
|
2645
|
+
* The function handles both static values
|
|
2646
|
+
* and functions within the configuration.
|
|
2647
|
+
*
|
|
2648
|
+
* @param esbuild - The `esbuild` configuration object where the `banner` or `footer`
|
|
2649
|
+
* should be injected or updated.
|
|
2650
|
+
* @param object - The configuration object that contains the properties to inject.
|
|
2651
|
+
* The properties can either be static values or functions.
|
|
2652
|
+
* @param name - A string that determines whether the method modifies the `banner` or `footer`
|
|
2653
|
+
* property of the `esbuild` object.
|
|
2654
|
+
*
|
|
2655
|
+
* @returns void - This method does not return any value.
|
|
2656
|
+
* It modifies the `esbuild` object directly.
|
|
2657
|
+
*
|
|
2658
|
+
* @throws Error - If the `object` parameter is not provided, nothing is injected.
|
|
2659
|
+
* No action will be taken if the specific `name` property (either
|
|
2660
|
+
* 'banner' or 'footer') does not exist in the `esbuild` object.
|
|
2661
|
+
*/
|
|
2662
|
+
private injects;
|
|
2663
|
+
/**
|
|
2664
|
+
* Builds the project based on the configuration.
|
|
2665
|
+
* Depending on the configuration, this method either uses esbuild's `context` for watching or `build` for a one-time build.
|
|
2666
|
+
*
|
|
2667
|
+
* @returns A promise that resolves with the build context or result.
|
|
2668
|
+
*
|
|
2669
|
+
* @private
|
|
2670
|
+
*/
|
|
2671
|
+
private build;
|
|
2672
|
+
/**
|
|
2673
|
+
* Manages development processes for specified entry points.*
|
|
2674
|
+
* This method spawns development processes for each file in the metafile that matches any of the specified entry points.
|
|
2675
|
+
* It enables features like source maps and optional debugging mode for each spawned process.
|
|
2676
|
+
*
|
|
2677
|
+
* @param meta - The metafile containing information about build outputs.
|
|
2678
|
+
* This typically includes a mapping of output files and their dependencies.
|
|
2679
|
+
* @param entryPoint - An array of entry point file names to match against the metafile outputs.
|
|
2680
|
+
* Only files that match these entry points will have development processes spawned.
|
|
2681
|
+
* @param debug - A boolean flag to enable debugging mode for spawned processes.
|
|
2682
|
+
* If `true`, the processes will start in debug mode with the `--inspect-brk` option. Defaults to `false`.
|
|
2683
|
+
*
|
|
2684
|
+
* @returns void
|
|
2685
|
+
*
|
|
2686
|
+
* @remarks
|
|
2687
|
+
* - Files that contain 'map' in their names (e.g., source map files) are ignored and no process is spawned for them.
|
|
2688
|
+
* - For each matching file in the metafile outputs, a new development process is spawned using the `spawn` function.
|
|
2689
|
+
* - The `activePossess` array tracks all spawned processes, allowing further management (e.g., termination).
|
|
2690
|
+
*
|
|
2691
|
+
* @example
|
|
2692
|
+
* ```ts
|
|
2693
|
+
* const meta = {
|
|
2694
|
+
* outputs: {
|
|
2695
|
+
* 'dist/index.js': { \/* ... *\/ },
|
|
2696
|
+
* 'dist/index.js.map': { \/* ... *\/ }
|
|
2697
|
+
* }
|
|
2698
|
+
* };
|
|
2699
|
+
* const entryPoints = ['index'];
|
|
2700
|
+
*
|
|
2701
|
+
* this.spawnDev(meta, entryPoints, true); // Spawns processes in debug mode
|
|
2702
|
+
* ```
|
|
2703
|
+
*
|
|
2704
|
+
* @private
|
|
2705
|
+
*/
|
|
2706
|
+
private spawnDev;
|
|
2707
|
+
/**
|
|
2708
|
+
* Starts the build process and type checking.
|
|
2709
|
+
* This method performs initial setup for the build and ensures that any child processes are terminated properly.
|
|
2710
|
+
*
|
|
2711
|
+
* @private
|
|
2712
|
+
*/
|
|
2713
|
+
private start;
|
|
2714
|
+
/**
|
|
2715
|
+
* Finalizes the build process and logs results.
|
|
2716
|
+
* This method handles the end of the build process, logs build results, and processes development files if applicable.
|
|
2717
|
+
*
|
|
2718
|
+
* @private
|
|
2719
|
+
*/
|
|
2720
|
+
private end;
|
|
2721
|
+
/**
|
|
2722
|
+
* Processes and updates entry points based on project dependencies.
|
|
2723
|
+
* This method analyzes the project's dependencies and adjusts entry points configuration as needed.
|
|
2724
|
+
*
|
|
2725
|
+
* @private
|
|
2726
|
+
*/
|
|
2727
|
+
private processEntryPoints;
|
|
2728
|
+
}
|
|
2729
|
+
/**
|
|
2730
|
+
* Main run function that initiates the build process based on CLI arguments.
|
|
2731
|
+
*
|
|
2732
|
+
* This function parses the CLI arguments, configures the build settings, and executes
|
|
2733
|
+
* the appropriate build tasks, including type checking, serving, or running in debug mode.
|
|
2734
|
+
*
|
|
2735
|
+
* @param argv - An array of strings representing the CLI arguments.
|
|
2736
|
+
*
|
|
2737
|
+
* @returns A promise that resolves when all build tasks are completed.
|
|
2738
|
+
*
|
|
2739
|
+
* @example
|
|
2740
|
+
* ```ts
|
|
2741
|
+
* await buildWithArgv(process.argv);
|
|
2742
|
+
* ```
|
|
2743
|
+
*/
|
|
2744
|
+
export declare function buildWithArgv(argv: Array<string>): Promise<void>;
|
|
2745
|
+
/**
|
|
2746
|
+
* Builds the project using a configuration file specified by its path.
|
|
2747
|
+
*
|
|
2748
|
+
* This function reads the configuration from the provided file path, processes it,
|
|
2749
|
+
* and initiates the build tasks.
|
|
2750
|
+
*
|
|
2751
|
+
* @param configFilePath - The path to the configuration file to be used for the build.
|
|
2752
|
+
*
|
|
2753
|
+
* @returns A promise that resolves to an array of `BuildResult` objects once all build tasks are completed.
|
|
2754
|
+
*
|
|
2755
|
+
* @throws Error Throws an error if the configuration file does not exist or is invalid.
|
|
2756
|
+
*
|
|
2757
|
+
* @example
|
|
2758
|
+
* ```ts
|
|
2759
|
+
* const results = await buildWithPath('./config.ts');
|
|
2760
|
+
* console.log('Build results:', results);
|
|
2761
|
+
* ```
|
|
2762
|
+
*/
|
|
2763
|
+
export declare function buildWithConfigPath(configFilePath: string): Promise<BuildResult[]>;
|
|
2764
|
+
/**
|
|
2765
|
+
* Builds the project based on the provided configuration object.
|
|
2766
|
+
*
|
|
2767
|
+
* This function processes the given configuration and executes the build tasks accordingly.
|
|
2768
|
+
*
|
|
2769
|
+
* @param config - A partial configuration object used to define the build settings.
|
|
2770
|
+
*
|
|
2771
|
+
* @returns A promise that resolves to an array of `BuildResult` objects once all build tasks are completed.
|
|
2772
|
+
*
|
|
2773
|
+
* @example
|
|
2774
|
+
* ```ts
|
|
2775
|
+
* const results = await build({ entryPoints: ['./src/index.ts'] });
|
|
2776
|
+
* console.log('Build results:', results);
|
|
2777
|
+
* ```
|
|
2778
|
+
*/
|
|
2779
|
+
export declare function build(config: PartialDeepConfigurationsType): Promise<BuildResult[]>;
|