@savvy-web/rslib-builder 0.2.2 → 0.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.
Files changed (3) hide show
  1. package/index.d.ts +466 -412
  2. package/index.js +48 -204
  3. package/package.json +1 -5
package/index.d.ts CHANGED
@@ -84,10 +84,12 @@ export declare interface ApiModelOptions {
84
84
  *
85
85
  * @example
86
86
  * ```typescript
87
- * apiModel: {
87
+ * import type { ApiModelOptions } from '@savvy-web/rslib-builder';
88
+ *
89
+ * const apiModel: ApiModelOptions = {
88
90
  * enabled: true,
89
- * localPaths: ["../docs-site/lib/packages/my-package"],
90
- * }
91
+ * localPaths: ['../docs-site/lib/packages/my-package'],
92
+ * };
91
93
  * ```
92
94
  */
93
95
  localPaths?: string[];
@@ -102,12 +104,14 @@ export declare interface ApiModelOptions {
102
104
  *
103
105
  * @example
104
106
  * ```typescript
105
- * apiModel: {
107
+ * import type { ApiModelOptions } from '@savvy-web/rslib-builder';
108
+ *
109
+ * const apiModel: ApiModelOptions = {
106
110
  * enabled: true,
107
111
  * tsdoc: {
108
- * tagDefinitions: [{ tagName: "@error", syntaxKind: "inline" }]
109
- * }
110
- * }
112
+ * tagDefinitions: [{ tagName: '@error', syntaxKind: 'inline' }],
113
+ * },
114
+ * };
111
115
  * ```
112
116
  */
113
117
  tsdoc?: TsDocOptions;
@@ -119,30 +123,109 @@ export declare interface ApiModelOptions {
119
123
  }
120
124
 
121
125
  /**
122
- * Plugin to read package.json and configure entry points based on exports.
126
+ * Plugin to automatically detect and configure entry points from package.json exports.
127
+ *
128
+ * @remarks
129
+ * This plugin reads your package.json exports field and automatically configures
130
+ * RSlib entry points, eliminating the need to manually specify entries in your config.
131
+ *
132
+ * ## Features
133
+ *
134
+ * - Automatically extracts entry points from package.json `exports` field
135
+ * - Supports both string and object export values
136
+ * - Handles bin field entries for CLI tools
137
+ * - Exposes entrypoints map for other plugins to consume
138
+ *
139
+ * ## How It Works
140
+ *
141
+ * 1. Reads package.json from the project root
142
+ * 2. Extracts entry points from the `exports` field
143
+ * 3. Configures RSlib with the discovered entries
144
+ * 4. Exposes the entrypoints map via `api.useExposed("entrypoints")`
145
+ *
146
+ * @param options - Plugin configuration options
147
+ *
148
+ * @example
149
+ * Basic usage (entries detected from package.json):
150
+ * ```typescript
151
+ * import { AutoEntryPlugin } from '@savvy-web/rslib-builder';
152
+ *
153
+ * export default {
154
+ * plugins: [AutoEntryPlugin()],
155
+ * };
156
+ * ```
157
+ *
158
+ * @example
159
+ * With nested directory output:
160
+ * ```typescript
161
+ * import { AutoEntryPlugin } from '@savvy-web/rslib-builder';
162
+ *
163
+ * export default {
164
+ * plugins: [
165
+ * AutoEntryPlugin({ exportsAsIndexes: true }),
166
+ * ],
167
+ * };
168
+ * ```
123
169
  *
124
- * @param options - Plugin configuration options with properties:
125
- * - `exportsAsIndexes`: When true, export paths create index files in nested directories
126
170
  * @public
127
171
  */
128
- export declare const AutoEntryPlugin: (options?: {
129
- exportsAsIndexes?: boolean | undefined;
130
- } | undefined) => RsbuildPlugin;
172
+ export declare const AutoEntryPlugin: (options?: AutoEntryPluginOptions | undefined) => RsbuildPlugin;
131
173
 
132
174
  /**
175
+ * Options for the AutoEntryPlugin.
133
176
  * @public
134
177
  */
135
- export declare type BuildTarget = "dev" | "npm";
178
+ export declare interface AutoEntryPluginOptions {
179
+ /**
180
+ * When enabled, export paths create `index.js` files in nested directories
181
+ * instead of using the export name as the filename.
182
+ *
183
+ * @remarks
184
+ * This is useful when you want cleaner import paths that don't require
185
+ * specifying a filename, relying on Node's directory index resolution.
186
+ *
187
+ * @example
188
+ * With `exportsAsIndexes: true` and this package.json:
189
+ * ```json
190
+ * {
191
+ * "exports": {
192
+ * ".": "./src/index.ts",
193
+ * "./utils": "./src/utils/index.ts"
194
+ * }
195
+ * }
196
+ * ```
197
+ *
198
+ * Output structure:
199
+ * ```
200
+ * dist/
201
+ * index.js
202
+ * utils/
203
+ * index.js
204
+ * ```
205
+ *
206
+ * @defaultValue false
207
+ */
208
+ exportsAsIndexes?: boolean;
209
+ }
136
210
 
137
211
  /**
138
- * Recursively collects all .d.ts and .d.ts.map files from a directory.
212
+ * Build target environment for library output.
213
+ *
214
+ * @remarks
215
+ * Each target produces different output optimizations:
216
+ * - `"dev"`: Development build with source maps for debugging
217
+ * - `"npm"`: Production build optimized for npm publishing
218
+ *
219
+ * @example
220
+ * Specifying targets via CLI:
221
+ * ```bash
222
+ * rslib build --env-mode dev
223
+ * rslib build --env-mode npm
224
+ * ```
139
225
  *
140
226
  * @public
141
227
  */
142
- export declare function collectDtsFiles(dir: string, baseDir?: string): Promise<Array<{
143
- path: string;
144
- relativePath: string;
145
- }>>;
228
+ export declare type BuildTarget = "dev" | "npm";
146
229
 
147
230
  /**
148
231
  * Plugin to generate TypeScript declaration files using tsgo and emit them through Rslib's asset pipeline.
@@ -180,7 +263,7 @@ export declare function collectDtsFiles(dir: string, baseDir?: string): Promise<
180
263
  *
181
264
  * @example
182
265
  * ```typescript
183
- * import { DtsPlugin } from "@savvy-web/shared/rslib";
266
+ * import { DtsPlugin } from "@savvy-web/rslib-builder";
184
267
  *
185
268
  * export default {
186
269
  * plugins: [
@@ -261,196 +344,173 @@ export declare interface DtsPluginOptions {
261
344
  }
262
345
 
263
346
  /**
264
- * Ensures a temp directory exists for declaration file generation.
265
- * @internal
266
- */
267
- export declare function ensureTempDeclarationDir(cwd: string, name: string): Promise<string>;
268
-
269
- /**
270
- * Extracts TypeScript entry points from package.json for build configuration.
347
+ * Plugin to manage the `files` array in package.json for npm publishing.
271
348
  *
272
349
  * @remarks
273
- * This class analyzes package.json export and bin configurations to identify
274
- * TypeScript source files that need to be built. It handles various export
275
- * formats and automatically maps JavaScript output paths back to their
276
- * TypeScript source files.
277
- *
278
- * **Export Path Mapping:**
279
- * - Converts export keys to entry names (e.g., "./utils" becomes "utils")
280
- * - Maps the root export "." to "index" entry
281
- * - Replaces path separators with hyphens for nested exports (default)
282
- * - When `exportsAsIndexes` is true, preserves path structure
283
- *
284
- * **Source Path Resolution:**
285
- * - Prioritizes TypeScript files (.ts/.tsx) over JavaScript files
286
- * - Maps /dist/ JavaScript paths back to /src/ TypeScript sources
287
- * - Supports conditional exports (import, default, types fields)
350
+ * This plugin automatically populates the `files` field in the output package.json
351
+ * with all compiled assets and essential files. Other plugins can add files via
352
+ * the shared `files-array` exposed through the Rsbuild API.
353
+ *
354
+ * ## Files Included
355
+ *
356
+ * - Essential files: package.json, README.md, LICENSE
357
+ * - All compiled JavaScript files
358
+ * - All declaration files (.d.ts)
359
+ * - Files added by other plugins via `api.useExposed("files-array")`
360
+ *
361
+ * ## Files Excluded
362
+ *
363
+ * - Source map files (.map)
364
+ * - Files prefixed with `!` in the files array (negated patterns)
365
+ *
366
+ * ## Plugin Interoperability
367
+ *
368
+ * Other plugins can add files to the array:
369
+ * ```typescript
370
+ * const filesArray = api.useExposed("files-array") as Set<string>;
371
+ * filesArray.add("my-custom-file.json");
372
+ * ```
373
+ *
374
+ * @param options - Plugin configuration options
288
375
  *
289
376
  * @example
377
+ * Basic usage:
290
378
  * ```typescript
291
- * const extractor = new EntryExtractor();
379
+ * import { FilesArrayPlugin } from '@savvy-web/rslib-builder';
292
380
  *
293
- * const packageJson = {
294
- * exports: {
295
- * ".": "./src/index.ts",
296
- * "./utils": "./src/utils.ts",
297
- * },
298
- * bin: {
299
- * "my-cli": "./src/bin/cli.ts"
300
- * }
381
+ * export default {
382
+ * plugins: [
383
+ * FilesArrayPlugin({ target: 'npm' }),
384
+ * ],
301
385
  * };
386
+ * ```
302
387
  *
303
- * const result = extractor.extract(packageJson);
304
- * console.log(result.entries);
305
- * // {
306
- * // "index": "./src/index.ts",
307
- * // "utils": "./src/utils.ts",
308
- * // "bin/my-cli": "./src/bin/cli.ts"
309
- * // }
388
+ * @example
389
+ * With custom file transformation:
390
+ * ```typescript
391
+ * import { FilesArrayPlugin } from '@savvy-web/rslib-builder';
392
+ *
393
+ * export default {
394
+ * plugins: [
395
+ * FilesArrayPlugin({
396
+ * target: 'npm',
397
+ * transformFiles({ filesArray }) {
398
+ * filesArray.add('CHANGELOG.md');
399
+ * },
400
+ * }),
401
+ * ],
402
+ * };
310
403
  * ```
311
404
  *
312
405
  * @public
313
406
  */
314
- export declare class EntryExtractor {
315
- private readonly options;
316
- constructor(options?: EntryExtractorOptions);
317
- /**
318
- * Extracts entry points from package.json exports and bin fields.
319
- *
320
- * @param packageJson - The package.json to extract entries from
321
- * @returns Object containing the extracted entries
322
- */
323
- extract(packageJson: PackageJson): ExtractedEntries;
324
- /**
325
- * Extracts entries from the exports field.
326
- */
327
- private extractFromExports;
328
- /**
329
- * Extracts entries from the bin field.
330
- */
331
- private extractFromBin;
332
- /**
333
- * Resolves a source path from various export value formats.
334
- */
335
- private resolveSourcePath;
336
- /**
337
- * Resolves a path to its TypeScript source equivalent.
338
- * Maps /dist/ JavaScript paths back to /src/ TypeScript sources.
339
- */
340
- private resolveToTypeScript;
341
- /**
342
- * Checks if a path points to a TypeScript file.
343
- */
344
- private isTypeScriptFile;
345
- /**
346
- * Creates an entry name from an export key.
347
- */
348
- private createEntryName;
349
- }
350
-
351
- /**
352
- * Options for entry extraction.
353
- * @public
354
- */
355
- export declare interface EntryExtractorOptions {
356
- /**
357
- * When true, export paths create index files in nested directories.
358
- * "./foo/bar" becomes "foo/bar/index" instead of "foo-bar".
359
- */
360
- exportsAsIndexes?: boolean;
361
- }
362
-
363
- /**
364
- * Result of entry extraction.
365
- * @public
366
- */
367
- export declare interface ExtractedEntries {
368
- /**
369
- * Entry name to TypeScript source path mapping.
370
- */
371
- entries: Record<string, string>;
372
- }
373
-
374
- /**
375
- * Plugin to manage the files array in package.json
376
- * Adds essential files like package.json, README.md, LICENSE and compiled outputs
377
- * Other plugins can use api.useExposed("files-array") to add additional files
378
- * @public
379
- */
380
407
  export declare const FilesArrayPlugin: <TTarget extends string = string>(options?: FilesArrayPluginOptions<TTarget> | undefined) => RsbuildPlugin;
381
408
 
382
409
  /**
410
+ * Options for the FilesArrayPlugin.
411
+ *
412
+ * @typeParam TTarget - The build target type, defaults to string
413
+ *
383
414
  * @public
384
415
  */
385
416
  export declare interface FilesArrayPluginOptions<TTarget extends string = string> {
386
417
  /**
387
418
  * Optional callback to transform files after they're built but before the files array is finalized.
388
- * Called in the additional stage, after all assets are created.
419
+ *
420
+ * @remarks
421
+ * Called during the "additional" stage of asset processing, after all other assets are created.
422
+ * Use this to copy/rename files or add additional files to the build output.
423
+ *
424
+ * @param context - Transform context containing:
425
+ * - `compilation`: Rspack compilation object with assets
426
+ * - `filesArray`: Set of files to be included in package.json `files` field
427
+ * - `target`: Current build target
428
+ *
429
+ * @example
430
+ * ```typescript
431
+ * import type { FilesArrayPluginOptions } from '@savvy-web/rslib-builder';
432
+ *
433
+ * const options: FilesArrayPluginOptions = {
434
+ * target: 'npm',
435
+ * transformFiles({ compilation, filesArray }) {
436
+ * // Add a custom file to the output
437
+ * filesArray.add('custom-file.txt');
438
+ * },
439
+ * };
440
+ * ```
389
441
  */
390
442
  transformFiles?: (context: {
391
443
  /** Rspack compilation object with assets */
392
444
  compilation: {
393
445
  assets: Record<string, unknown>;
394
446
  };
447
+ /** Set of files to include in package.json files array */
395
448
  filesArray: Set<string>;
449
+ /** Current build target */
396
450
  target: TTarget;
397
451
  }) => void | Promise<void>;
398
- /** Build target (dev/npm/jsr) */
452
+ /**
453
+ * Build target identifier (e.g., "dev", "npm").
454
+ *
455
+ * @remarks
456
+ * Passed to the `transformFiles` callback to allow target-specific transformations.
457
+ */
399
458
  target: TTarget;
400
459
  }
401
460
 
402
461
  /**
403
- * Finds the TypeScript config file.
404
- * @internal
405
- */
406
- export declare function findTsConfig(cwd: string, tsconfigPath?: string): string | null;
407
-
408
- /**
409
- * Flexible type definition for package.json exports field that accommodates various export formats.
462
+ * Builder for Node.js ESM libraries using RSlib.
410
463
  *
411
464
  * @remarks
412
- * This type extends the standard PackageJson.Exports to allow for custom fields and nested
413
- * structures commonly found in complex package configurations. It supports:
414
- * - Standard exports objects with conditions
415
- * - Custom field exports
416
- * - Array-based exports (for fallbacks)
417
- * - Null/undefined values for conditional exports
418
- */
419
- declare type FlexibleExports = PackageJson.Exports | Record<string, unknown> | FlexibleExports[] | undefined | null;
420
-
421
- /**
422
- * Generates command-line arguments for tsgo.
465
+ * NodeLibraryBuilder provides a high-level API for building modern ESM Node.js libraries.
466
+ * It handles TypeScript compilation, declaration bundling, package.json transformation,
467
+ * and multi-target builds (dev and npm).
468
+ *
469
+ * Features:
470
+ * - Automatic entry point detection from package.json exports
471
+ * - TypeScript declarations via tsgo + API Extractor
472
+ * - pnpm catalog and workspace protocol resolution
473
+ * - Source maps for development builds
474
+ * - Configurable external dependencies and type bundling
475
+ *
476
+ * @example
477
+ * Basic usage in `rslib.config.ts`:
478
+ * ```typescript
479
+ * import { NodeLibraryBuilder } from '@savvy-web/rslib-builder';
480
+ *
481
+ * export default NodeLibraryBuilder.create();
482
+ * ```
483
+ *
484
+ * @example
485
+ * With custom options:
486
+ * ```typescript
487
+ * import { NodeLibraryBuilder } from '@savvy-web/rslib-builder';
488
+ *
489
+ * export default NodeLibraryBuilder.create({
490
+ * externals: ['@rslib/core', '@rsbuild/core'],
491
+ * dtsBundledPackages: ['picocolors'],
492
+ * apiModel: true,
493
+ * transform({ target, pkg }) {
494
+ * if (target === 'npm') {
495
+ * delete pkg.devDependencies;
496
+ * }
497
+ * return pkg;
498
+ * },
499
+ * });
500
+ * ```
501
+ *
502
+ * @example
503
+ * Build commands:
504
+ * ```bash
505
+ * # Development build (with source maps)
506
+ * rslib build --env-mode dev
507
+ *
508
+ * # Production build (for npm publishing)
509
+ * rslib build --env-mode npm
510
+ * ```
423
511
  *
424
512
  * @public
425
513
  */
426
- export declare function generateTsgoArgs(options: {
427
- configPath: string;
428
- declarationDir: string;
429
- rootDir?: string;
430
- tsBuildInfoFile?: string;
431
- }): string[];
432
-
433
- /**
434
- * Gets the path to the tsgo (TypeScript native compiler) executable.
435
- * Uses workspace-tools to find the workspace root and searches for tsgo binary.
436
- * Supports npm, pnpm, yarn, rush, and lerna workspaces.
437
- * @returns The absolute path to the tsgo binary
438
- * @internal
439
- */
440
- export declare function getTsgoBinPath(): string;
441
-
442
- /**
443
- * Extracts the unscoped package name from a potentially scoped package name.
444
- * @param name - The package name (e.g., `@scope/package` or `package`)
445
- * @returns The unscoped name (e.g., `package`)
446
- * @internal
447
- */
448
- export declare function getUnscopedPackageName(name: string): string;
449
-
450
- /**
451
- * @public
452
- * Node library builder class
453
- */
454
514
  export declare class NodeLibraryBuilder {
455
515
  static DEFAULT_OPTIONS: NodeLibraryBuilderOptions;
456
516
  static mergeOptions(options?: Partial<NodeLibraryBuilderOptions>): NodeLibraryBuilderOptions;
@@ -518,9 +578,11 @@ export declare interface NodeLibraryBuilderOptions {
518
578
  *
519
579
  * @example
520
580
  * ```typescript
521
- * NodeLibraryBuilder.create({
522
- * externals: ['@rslib/core', '@rsbuild/core']
523
- * })
581
+ * import { NodeLibraryBuilder } from '@savvy-web/rslib-builder';
582
+ *
583
+ * export default NodeLibraryBuilder.create({
584
+ * externals: ['@rslib/core', '@rsbuild/core'],
585
+ * });
524
586
  * ```
525
587
  */
526
588
  externals?: (string | RegExp)[];
@@ -536,9 +598,11 @@ export declare interface NodeLibraryBuilderOptions {
536
598
  *
537
599
  * @example
538
600
  * ```typescript
539
- * NodeLibraryBuilder.create({
540
- * dtsBundledPackages: ['@pnpm/lockfile.types', '@pnpm/types', 'picocolors']
541
- * })
601
+ * import { NodeLibraryBuilder } from '@savvy-web/rslib-builder';
602
+ *
603
+ * export default NodeLibraryBuilder.create({
604
+ * dtsBundledPackages: ['@pnpm/lockfile.types', '@pnpm/types', 'picocolors'],
605
+ * });
542
606
  * ```
543
607
  */
544
608
  dtsBundledPackages?: string[];
@@ -553,16 +617,18 @@ export declare interface NodeLibraryBuilderOptions {
553
617
  *
554
618
  * @example
555
619
  * ```typescript
556
- * NodeLibraryBuilder.create({
557
- * transformFiles({ compilation, filesArray, target }) {
620
+ * import { NodeLibraryBuilder } from '@savvy-web/rslib-builder';
621
+ *
622
+ * export default NodeLibraryBuilder.create({
623
+ * transformFiles({ compilation, filesArray }) {
558
624
  * // Copy index.cjs to .pnpmfile.cjs
559
625
  * const indexAsset = compilation.assets['index.cjs'];
560
626
  * if (indexAsset) {
561
627
  * compilation.assets['.pnpmfile.cjs'] = indexAsset;
562
628
  * filesArray.add('.pnpmfile.cjs');
563
629
  * }
564
- * }
565
- * })
630
+ * },
631
+ * });
566
632
  * ```
567
633
  */
568
634
  transformFiles?: (context: {
@@ -582,16 +648,17 @@ export declare interface NodeLibraryBuilderOptions {
582
648
  *
583
649
  * @example
584
650
  * ```typescript
585
- * NodeLibraryBuilder.create({
651
+ * import { NodeLibraryBuilder } from '@savvy-web/rslib-builder';
652
+ *
653
+ * export default NodeLibraryBuilder.create({
586
654
  * transform({ target, pkg }) {
587
655
  * if (target === 'npm') {
588
- * // Mutation approach
589
656
  * delete pkg.devDependencies;
590
657
  * delete pkg.scripts;
591
658
  * }
592
659
  * return pkg;
593
- * }
594
- * })
660
+ * },
661
+ * });
595
662
  * ```
596
663
  */
597
664
  transform?: TransformPackageJsonFn;
@@ -606,282 +673,213 @@ export declare interface NodeLibraryBuilderOptions {
606
673
  * The file is emitted to dist but excluded from npm publish (added as negated pattern in `files` array).
607
674
  *
608
675
  * @example
676
+ * Enable API model generation with defaults:
609
677
  * ```typescript
610
- * // Enable API model generation with defaults
611
- * NodeLibraryBuilder.create({
678
+ * import { NodeLibraryBuilder } from '@savvy-web/rslib-builder';
679
+ *
680
+ * export default NodeLibraryBuilder.create({
612
681
  * apiModel: true,
613
- * })
682
+ * });
683
+ * ```
684
+ *
685
+ * @example
686
+ * Enable with custom filename:
687
+ * ```typescript
688
+ * import { NodeLibraryBuilder } from '@savvy-web/rslib-builder';
614
689
  *
615
- * // Enable with custom filename
616
- * NodeLibraryBuilder.create({
690
+ * export default NodeLibraryBuilder.create({
617
691
  * apiModel: {
618
692
  * enabled: true,
619
- * filename: "my-package.api.json",
693
+ * filename: 'my-package.api.json',
620
694
  * },
621
- * })
695
+ * });
622
696
  * ```
623
697
  */
624
698
  apiModel?: ApiModelOptions | boolean;
625
699
  }
626
700
 
627
701
  /**
628
- * Transforms package.json for build output and publishing.
702
+ * Plugin to transform package.json for distribution.
629
703
  *
630
704
  * @remarks
631
- * This class consolidates all package.json transformation logic including:
632
- * - Path transformations (src/ to dist/, .ts to .js)
633
- * - Export conditions generation (types, import, require)
634
- * - Bin field transformations
635
- * - PNPM catalog and workspace resolution
636
- * - Field cleanup for publishing
705
+ * This plugin processes the source package.json and transforms it for the build output.
706
+ * It handles path transformations, pnpm catalog/workspace resolution, and field cleanup.
707
+ *
708
+ * ## Transformations Applied
709
+ *
710
+ * - **Path Updates**: Converts source paths to output paths (e.g., `./src/index.ts` → `./index.js`)
711
+ * - **Type Conditions**: Adds `types` fields to exports pointing to `.d.ts` files
712
+ * - **pnpm Resolution**: Resolves `catalog:` and `workspace:*` dependency versions
713
+ * - **Field Cleanup**: Removes `scripts`, `publishConfig`, and other dev-only fields
714
+ * - **Private Flag**: Sets based on `publishConfig.access` or `forcePrivate` option
715
+ *
716
+ * ## Plugin Interoperability
717
+ *
718
+ * - Consumes `entrypoints` map from AutoEntryPlugin
719
+ * - Consumes `exportToOutputMap` for exportsAsIndexes mode
720
+ * - Exposes `files-cache` for asset caching
721
+ * - Consumes `use-rollup-types` flag from DtsPlugin
722
+ *
723
+ * @param options - Plugin configuration options
637
724
  *
638
725
  * @example
726
+ * Basic usage:
639
727
  * ```typescript
640
- * const transformer = new PackageJsonTransformer({
641
- * processTSExports: true,
642
- * collapseIndex: true,
643
- * });
728
+ * import { PackageJsonTransformPlugin } from '@savvy-web/rslib-builder';
644
729
  *
645
- * // For production builds (resolves catalog: and workspace: references)
646
- * const prodPkg = await transformer.transform(packageJson, { isProduction: true });
730
+ * export default {
731
+ * plugins: [
732
+ * PackageJsonTransformPlugin({
733
+ * bundle: true,
734
+ * processTSExports: true,
735
+ * }),
736
+ * ],
737
+ * };
738
+ * ```
739
+ *
740
+ * @example
741
+ * With custom transform:
742
+ * ```typescript
743
+ * import { PackageJsonTransformPlugin } from '@savvy-web/rslib-builder';
647
744
  *
648
- * // For development builds (preserves workspace links)
649
- * const devPkg = await transformer.transform(packageJson, { isProduction: false });
745
+ * export default {
746
+ * plugins: [
747
+ * PackageJsonTransformPlugin({
748
+ * target: 'npm',
749
+ * transform(pkg) {
750
+ * delete pkg.devDependencies;
751
+ * return pkg;
752
+ * },
753
+ * }),
754
+ * ],
755
+ * };
650
756
  * ```
651
757
  *
652
758
  * @public
653
759
  */
654
- export declare class PackageJsonTransformer {
655
- private readonly options;
656
- private readonly pnpmCatalog;
657
- constructor(options?: PackageJsonTransformOptions);
760
+ export declare const PackageJsonTransformPlugin: (options?: PackageJsonTransformPluginOptions) => RsbuildPlugin;
761
+
762
+ /**
763
+ * Options for the PackageJsonTransformPlugin.
764
+ *
765
+ * @public
766
+ */
767
+ export declare interface PackageJsonTransformPluginOptions {
658
768
  /**
659
- * Transforms a single export path for RSLib compatibility.
769
+ * Override the package name in the output package.json.
660
770
  *
661
771
  * @remarks
662
- * Performs several transformations:
663
- * 1. Strips source directory prefixes (exports/, public/, src/)
664
- * 2. Converts TypeScript extensions to JavaScript
665
- * 3. Preserves bin/ prefix for executables
666
- *
667
- * @param path - The file path to transform
668
- * @returns The transformed path
772
+ * - When a string is provided, the package name is replaced with that value
773
+ * - When `true`, the original name is preserved (no override)
774
+ * - When undefined, the original name is preserved
669
775
  *
670
776
  * @example
671
777
  * ```typescript
672
- * transformer.transformExportPath("./src/index.ts"); // "./index.js"
673
- * transformer.transformExportPath("./bin/cli.ts"); // "./bin/cli.js"
674
- * ```
675
- */
676
- transformExportPath(path: string): string;
677
- /**
678
- * Creates a TypeScript declaration file path from a JavaScript file path.
679
- *
680
- * @param jsPath - The JavaScript file path
681
- * @returns The corresponding .d.ts file path
778
+ * import type { PackageJsonTransformPluginOptions } from '@savvy-web/rslib-builder';
682
779
  *
683
- * @example
684
- * ```typescript
685
- * transformer.createTypePath("./index.js"); // "./index.d.ts"
686
- * transformer.createTypePath("./rslib/index.js"); // "./rslib.d.ts" (bundled)
780
+ * const options: PackageJsonTransformPluginOptions = {
781
+ * name: '@scope/my-package-dist',
782
+ * };
687
783
  * ```
688
784
  */
689
- createTypePath(jsPath: string): string;
690
- /**
691
- * Transforms package.json exports field recursively.
692
- *
693
- * @param exports - The exports value to transform
694
- * @param exportKey - The current export key for context
695
- * @returns The transformed exports value
696
- */
697
- transformExports(exports: FlexibleExports, exportKey?: string): FlexibleExports;
785
+ name?: string | true;
698
786
  /**
699
- * Transforms the bin field for build output.
787
+ * Force the output package.json to have `"private": true`.
700
788
  *
701
789
  * @remarks
702
- * TypeScript bin entries are compiled to `./bin/{command-name}.js` by RSlib.
703
- * Non-TypeScript entries (shell scripts, compiled JS) are preserved as-is.
790
+ * Useful for development builds that should never be published.
791
+ * When true, overrides the `publishConfig.access` setting.
704
792
  *
705
- * @param bin - The bin field from package.json
706
- * @returns The transformed bin field
793
+ * @defaultValue false
707
794
  */
708
- transformBin(bin: PackageJson["bin"]): PackageJson["bin"];
795
+ forcePrivate?: boolean;
709
796
  /**
710
- * Performs the complete package.json transformation.
797
+ * Whether to process TypeScript exports and generate type conditions.
711
798
  *
712
- * @param packageJson - The source package.json
713
- * @param context - Transform context with properties:
714
- * - `isProduction`: Whether this is a production build
715
- * - `customTransform`: Optional custom transform function
716
- * @returns The transformed package.json
717
- */
718
- transform(packageJson: PackageJson, context?: {
719
- isProduction?: boolean;
720
- customTransform?: (pkg: PackageJson) => PackageJson;
721
- }): Promise<PackageJson>;
722
- private applyPnpmTransformations;
723
- /**
724
- * Applies RSLib-specific transformations.
725
- */
726
- private applyRslibTransformations;
727
- /**
728
- * Transforms a string export value.
729
- */
730
- private transformStringExport;
731
- /**
732
- * Transforms an object export value.
733
- */
734
- private transformObjectExports;
735
- /**
736
- * Determines if an export object represents export conditions.
737
- */
738
- private isConditionsObject;
739
- /**
740
- * Transforms a single export entry.
741
- */
742
- private transformExportEntry;
743
- }
744
-
745
- /**
746
- * Options for transforming package.json.
747
- * @public
748
- */
749
- export declare interface PackageJsonTransformOptions {
750
- /**
751
- * Whether to process TypeScript exports (convert .ts to .js and add types field).
752
- * @defaultValue true
799
+ * @remarks
800
+ * When enabled, transforms export paths from `.ts` to `.js` and adds
801
+ * `types` conditions pointing to the corresponding `.d.ts` files.
802
+ *
803
+ * @example
804
+ * Input: `"./src/index.ts"`
805
+ * Output: `{ "types": "./index.d.ts", "import": "./index.js" }`
753
806
  */
754
807
  processTSExports?: boolean;
755
808
  /**
756
- * Whether to collapse index files (./foo/index.ts becomes ./foo.js).
757
- * This is typically true for bundled builds.
809
+ * Whether the build is in bundle mode.
810
+ *
811
+ * @remarks
812
+ * Affects export path transformations - in bundle mode, nested index files
813
+ * are collapsed (e.g., `./utils/index.ts` becomes `./utils.js`).
814
+ *
758
815
  * @defaultValue false
759
816
  */
760
- collapseIndex?: boolean;
761
- /**
762
- * Map of export paths to entry files (from AutoEntryPlugin).
763
- */
764
- entrypoints?: Map<string, string>;
765
- /**
766
- * Map of export paths to output files (for exportsAsIndexes mode).
767
- */
768
- exportToOutputMap?: Map<string, string>;
769
- }
770
-
771
- /**
772
- * Plugin to process package.json for distribution
773
- * @public
774
- */
775
- export declare const PackageJsonTransformPlugin: (options?: PackageJsonTransformPluginOptions) => RsbuildPlugin;
776
-
777
- /**
778
- * @public
779
- */
780
- export declare interface PackageJsonTransformPluginOptions {
781
- /** Override the name property of the source package.json when building to a target */
782
- name?: string | true;
783
- /** Whether to force include private packages (with "private": true) in the output */
784
- forcePrivate?: boolean;
785
- /** Whether to process package.json exports of into */
786
- processTSExports?: boolean;
787
- /** Whether the build is in bundle mode (affects export path transformation) */
788
817
  bundle?: boolean;
789
- /** Build target (dev, npm) - used for custom transformations */
790
- target?: string;
791
- /** Optional transform function to modify package.json after standard transformations */
792
- transform?: (pkg: PackageJson) => PackageJson;
793
- }
794
-
795
- /**
796
- * Manages PNPM catalog resolution with caching.
797
- *
798
- * @remarks
799
- * This class handles the resolution of PNPM-specific dependency references:
800
- * - `catalog:` references to centralized version definitions
801
- * - `workspace:` references to local workspace packages
802
- *
803
- * The class caches the catalog data based on file modification time to avoid
804
- * repeated filesystem operations during builds.
805
- *
806
- * @example
807
- * ```typescript
808
- * const catalog = new PnpmCatalog();
809
- *
810
- * // Get the catalog data
811
- * const versions = await catalog.getCatalog();
812
- * console.log(versions);
813
- * // { "react": "^18.2.0", "typescript": "^5.0.0" }
814
- *
815
- * // Resolve package.json dependencies
816
- * const resolved = await catalog.resolvePackageJson(packageJson);
817
- * ```
818
- *
819
- * @public
820
- */
821
- export declare class PnpmCatalog {
822
- private catalogCache;
823
- private catalogCacheMtime;
824
- private cachedWorkspaceRoot;
825
818
  /**
826
- * Clears the cached catalog data.
819
+ * Build target identifier for custom transformations.
827
820
  *
828
821
  * @remarks
829
- * Useful in testing scenarios to ensure clean state between tests.
822
+ * Passed to the transform function to allow target-specific modifications.
823
+ * Common values: "dev", "npm"
830
824
  */
831
- clearCache(): void;
825
+ target?: string;
832
826
  /**
833
- * Gets the PNPM catalog from pnpm-workspace.yaml.
827
+ * Custom transform function to modify package.json after standard transformations.
834
828
  *
835
829
  * @remarks
836
- * The catalog is cached based on file modification time. If the file hasn't
837
- * changed since the last read, the cached version is returned.
830
+ * Called after all built-in transformations (path updates, pnpm resolution, etc.)
831
+ * are applied. Mutations to the object are also supported.
838
832
  *
839
- * @returns The catalog mapping dependency names to versions
840
- */
841
- getCatalog(): Promise<Record<string, string>>;
842
- /**
843
- * Resolves catalog: and workspace: references in a package.json.
833
+ * @param pkg - The package.json object after standard transformations
834
+ * @returns The modified package.json object
844
835
  *
845
- * @param packageJson - The package.json to resolve
846
- * @param dir - The directory containing the package (defaults to cwd)
847
- * @returns The resolved package.json
836
+ * @example
837
+ * ```typescript
838
+ * import type { PackageJsonTransformPluginOptions } from '@savvy-web/rslib-builder';
848
839
  *
849
- * @throws When resolution fails for critical dependencies
850
- */
851
- resolvePackageJson(packageJson: PackageJson, dir?: string): Promise<PackageJson>;
852
- /**
853
- * Collects dependencies with a specific prefix (catalog: or workspace:).
854
- */
855
- private collectDependencies;
856
- /**
857
- * Logs resolved dependencies in a formatted way.
858
- */
859
- private logResolvedDependencies;
860
- /**
861
- * Validates that no unresolved catalog: or workspace: references remain.
840
+ * const options: PackageJsonTransformPluginOptions = {
841
+ * transform(pkg) {
842
+ * delete pkg.devDependencies;
843
+ * pkg.publishConfig = { access: 'public' };
844
+ * return pkg;
845
+ * },
846
+ * };
847
+ * ```
862
848
  */
863
- private validateNoUnresolvedReferences;
849
+ transform?: (pkg: PackageJson) => PackageJson;
864
850
  }
865
851
 
866
852
  /**
867
853
  * Async RSLib configuration function type.
868
854
  * @public
869
855
  */
870
- export declare type RslibConfigAsyncFn = (env: ConfigParams) => Promise<RslibConfig>;
856
+ declare type RslibConfigAsyncFn = (env: ConfigParams) => Promise<RslibConfig>;
871
857
 
872
858
  /**
873
- * Strips sourceMappingURL comment from declaration file content.
874
- * This removes comments like: `//# source` + `MappingURL=index.d.ts.map`
859
+ * Function to transform package.json during the build process.
875
860
  *
876
861
  * @remarks
877
- * Source maps are preserved in the temp directory for API Extractor documentation
878
- * but are excluded from the final dist output to reduce package size.
862
+ * This function is called after all standard transformations are applied,
863
+ * allowing you to modify the package.json before it's written to the output directory.
864
+ * Mutations to the `pkg` object are also supported.
879
865
  *
880
- * @public
881
- */
882
- export declare function stripSourceMapComment(content: string): string;
883
-
884
- /**
866
+ * @param context - Transform context containing:
867
+ * - `target`: The current build target ("dev" or "npm")
868
+ * - `pkg`: The package.json object to transform
869
+ * @returns The modified package.json object
870
+ *
871
+ * @example
872
+ * ```typescript
873
+ * import type { TransformPackageJsonFn } from '@savvy-web/rslib-builder';
874
+ *
875
+ * const transform: TransformPackageJsonFn = ({ target, pkg }) => {
876
+ * if (target === 'npm') {
877
+ * delete pkg.devDependencies;
878
+ * delete pkg.scripts;
879
+ * }
880
+ * return pkg;
881
+ * };
882
+ * ```
885
883
  * @public
886
884
  */
887
885
  export declare type TransformPackageJsonFn = (context: {
@@ -890,8 +888,50 @@ export declare type TransformPackageJsonFn = (context: {
890
888
  }) => PackageJson;
891
889
 
892
890
  /**
893
- * Builder for TSDoc configuration files.
894
- * Handles tag group expansion, config generation, and file persistence.
891
+ * Builder for TSDoc configuration files used by API Extractor.
892
+ *
893
+ * @remarks
894
+ * This class provides utilities for generating `tsdoc.json` configuration files
895
+ * that control TSDoc tag support during API documentation generation.
896
+ *
897
+ * ## Features
898
+ *
899
+ * - Expands tag groups into individual tag definitions
900
+ * - Generates properly formatted tsdoc.json files
901
+ * - Handles config persistence based on environment (CI vs local)
902
+ * - Supports custom tag definitions
903
+ *
904
+ * ## Tag Groups
905
+ *
906
+ * The builder supports three standardization groups from `\@microsoft/tsdoc`:
907
+ * - `core`: Essential tags (`\@param`, `\@returns`, `\@remarks`, etc.)
908
+ * - `extended`: Additional tags (`\@example`, `\@defaultValue`, `\@see`, etc.)
909
+ * - `discretionary`: Release tags (`\@alpha`, `\@beta`, `\@public`, `\@internal`)
910
+ *
911
+ * @example
912
+ * Build tag configuration from options:
913
+ * ```typescript
914
+ * import { TsDocConfigBuilder } from '@savvy-web/rslib-builder';
915
+ *
916
+ * const config = TsDocConfigBuilder.build({
917
+ * groups: ['core', 'extended'],
918
+ * tagDefinitions: [
919
+ * { tagName: '@error', syntaxKind: 'inline' },
920
+ * ],
921
+ * });
922
+ * ```
923
+ *
924
+ * @example
925
+ * Write a tsdoc.json file:
926
+ * ```typescript
927
+ * import { TsDocConfigBuilder } from '@savvy-web/rslib-builder';
928
+ *
929
+ * const configPath = await TsDocConfigBuilder.writeConfigFile(
930
+ * { groups: ['core', 'extended', 'discretionary'] },
931
+ * process.cwd(),
932
+ * );
933
+ * ```
934
+ *
895
935
  * @public
896
936
  */
897
937
  export declare class TsDocConfigBuilder {
@@ -1016,10 +1056,12 @@ export declare interface TsDocOptions {
1016
1056
  *
1017
1057
  * @example
1018
1058
  * ```typescript
1019
- * tagDefinitions: [
1020
- * { tagName: "@error", syntaxKind: "inline" },
1021
- * { tagName: "@category", syntaxKind: "block", allowMultiple: false }
1022
- * ]
1059
+ * import type { TsDocTagDefinition } from '@savvy-web/rslib-builder';
1060
+ *
1061
+ * const tagDefinitions: TsDocTagDefinition[] = [
1062
+ * { tagName: '@error', syntaxKind: 'inline' },
1063
+ * { tagName: '@category', syntaxKind: 'block', allowMultiple: false },
1064
+ * ];
1023
1065
  * ```
1024
1066
  */
1025
1067
  tagDefinitions?: TsDocTagDefinition[];
@@ -1028,9 +1070,9 @@ export declare interface TsDocOptions {
1028
1070
  * Tags from enabled groups and custom tagDefinitions are auto-supported.
1029
1071
  *
1030
1072
  * @example
1073
+ * Disable \@beta even though "extended" group is enabled:
1031
1074
  * ```typescript
1032
- * // Disable @beta even though "extended" group is enabled
1033
- * supportForTags: { "@beta": false }
1075
+ * const supportForTags: Record<string, boolean> = { '@beta': false };
1034
1076
  * ```
1035
1077
  */
1036
1078
  supportForTags?: Record<string, boolean>;
@@ -1054,15 +1096,6 @@ export declare interface TsDocOptions {
1054
1096
  * TSDoc warnings include unknown tags, malformed syntax, and other
1055
1097
  * documentation issues detected by API Extractor during processing.
1056
1098
  *
1057
- * @example
1058
- * ```typescript
1059
- * // Fail build on any TSDoc issues (CI default)
1060
- * warnings: "fail"
1061
- *
1062
- * // Show warnings but continue build (local default)
1063
- * warnings: "log"
1064
- * ```
1065
- *
1066
1099
  * @defaultValue `"fail"` in CI environments (`CI` or `GITHUB_ACTIONS` env vars),
1067
1100
  * `"log"` otherwise
1068
1101
  */
@@ -1084,6 +1117,27 @@ export declare interface TsDocTagDefinition {
1084
1117
 
1085
1118
  /**
1086
1119
  * TSDoc standardization groups for predefined tag sets.
1120
+ *
1121
+ * @remarks
1122
+ * These groups correspond to the TSDoc specification's standardization levels
1123
+ * as defined in `\@microsoft/tsdoc`. Each group contains a set of related tags:
1124
+ *
1125
+ * - `"core"`: Essential tags for basic documentation
1126
+ * (`\@param`, `\@returns`, `\@remarks`, `\@deprecated`, `\@privateRemarks`, etc.)
1127
+ *
1128
+ * - `"extended"`: Additional tags for richer documentation
1129
+ * (`\@example`, `\@defaultValue`, `\@see`, `\@throws`, `\@typeParam`, etc.)
1130
+ *
1131
+ * - `"discretionary"`: Release stage and visibility modifiers
1132
+ * (`\@alpha`, `\@beta`, `\@public`, `\@internal`, `\@experimental`)
1133
+ *
1134
+ * @example
1135
+ * ```typescript
1136
+ * import type { TsDocTagGroup } from '@savvy-web/rslib-builder';
1137
+ *
1138
+ * const groups: TsDocTagGroup[] = ['core', 'extended'];
1139
+ * ```
1140
+ *
1087
1141
  * @public
1088
1142
  */
1089
1143
  export declare type TsDocTagGroup = "core" | "extended" | "discretionary";