@remotex-labs/xbuild 2.1.1 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bash.d.ts CHANGED
@@ -26,14 +26,13 @@ import type { SourceService } from '@remotex-labs/xmap';
26
26
  import type { ParsedStackTraceInterface, StackFrameInterface } from '@remotex-labs/xmap/parser.component';
27
27
  import { type PositionWithCodeInterface } from '@remotex-labs/xmap';
28
28
  import { type SourceOptionsInterface } from '@remotex-labs/xmap/formatter.component';
29
- import type { Message } from 'esbuild';
30
29
  import type { Plugin } from 'esbuild';
31
30
  import type { BuildOptions, BuildResult, Metafile } from 'esbuild';
32
31
  import type { OnLoadResult } from 'esbuild';
33
32
  import type { VariableStatement, ExpressionStatement } from 'typescript';
34
33
  import type { SourceFile } from 'typescript';
35
34
  import type { VariableDeclaration, CallExpression } from 'typescript';
36
- import type { SourceFile, Node, NodeArray, Expression } from 'typescript';
35
+ import type { SourceFile, Node } from 'typescript';
37
36
  import type { CallExpression, VariableDeclaration, Node } from 'typescript';
38
37
  import type { NodeArray, Expression, VariableStatement } from 'typescript';
39
38
  import type { Node, FunctionDeclaration, ArrowFunction, FunctionExpression } from 'typescript';
@@ -427,7 +426,7 @@ declare global {
427
426
  *
428
427
  * @since 2.0.0
429
428
  */
430
- function $$inline(callback: unknown): string | undefined;
429
+ function $$inline<T>(callback: () => T): string | undefined;
431
430
  /**
432
431
  * Pre-configuration CLI arguments snapshot (bootstrap argv).
433
432
  *
@@ -9571,7 +9570,7 @@ declare class esBuildError extends xBuildBaseError {
9571
9570
  *
9572
9571
  * @since 2.0.0
9573
9572
  */
9574
- constructor(message: Message);
9573
+ constructor(message: PartialMessage);
9575
9574
  }
9576
9575
 
9577
9576
  /**
@@ -12325,6 +12324,24 @@ declare function extractEntryPoints(baseDir: string, entryPoints: BuildOptions['
12325
12324
  /**
12326
12325
  * Import will remove at compile time
12327
12326
  */
12327
+ /**
12328
+ * Imports
12329
+ */
12330
+ /**
12331
+ * Checks whether a given AST node’s source text contains any supported macro function name.
12332
+ *
12333
+ * @param node - AST node to inspect
12334
+ * @param sourceFile - Optional source file used by TypeScript to compute node text
12335
+ *
12336
+ * @returns `true` if the node text contains at least one macro identifier; otherwise `false`.
12337
+ *
12338
+ * @remarks
12339
+ * This is a fast, text-based pre-check used to avoid deeper macro parsing work when a node
12340
+ * clearly cannot contain macro calls.
12341
+ *
12342
+ * @since 2.0.0
12343
+ */
12344
+ declare function nodeContainsMacro(node: ts.Node, sourceFile?: ts.SourceFile): boolean;
12328
12345
  /**
12329
12346
  * Processes variable statements containing macro calls and adds replacements to the replacement set.
12330
12347
  *
@@ -12394,7 +12411,7 @@ declare function extractEntryPoints(baseDir: string, entryPoints: BuildOptions['
12394
12411
  *
12395
12412
  * @since 2.0.0
12396
12413
  */
12397
- declare function isVariableStatement(node: VariableStatement, replacements: Set<SubstInterface>, state: StateInterface): Promise<void>;
12414
+ declare function isVariableStatement(node: VariableStatement, replacements: Set<SubstInterface>, state: StateInterface): Promise<boolean>;
12398
12415
  /**
12399
12416
  * Processes standalone macro call expressions and adds replacements to the replacement set.
12400
12417
  *
@@ -12449,7 +12466,7 @@ declare function isVariableStatement(node: VariableStatement, replacements: Set<
12449
12466
  *
12450
12467
  * @since 2.0.0
12451
12468
  */
