@remotex-labs/xbuild 1.1.1 → 1.3.0

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/README.md CHANGED
@@ -1,91 +1,96 @@
1
- # @remotex-labs/xbuild
2
-
3
- `@remotex-labs/xbuild` is a versatile TypeScript toolchain build system
4
-
5
- ## Installation
6
-
7
- Install `@remotex-labs/xbuild` globally using npm:
8
-
9
- ```bash
10
- npm install -g @remotex-labs/xbuild
11
- ```
12
-
13
- Or using Yarn:
14
-
15
- ```bash
16
- yarn global add @remotex-labs/xbuild
17
- ```
18
-
19
- ## Usage
20
-
21
- Run the `xBuild -h` CLI tool with various options to build your typeScript projects.
22
- Command-Line Options
23
-
24
- ```bash
25
- ______ _ _ _
26
- | ___ \ (_) | | |
27
- __ _| |_/ /_ _ _| | __| |
28
- \ \/ / ___ \ | | | | |/ _` |
29
- > <| |_/ / |_| | | | (_| |
30
- /_/\_\____/ \__,_|_|_|\__,_|
31
-
32
- Version: 0.0.2-beta.0
33
-
34
- index.js [file]
35
-
36
- A versatile JavaScript and TypeScript toolchain build system.
37
-
38
- Positionals:
39
- entryPoints The file entryPoints to build [string]
40
-
41
- Options:
42
- -h, --help Show help [boolean]
43
- -n, --node Build for node platform [boolean] [default: false]
44
- -d, --dev Array entryPoints to run for development [array]
45
- -s, --serve Serve the build folder over HTTP
46
- [boolean] [default: false]
47
- -o, --outdir Output directory [string] [default: "dist"]
48
- --declaration, --de Add TypeScript declarations
49
- [boolean] [default: false]
50
- -w, --watch Watch for file changes [boolean] [default: false]
51
- -c, --config Build configuration file (js/ts)
52
- [string] [default: "xbuild.config.ts"]
53
- --tsconfig, --tc Set TypeScript configuration file to use
54
- [string] [default: "tsconfig.json"]
55
- -m, --minify Minify the code [boolean] [default: false]
56
- -b, --bundle Bundle the code [boolean] [default: false]
57
- --noTypeChecker, --ntc Skip TypeScript type checking
58
- [boolean] [default: false]
59
- --buildOnError, --boe Continue building even if there are TypeScript
60
- type errors [boolean] [default: false]
61
- -v, --version Show version number [boolean] [default: false]
62
-
63
- ```
64
-
65
- ## Configuration
66
-
67
- The `xBuild` configuration file allows you to customize various settings for the build and development process. By default, xbuild uses `xbuild.config.ts` (`--config` change it). Here’s how you can configure it:
68
-
69
- ### Example Configuration
70
- ```typescript
71
- const config: ConfigurationInterface = {
72
- declaration: true,
73
- buildOnError: false,
74
- noTypeChecker: false,
75
- esbuild: {
76
- entryPoints: ['./src/index.ts'],
77
- bundle: true,
78
- minify: true,
79
- format: 'esm',
80
- },
81
- serve: {
82
- active: true,
83
- port: 8080,
84
- host: 'localhost',
85
- onRequest: (req, res, next) => {
86
- console.log('Server request received');
87
- next();
88
- }
89
- }
90
- };
91
- ```
1
+ # @remotex-labs/xbuild
2
+
3
+ `@remotex-labs/xbuild` is a versatile TypeScript toolchain build system
4
+
5
+ ## Installation
6
+
7
+ Install `@remotex-labs/xbuild` globally using npm:
8
+
9
+ ```bash
10
+ npm install -g @remotex-labs/xbuild
11
+ ```
12
+
13
+ Or using Yarn:
14
+
15
+ ```bash
16
+ yarn global add @remotex-labs/xbuild
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Run the `xBuild -h` CLI tool with various options to build your typeScript projects.
22
+ Command-Line Options
23
+
24
+ ```bash
25
+
26
+
27
+ ______ _ _ _
28
+ | ___ \ (_) | | |
29
+ __ _| |_/ /_ _ _| | __| |
30
+ \ \/ / ___ \ | | | | |/ _` |
31
+ > <| |_/ / |_| | | | (_| |
32
+ /_/\_\____/ \__,_|_|_|\__,_|
33
+
34
+ Version: <xBuild version>
35
+
36
+ index.js [file]
37
+
38
+ A versatile JavaScript and TypeScript toolchain build system.
39
+
40
+ Positionals:
41
+ entryPoints The file entryPoints to build [string]
42
+
43
+ Options:
44
+ -h, --help Show help [boolean]
45
+ --typeCheck, --tc Perform type checking [boolean]
46
+ -n, --node Build for node platform [boolean]
47
+ -d, --dev Array entryPoints to run as development in Node.js
48
+ [array]
49
+ --debug, --db Array entryPoints to run in Node.js with debug sta
50
+ te [array]
51
+ -s, --serve Serve the build folder over HTTP [boolean]
52
+ -o, --outdir Output directory [string]
53
+ --declaration, --de Add TypeScript declarations [boolean]
54
+ -w, --watch Watch for file changes [boolean]
55
+ -c, --config Build configuration file (js/ts)
56
+ [string] [default: "xbuild.config.ts"]
57
+ --tsconfig, --tsc Set TypeScript configuration file to use
58
+ [string] [default: "tsconfig.json"]
59
+ -m, --minify Minify the code [boolean]
60
+ -b, --bundle Bundle the code [boolean]
61
+ --noTypeChecker, --ntc Skip TypeScript type checking [boolean]
62
+ --buildOnError, --boe Continue building even if there are TypeScript typ
63
+ e errors [boolean]
64
+ -f, --format Defines the format for the build output ('cjs' | '
65
+ esm' | 'iif'). [string]
66
+ -v, --version Show version number [boolean] [default: false]
67
+
68
+ ```
69
+
70
+ ## Configuration
71
+
72
+ The `xBuild` configuration file allows you to customize various settings for the build and development process. By default, xbuild uses `xbuild.config.ts` (`--config` change it). Here’s how you can configure it:
73
+
74
+ ### Example Configuration
75
+ ```typescript
76
+ const config: ConfigurationInterface = {
77
+ declaration: true,
78
+ buildOnError: false,
79
+ noTypeChecker: false,
80
+ esbuild: {
81
+ entryPoints: ['./src/index.ts'],
82
+ bundle: true,
83
+ minify: true,
84
+ format: 'esm',
85
+ },
86
+ serve: {
87
+ active: true,
88
+ port: 8080,
89
+ host: 'localhost',
90
+ onRequest: (req, res, next) => {
91
+ console.log('Server request received');
92
+ next();
93
+ }
94
+ }
95
+ };
96
+ ```
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Import will remove at compile time
3
+ */
4
+ import type { ConfigurationInterface } from "../configuration/interfaces/configuration.interface";
5
+ /**
6
+ * Generates a `package.json` file with the appropriate `type` field
7
+ * based on the format specified in the configuration.
8
+ *
9
+ * - If the format is `esm`, the `type` will be set to `"module"`.
10
+ * - If the format is `cjs`, the `type` will be set to `"commonjs"`.
11
+ *
12
+ * The function will ensure that the output directory exists, and if it doesn't,
13
+ * it will create the necessary directories before writing the `package.json` file.
14
+ *
15
+ * @param {ConfigurationInterface} config - The build configuration object containing
16
+ * esbuild-related settings, such as the output directory (`outdir`) and format (`format`).
17
+ *
18
+ * - `config.esbuild.outdir`: The output directory where the `package.json` will be created.
19
+ * - `config.esbuild.format`: The module format, either `'esm'` or `'cjs'`, that determines the `type` field.
20
+ *
21
+ * @throws Will throw an error if there is a problem creating the directory or writing the file.
22
+ *
23
+ * Example usage:
24
+ *
25
+ * ```ts
26
+ * const config = {
27
+ * esbuild: {
28
+ * outdir: 'dist',
29
+ * format: 'esm'
30
+ * }
31
+ * };
32
+ * packageTypeComponent(config);
33
+ * // This will create 'dist/package.json' with the content: {"type": "module"}
34
+ * ```
35
+ */
36
+ export declare function packageTypeComponent(config: ConfigurationInterface): void;
@@ -301,4 +301,51 @@ interface ExportedConfigurationInterface extends ConfigurationInterface {
301
301
  * objects where only a subset of properties need to be specified.
302
302
  */
303
303
  export type xBuildConfig = PartialDeep<ExportedConfigurationInterface>;
304
+ /**
305
+ * Represents a partially deep configuration type based on the `ConfigurationInterface`.
306
+ *
307
+ * This type is used to define configurations that may have some properties
308
+ * missing or undefined. It leverages the `PartialDeep` utility type to allow
309
+ * for flexibility in configuration management.
310
+ */
311
+ export type PartialDeepConfigurationsType = PartialDeep<ConfigurationInterface>;
312
+ /**
313
+ * Defines the possible types for configurations.
314
+ *
315
+ * This type can either be a single instance of `PartialDeepConfigurationsType`
316
+ * or an array of such instances. This flexibility allows for configurations
317
+ * to be specified as a single object or as multiple objects, enabling
318
+ * support for various build setups.
319
+ *
320
+ * @example
321
+ * // A single configuration object
322
+ * const config: ConfigurationsType = {
323
+ * esbuild: {
324
+ * bundle: true,
325
+ * outdir: 'dist'
326
+ * }
327
+ * };
328
+ *
329
+ * @example
330
+ * ```ts
331
+ * // An array of configuration objects
332
+ * const configs: ConfigurationsType = [
333
+ * {
334
+ * esbuild: {
335
+ * bundle: true,
336
+ * outdir: 'dist/esm'
337
+ * }
338
+ * },
339
+ * {
340
+ * esbuild: {
341
+ * bundle: false,
342
+ * outdir: 'dist/cjs',
343
+ * declaration: false,
344
+ * noTypeChecker: true
345
+ * }
346
+ * }
347
+ * ];
348
+ * ```
349
+ */
350
+ export type ConfigurationsType = PartialDeepConfigurationsType | Array<PartialDeepConfigurationsType>;
304
351
  export {};
@@ -1,39 +1,21 @@
1
1
  /**
2
2
  * Import will remove at compile time
3
3
  */
4
- import type { StackEntryInterface } from '@remotex-labs/xmap';
5
- import { SourceService } from '@remotex-labs/xmap';
6
4
  /**
7
- * Constants
5
+ * Imports
8
6
  */
9
- export declare const dirname: string;
10
- export declare const sourceMapData: Buffer;
11
- export declare const defaultSourceService: SourceService;
7
+ import type { SourceService } from '@remotex-labs/xmap';
8
+ import type { ErrorType } from "./interfaces/stack.interface";
12
9
  /**
13
10
  * A base class for custom errors with enhanced stack trace formatting and source code information.
14
11
  *
15
12
  * The `BaseError` class extends the native `Error` class, adding functionality to format the error stack
16
13
  * trace and include details from a source map service. This is useful for debugging errors in compiled
17
14
  * or transpiled code by providing clearer information about the source of the error.
18
- *
19
- * @augments Error
20
15
  */
21
16
  export declare abstract class BaseError extends Error {
22
- protected sourceMap: SourceService;
23
- /**
24
- * An array to hold the formatted lines of the stack trace.
25
- *
26
- * This property contains the enhanced stack trace of the error, formatted using source map information,
27
- * if available.
28
- */
29
- stackArray: Array<string>;
30
- /**
31
- * The code block related to the error.
32
- *
33
- * This property holds a string representing the code snippet where the error occurred. It is formatted
34
- * for better readability and is set based on the source map details.
35
- */
36
- blockCode: string | null;
17
+ readonly sourceMap?: SourceService | undefined;
18
+ callStacks: Array<NodeJS.CallSite>;
37
19
  /**
38
20
  * Creates a new instance of `BaseError`.
39
21
  *
@@ -46,16 +28,7 @@ export declare abstract class BaseError extends Error {
46
28
  * @param sourceMap - (Optional) The `SourceService` instance used to format and resolve the stack trace.
47
29
  * If not provided, the default source map service (`defaultSourceService`) is used.
48
30
  */
49
- protected constructor(message: string, sourceMap?: SourceService);
50
- /**
51
- * Converts the error object to a formatted string.
52
- *
53
- * This method provides a string representation of the `BaseError` instance, including the error name,
54
- * message, and an enhanced stack trace. It also includes the code block related to the error if available.
55
- *
56
- * @returns A string that represents the formatted error message and stack trace.
57
- */
58
- toString(): string;
31
+ protected constructor(message: string, sourceMap?: SourceService | undefined);
59
32
  /**
60
33
  * Reformats the error stack trace using source map information.
61
34
  *
@@ -63,39 +36,8 @@ export declare abstract class BaseError extends Error {
63
36
  * back to its original position in the source file using the provided source map service.
64
37
  * If the source map information is not available, it returns the original stack trace.
65
38
  *
66
- * @param stack - The original stack trace of the error.
39
+ * @param error - The original error with stack trace of the error.
67
40
  * @returns The reformatted stack trace or the original stack trace if no mapping is available.
68
41
  */
69
- protected reformatStack(stack: string | undefined): string | undefined;
70
- /**
71
- * Gets the formatted stack entries based on the provided source map service.
72
- *
73
- * This method processes each stack trace entry, resolves its original position using
74
- * the source map service, and formats it accordingly. If the source position is available,
75
- * the corresponding code block is also highlighted.
76
- *
77
- * @param stackEntries - An array of stack trace entries.
78
- * @param sourceMap - The source map service used to resolve the stack trace to original source positions.
79
- * @returns An array of formatted stack trace entries.
80
- */
81
- protected getFormattedStackEntries(stackEntries: StackEntryInterface[], sourceMap: SourceService): string[];
82
- /**
83
- * Updates the block code property if it has not been set yet.
84
- *
85
- * This method highlights the code snippet associated with the given position and formats it
86
- * for better readability. The `blockCode` property is only updated once with the highlighted code.
87
- *
88
- * @param position - The position object containing the code snippet to be highlighted.
89
- */
90
- private updateBlockCodeIfNecessary;
91
- /**
92
- * Constructs the file path with the source root and line number information.
93
- *
94
- * This method ensures that the file path is correctly formatted with the source root (if available)
95
- * and appends the line number for reference.
96
- *
97
- * @param position - The position object containing source file path and source root.
98
- * @returns The formatted file path with line number.
99
- */
100
- private getFilePathWithSourceRoot;
42
+ protected reformatStack(error: ErrorType): string;
101
43
  }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * A custom error type that extends the native `Error` object by adding a `callStacks` property.
3
+ * This property contains an array of `NodeJS.CallSite` objects, representing the call stack details.
4
+ */
5
+ import type { BaseError } from "../base.error";
6
+ /**
7
+ * Represents an enhanced error type that extends the built-in Error object.
8
+ * This type adds an optional property to store call stack information.
9
+ *
10
+ * @type ErrorType
11
+ *
12
+ * @extends {Error}
13
+ *
14
+ * @property callStacks - An optional array of call sites
15
+ * captured when the error was created. This can provide additional context
16
+ * regarding the call stack at the time of the error, useful for debugging.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * const myError: ErrorType = new Error("Something went wrong!");
21
+ * myError.callStacks = getCallStack(); // Assuming getCallStack captures call sites.
22
+ * console.error(myError);
23
+ * ```
24
+ */
25
+ export type ErrorType = Error & {
26
+ callStacks?: Array<NodeJS.CallSite>;
27
+ };
28
+ /**
29
+ * Represents the state of a stack trace, containing information about the error, associated code, and formatted error message.
30
+ *
31
+ * @interface StackTraceStateInterface
32
+ * @property error - The error object with attached `callStacks`.
33
+ * @property blockCode - The block of code (if any) related to the error, or `null` if unavailable.
34
+ * @property formattedError - A formatted string representing the error details.
35
+ */
36
+ export interface StackTraceStateInterface {
37
+ error: ErrorType & BaseError;
38
+ blockCode: null | string;
39
+ formattedError: string;
40
+ }
41
+ /**
42
+ * Represents detailed information about a specific frame in the call stack.
43
+ *
44
+ * @interface FrameDetailsInterface
45
+ * @property line - The line number where the frame occurred.
46
+ * @property column - The column number where the frame occurred.
47
+ * @property source - The source file path where the frame occurred.
48
+ * @property functionName - The name of the function being executed at this frame, or an empty string if not available.
49
+ */
50
+ export interface FrameDetailsInterface {
51
+ line: number;
52
+ column: number;
53
+ source: string;
54
+ functionName: string;
55
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Import will remove at compile time
3
+ */
4
+ import type { BaseError } from ".//base.error";
5
+ import type { ErrorType } from "./interfaces/stack.interface";
6
+ import { SourceService } from '@remotex-labs/xmap';
7
+ export declare const xBuildLazy: {
8
+ readonly service: SourceService;
9
+ };
10
+ /**
11
+ * Prepares the error stack trace for display.
12
+ *
13
+ * This function overrides the default stack trace preparation to provide a custom format,
14
+ * including enhanced stack trace information and error details.
15
+ *
16
+ * @param error - The error object (Error or BaseError).
17
+ * @param stackEntries - The array of stack entries from the call stack.
18
+ * @returns The formatted stack trace as a string.
19
+ */
20
+ export declare function formatStackTrace(error: ErrorType & BaseError, stackEntries: Array<NodeJS.CallSite>): string;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Handles uncaught exceptions in the Node.js process.
3
+ *
4
+ * This handler is triggered when an error is thrown that is not caught by any try-catch blocks.
5
+ * It captures such exceptions and logs them to the console. If the exception is an instance of `Error`,
6
+ * its string representation is logged. Otherwise, the raw error object is logged.
7
+ *
8
+ * This setup helps in debugging by ensuring that all uncaught exceptions are logged, providing visibility
9
+ * into errors that might otherwise go unnoticed.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * process.on('uncaughtException', (error) => {
14
+ * if (error instanceof Error) {
15
+ * console.error(error.toString());
16
+ * } else {
17
+ * console.error(error);
18
+ * }
19
+ * });
20
+ * ```
21
+ *
22
+ * @throws Will log uncaught exceptions to the console.
23
+ * Custom handling logic should be added if additional error handling or logging is required.
24
+ */
25
+ export {};
@@ -2,6 +2,7 @@
2
2
  * Import will remove at compile time
3
3
  */
4
4
  import type { SourceService } from '@remotex-labs/xmap';
5
+ import type { ErrorType } from "./interfaces/stack.interface";
5
6
  /**
6
7
  * Imports
7
8
  */
@@ -31,6 +32,10 @@ export declare class VMRuntimeError extends BaseError {
31
32
  * The original error thrown during the VM execution.
32
33
  */
33
34
  originalError: Error;
35
+ /**
36
+ * Original error stack
37
+ */
38
+ originalErrorStack: string | undefined;
34
39
  /**
35
40
  * Creates a new VMRuntimeError instance.
36
41
  *
@@ -52,5 +57,5 @@ export declare class VMRuntimeError extends BaseError {
52
57
  * }
53
58
  * ```
54
59
  */
55
- constructor(originalError: Error, sourceMap?: SourceService);
60
+ constructor(originalError: ErrorType, sourceMap?: SourceService);
56
61
  }
@@ -12,6 +12,10 @@ import { BaseError } from ".//base.error";
12
12
  * @augments BaseError
13
13
  */
14
14
  export declare class xBuildError extends BaseError {
15
+ /**
16
+ * Original error stack
17
+ */
18
+ originalErrorStack: string | undefined;
15
19
  /**
16
20
  * Creates an instance of `xBuildError`.
17
21
  *
@@ -21,8 +25,4 @@ export declare class xBuildError extends BaseError {
21
25
  * that customize the error's behavior.
22
26
  */
23
27
  constructor(message: string, options?: ErrorOptions);
24
- /**
25
- * Set external stack
26
- */
27
- setStack(stack: string): void;
28
28
  }
package/dist/index.d.ts CHANGED
@@ -4,3 +4,8 @@
4
4
  */
5
5
  export type * from "./configuration/interfaces/configuration.interface";
6
6
  export type { BuildResult, OnLoadArgs, OnLoadResult, OnResolveArgs, OnResolveResult, PluginBuild } from 'esbuild';
7
+ /**
8
+ * Imports
9
+ */
10
+ import "./errors/stack.error";
11
+ import "./errors/uncaught.error";