12452
- declare function isCallExpression(node: ExpressionStatement, replacements: Set<SubstInterface>, state: StateInterface): Promise<void>;
12469
+ declare function isCallExpression(node: ExpressionStatement, replacements: Set<SubstInterface>, state: StateInterface): Promise<boolean>;
12453
12470
  /**
12454
12471
  * Recursively traverses the AST to find and transform all macro occurrences in the source file.
12455
12472
  *
@@ -12660,7 +12677,7 @@ declare function transformerDirective(variant: VariantService, context: LoadCont
12660
12677
  * ```
12661
12678
  *
12662
12679
  * @see {@link analyzeMacroMetadata} for the function that generates this metadata
12663
- * @see {@link MacrosStaeInterface} for the stage interface that contains this metadata
12680
+ * @see {@link MacrosStateInterface} for the stage interface that contains this metadata
12664
12681
  *
12665
12682
  * @since 2.0.0
12666
12683
  */
@@ -12715,6 +12732,54 @@ interface MacrosMetadataInterface {
12715
12732
  */
12716
12733
  disabledMacroNames: Set<string>;
12717
12734
  }
12735
+ /**
12736
+ * Describes a single text replacement produced by macro analysis/transformation.
12737
+ *
12738
+ * @remarks
12739
+ * This is a small helper contract used to capture:
12740
+ * - the original source fragment ({@link MacroReplacementInterface.source}), and
12741
+ * - the text that should replace it ({@link MacroReplacementInterface.replacement}).
12742
+ *
12743
+ * It is useful for debugging, reporting, and applying deterministic transformations.
12744
+ *
12745
+ * @example Recording a replacement
12746
+ * ```ts
12747
+ * const r: MacroReplacementInterface = {
12748
+ * source: "$$ifdef('DEBUG', () => log())",
12749
+ * replacement: "undefined"
12750
+ * };
12751
+ * ```
12752
+ *
12753
+ * @since 2.0.0
12754
+ */
12755
+ interface MacroReplacementInterface {
12756
+ /**
12757
+ * Original source text that should be replaced.
12758
+ *
12759
+ * @remarks
12760
+ * Typically, this is an exact substring extracted from a file (e.g., a macro call,
12761
+ * a macro variable initializer, or any other segment discovered during analysis).
12762
+ *
12763
+ * Consumers can use this to:
12764
+ * - build diagnostics ("what was replaced?")
12765
+ * - provide debug output / reports
12766
+ * - verify transformations are deterministic
12767
+ *
12768
+ * @since 2.0.0
12769
+ */
12770
+ source: string;
12771
+ /**
12772
+ * Replacement text that should be substituted in place of {@link source}.
12773
+ *
12774
+ * @remarks
12775
+ * This is the final text representation that should appear in the transformed output.
12776
+ * For disabled macros this is often `'undefined'`, but it may also be an inlined constant,
12777
+ * rewritten function body, or other generated code.
12778
+ *
12779
+ * @since 2.0.0
12780
+ */
12781
+ replacement: string;
12782
+ }
12718
12783
  /**
12719
12784
  * Extended lifecycle stage interface with macro-specific metadata.
12720
12785
  *
@@ -12776,7 +12841,7 @@ interface MacrosMetadataInterface {
12776
12841
  *
12777
12842
  * @since 2.0.0
12778
12843
  */
12779
- interface MacrosStaeInterface extends LifecycleStageInterface {
12844
+ interface MacrosStateInterface extends LifecycleStageInterface {
12780
12845
  /**
12781
12846
  * Macro analysis metadata for the current build.
12782
12847
  *
@@ -12791,6 +12856,29 @@ interface MacrosStaeInterface extends LifecycleStageInterface {
12791
12856
  * @since 2.0.0
12792
12857
  */
12793
12858
  defineMetadata: MacrosMetadataInterface;
12859
+ /**
12860
+ * Optional list of macro-driven source replacements recorded during transformation.
12861
+ *
12862
+ * @remarks
12863
+ * When present, this can be used for:
12864
+ * - debugging (“what exactly changed?”),
12865
+ * - producing transformation reports, or
12866
+ * - testing/verifying deterministic output.
12867
+ *
12868
+ * Each entry describes the original fragment and its replacement via
12869
+ * {@link MacroReplacementInterface}.
12870
+ *
12871
+ * @example
12872
+ * ```ts
12873
+ * stage.replacementInfo = [
12874
+ * { source: "$$inline(() => 1 + 1)", replacement: "2" },
12875
+ * { source: "$$debug()", replacement: "undefined" }
12876
+ * ];
12877
+ * ```
12878
+ *
12879
+ * @since 2.0.0
12880
+ */
12881
+ replacementInfo?: Array<MacroReplacementInterface>;
12794
12882
  }
12795
12883
 
12796
12884
  /**
@@ -12902,7 +12990,7 @@ type DefinesType = Record<string, unknown>;
12902
12990
  *
12903
12991
  * @see {@link transformerDirective} for state creation
12904
12992
  * @see {@link astProcess} for state usage during transformation
12905
- * @see {@link MacrosStaeInterface} for stage metadata structure
12993
+ * @see {@link MacrosStateInterface} for stage metadata structure
12906
12994
  *
12907
12995
  * @since 2.0.0
12908
12996
  */
@@ -12918,12 +13006,12 @@ interface StateInterface {
12918
13006
  * This metadata is used to optimize transformation by skipping files without
12919
13007
  * macros and replacing disabled macro references with `undefined`.
12920
13008
  *
12921
- * @see {@link MacrosStaeInterface} for metadata structure
13009
+ * @see {@link MacrosStateInterface} for metadata structure
12922
13010
  * @see {@link analyzeMacroMetadata} for metadata generation
12923
13011
  *
12924
13012
  * @since 2.0.0
12925
13013
  */
12926
- stage: MacrosStaeInterface;
13014
+ stage: MacrosStateInterface;
12927
13015
  /**
12928
13016
  * Array of error messages collected during transformation.
12929
13017
  *
@@ -13208,6 +13296,8 @@ declare function transformToFunction(fnName: string, node: Node, sourceFile: Sou
13208
13296
  *
13209
13297
  * @param node - The AST node to transform
13210
13298
  * @param sourceFile - The source file containing the node
13299
+ * @param prefix - The prefix to prepend before the IIFE; defaults to `''`
13300
+ * @param suffix - The suffix to append after the IIFE; defaults to `'();'`
13211
13301
  *
13212
13302
  * @returns A string containing the IIFE expression
13213
13303
  *
@@ -13218,10 +13308,15 @@ declare function transformToFunction(fnName: string, node: Node, sourceFile: Sou
13218
13308
  * **For function-like nodes** (arrow functions and function expressions):
13219
13309
  * - Wraps directly: `(function)()` or `(() => value)()`
13220
13310
  * - Preserves the function as-is
13311
+ * - Note: `prefix` and `suffix` are not applied to function-like nodes
13221
13312
  *
13222
13313
  * **For other node types** (expressions, statements):
13223
13314
  * - Wraps in an arrow function IIFE with explicit return
13224
13315
  * - Ensures the value is returned for use in expressions
13316
+ * - Applies `prefix` before and `suffix` after the IIFE
13317
+ *
13318
+ * The `prefix` and `suffix` parameters allow customization of the IIFE syntax, useful when
13319
+ * the IIFE needs additional context, chaining, or specific wrapping.
13225
13320
  *
13226
13321
  * Used when conditional macros appear in expression contexts where a function
13227
13322
  * declaration is not valid syntax.
@@ -13247,11 +13342,18 @@ declare function transformToFunction(fnName: string, node: Node, sourceFile: Sou
13247
13342
  * // '(() => { return 1 + 1; })()'
13248
13343
  * ```
13249
13344
  *
13345
+ * @example Custom prefix and suffix
13346
+ * ```ts
13347
+ * const node = parseExpression('getValue()');
13348
+ * const result = transformToIIFE(node, sourceFile, 'await ', '.catch(handleError)');
13349
+ * // 'await (() => { return getValue(); })().catch(handleError)'
13350
+ * ```
13351
+ *
13250
13352
  * @see {@link astDefineCallExpression} for the calling context
13251
13353
  *
13252
13354
  * @since 2.0.0
13253
13355
  */
13254
- declare function transformToIIFE(node: Node, sourceFile: SourceFile): string;
13356
+ declare function transformToIIFE(node: Node, sourceFile: SourceFile, prefix?: string, suffix?: string): string;
13255
13357
  /**
13256
13358
  * Transforms a conditional macro variable declaration into a function or returns an empty string if excluded.
13257
13359
  *
@@ -13316,64 +13418,76 @@ declare function transformToIIFE(node: Node, sourceFile: SourceFile): string;
13316
13418
  */
13317
13419
  declare function astDefineVariable(decl: VariableDeclaration, init: CallExpression, hasExport: boolean, state: StateInterface): string | false;
13318
13420
  /**
13319
- * Transforms a conditional macro call expression into an IIFE or returns empty string if excluded.
13421
+ * Transforms a conditional macro call expression into a constant assignment with an IIFE or returns empty string if excluded.
13320
13422
  *
13321
- * @param args - The arguments passed to the macro call
13322
- * @param fnName - The macro function name (`'$$ifdef'` or `'$$ifndef'`)
13423
+ * @param decl - The variable declaration node containing the macro
13424
+ * @param init - The call expression node representing the macro call
13425
+ * @param hasExport - Whether the variable declaration has an `export` modifier
13323
13426
  * @param state - The macro transformation state containing definitions and source file
13427
+ * @param outerSuffix - Optional suffix to append after the IIFE invocation
13324
13428
  *
13325
- * @returns The transformed IIFE string, empty string if excluded, or `false` if invalid
13429
+ * @returns The transformed constant assignment string, empty string if excluded, or `false` if invalid
13326
13430
  *
13327
13431
  * @remarks
13328
- * This function processes standalone conditional macro calls that appear in expression contexts:
13432
+ * This function processes conditional macro call expressions that are assigned to constants:
13329
13433
  * ```ts
13330
- * console.log($$ifdef('DEBUG', () => "debug mode"));
13331
- * const value = $$ifndef('PRODUCTION', () => devValue);
13434
+ * const $$value = $$ifdef('DEBUG', () => "debug mode");
13435
+ * export const $$config = $$ifndef('PRODUCTION', () => devConfig);
13332
13436
  * ```
13333
13437
  *
13334
- * Unlike {@link astDefineVariable}, this handles macros that are not part of variable
13335
- * declarations but are used directly as expressions.
13438
+ * Unlike {@link astDefineVariable}, which transforms macros into function declarations,
13439
+ * this handles macros that should remain as constant assignments with IIFE values.
13336
13440
  *
13337
13441
  * The transformation process:
13338
13442
  * 1. Validates that the first argument is a string literal (the definition name)
13339
- * 2. Checks if the definition condition is met using {@link isDefinitionMet}
13340
- * 3. If included: transforms the callback into an IIFE using {@link transformToIIFE}
13341
- * 4. If excluded: returns an empty string (macro evaluates to undefined)
13342
- * 5. If invalid: returns `false` (non-string definition argument)
13443
+ * 2. Extracts the macro function name (`$$ifdef` or `$$ifndef`)
13444
+ * 3. Checks if the definition condition is met using {@link isDefinitionMet}
13445
+ * 4. If included: transforms the callback into a constant assignment with IIFE using {@link transformToIIFE}
13446
+ * 5. If excluded: returns an empty string (macro is stripped from output)
13447
+ * 6. If invalid: returns `false` (non-string definition argument)
13448
+ *
13449
+ * The variable name from the declaration becomes the constant name in the output,
13450
+ * and the `hasExport` parameter controls whether the constant is exported.
13343
13451
  *
13344
13452
  * @example Included expression (DEBUG=true)
13345
13453
  * ```ts
13346
- * // Source: console.log($$ifdef('DEBUG', () => "debugging"));
13454
+ * // Source: const $$debugMsg = $$ifdef('DEBUG', () => "debugging");
13347
13455
  * // With: { DEBUG: true }
13348
- * const result = astDefineCallExpression(args, '$$ifdef', state);
13349
- * // '(() => "debugging")()'
13350
- * // Result: console.log((() => "debugging")());
13456
+ * const result = astDefineCallExpression(decl, init, false, state);
13457
+ * // 'const $$debugMsg = (() => { return "debugging"; })();'
13351
13458
  * ```
13352
13459
  *
13353
13460
  * @example Excluded expression (DEBUG=false)
13354
13461
  * ```ts
13355
- * // Source: console.log($$ifdef('DEBUG', () => "debugging"));
13462
+ * // Source: const $$debugMsg = $$ifdef('DEBUG', () => "debugging");
13356
13463
  * // With: { DEBUG: false }
13357
- * const result = astDefineCallExpression(args, '$$ifdef', state);
13464
+ * const result = astDefineCallExpression(decl, init, false, state);
13358
13465
  * // ''
13359
- * // Result: console.log();
13360
13466
  * ```
13361
13467
  *
13362
- * @example Using `$$ifndef`
13468
+ * @example Exported constant
13363
13469
  * ```ts
13364
- * // Source: const url = $$ifndef('PRODUCTION', () => 'http://localhost');
13470
+ * // Source: export const $$apiUrl = $$ifndef('PRODUCTION', () => 'http://localhost');
13365
13471
  * // With: { PRODUCTION: false }
13366
- * const result = astDefineCallExpression(args, '$$ifndef', state);
13367
- * // '(() => "http://localhost")()'
13472
+ * const result = astDefineCallExpression(decl, init, true, state);
13473
+ * // 'export const $$apiUrl = (() => { return "http://localhost"; })();'
13474
+ * ```
13475
+ *
13476
+ * @example With custom suffix
13477
+ * ```ts
13478
+ * // Source: const $$data = $$ifdef('FEATURE', () => fetchData());
13479
+ * // With: { FEATURE: true }
13480
+ * const result = astDefineCallExpression(decl, init, false, state, '.then(process)');
13481
+ * // 'const $$data = (() => { return fetchData(); })().then(process)'
13368
13482
  * ```
13369
13483
  *
13370
13484
  * @see {@link isDefinitionMet} for condition evaluation
13371
13485
  * @see {@link transformToIIFE} for transformation logic
13372
- * @see {@link astDefineVariable} for variable declaration handling
13486
+ * @see {@link astDefineVariable} for function declaration handling
13373
13487
  *
13374
13488
  * @since 2.0.0
13375
13489
  */
13376
- declare function astDefineCallExpression(args: NodeArray<Expression>, fnName: string, state: StateInterface): string | false;
13490
+ declare function astDefineCallExpression(init: CallExpression, state: StateInterface, decl?: VariableDeclaration, hasExport?: boolean, outerSuffix?: string): string | false;
13377
13491
 
13378
13492
  /**
13379
13493
  * Import will remove at compile time
@@ -14101,38 +14215,6 @@ declare function sandboxExecute(code: string, sandbox?: Context, options?: Scrip
14101
14215
  * @param index - The starting index in the text where the match was found
14102
14216
  * @returns A partial {@link Location} object containing file, line, and column information
14103
14217
  *
14104
- * @remarks
14105
- * Line numbers are 1-based. The column is calculated relative to the match index.
14106
- * This function is primarily used for generating accurate diagnostic messages.
14107
- *
14108
- * @example
14109
- * ```ts
14110
- * const sourceCode = 'import x from "y";\nconst $$myMacro = $$ifdef("DEBUG");';
14111
- * const macroIndex = sourceCode.indexOf('$$myMacro');
14112
- *
14113
- * const location = getLineAndColumn(sourceCode, '$$myMacro', 'src/app.ts', macroIndex);
14114
- * console.log(location);
14115
- * // Output: {
14116
- * // file: 'src/app.ts',
14117
- * // line: 2,
14118
- * // column: 6
14119
- * // }
14120
- * ```
14121
- *
14122
- * @example Multi-line file positioning
14123
- * ```ts
14124
- * const code = [
14125
- * 'const x = 1;',
14126
- * 'const y = 2;',
14127
- * 'const $$feature = $$ifdef("FEATURE_X");'
14128
- * ].join('\n');
14129
- *
14130
- * const index = code.indexOf('$$feature');
14131
- * const loc = getLineAndColumn(code, '$$feature', 'config.ts', index);
14132
- * // loc.line === 3
14133
- * ```
14134
- *
14135
- * @see {@link Location}
14136
14218
  * @since 2.0.0
14137
14219
  */
14138
14220
  declare function getLineAndColumn(text: string, name: string, file: string, index: number): Partial<Location>;
@@ -15679,7 +15761,7 @@ declare function logBuildStart({ variantName }: BuildContextInterface): void;
15679
15761
  *
15680
15762
  * @since 2.0.0
15681
15763
  */
15682
- declare function logBuildEnd({ variantName, duration, buildResult }: ResultContextInterface): void;
15764
+ declare function logBuildEnd({ variantName, duration, buildResult, stage }: ResultContextInterface): void;
15683
15765
 
15684
15766
  /**
15685
15767
  * Import will remove at compile time