@savvy-web/rslib-builder 0.1.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/.npmignore +2 -0
- package/LICENSE +21 -0
- package/README.md +160 -0
- package/api.model.json +3022 -0
- package/index.d.ts +855 -0
- package/index.js +1521 -0
- package/package.json +86 -0
- package/rslib-runtime.js +18 -0
- package/tsconfig/node/ecma/lib.json +49 -0
- package/tsconfig/root.json +13 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,855 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RSLib-based build system for Node.js libraries with automatic package.json transformation,
|
|
3
|
+
* TypeScript declaration bundling, and multi-target support.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* This package provides a powerful builder system built on top of RSLib that simplifies the
|
|
7
|
+
* process of building modern ESM Node.js libraries. It offers:
|
|
8
|
+
*
|
|
9
|
+
* - **Automatic Entry Detection**: Auto-detects entry points from package.json exports
|
|
10
|
+
* - **Multi-Target Builds**: Support for dev and npm build targets
|
|
11
|
+
* - **Bundled ESM Output**: Optimized single-file outputs with rolled-up types
|
|
12
|
+
* - **Package.json Transformation**: Automatic path updates, PNPM catalog resolution
|
|
13
|
+
* - **TypeScript Declaration Bundling**: Using tsgo and API Extractor
|
|
14
|
+
* - **File Array Generation**: Automatic files array creation for package.json
|
|
15
|
+
* - **API Model Generation**: Optional api.model.json for documentation tooling
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* Basic usage in rslib.config.ts:
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { NodeLibraryBuilder } from '@savvy-web/rslib-builder';
|
|
21
|
+
*
|
|
22
|
+
* export default NodeLibraryBuilder.create({});
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* With custom transformations:
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import { NodeLibraryBuilder } from '@savvy-web/rslib-builder';
|
|
29
|
+
*
|
|
30
|
+
* export default NodeLibraryBuilder.create({
|
|
31
|
+
* externals: ['@rslib/core'],
|
|
32
|
+
* transform({ pkg }) {
|
|
33
|
+
* delete pkg.devDependencies;
|
|
34
|
+
* return pkg;
|
|
35
|
+
* }
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @packageDocumentation
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
import type { ConfigParams } from '@rslib/core';
|
|
43
|
+
import type { PackageJson } from 'type-fest';
|
|
44
|
+
import type { RawCopyPattern } from '@rspack/binding';
|
|
45
|
+
import type { RsbuildPlugin } from '@rsbuild/core';
|
|
46
|
+
import type { RslibConfig } from '@rslib/core';
|
|
47
|
+
import type { SourceConfig } from '@rsbuild/core';
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Options for API model generation.
|
|
51
|
+
* When enabled, generates an api.model.json file using API Extractor.
|
|
52
|
+
*
|
|
53
|
+
* @remarks
|
|
54
|
+
* API models are only generated for the main "index" entry point (the "." export).
|
|
55
|
+
* Additional entry points like "./hooks" or "./utils" do not generate separate API models.
|
|
56
|
+
* This prevents multiple conflicting API models and ensures a single source of truth
|
|
57
|
+
* for package documentation.
|
|
58
|
+
*
|
|
59
|
+
* @public
|
|
60
|
+
*/
|
|
61
|
+
export declare interface ApiModelOptions {
|
|
62
|
+
/**
|
|
63
|
+
* Whether to enable API model generation.
|
|
64
|
+
* @defaultValue false
|
|
65
|
+
*/
|
|
66
|
+
enabled?: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Filename for the generated API model file.
|
|
69
|
+
* @defaultValue "api.model.json"
|
|
70
|
+
*/
|
|
71
|
+
filename?: string;
|
|
72
|
+
/**
|
|
73
|
+
* Whether to add a .npmignore file that excludes the API model file.
|
|
74
|
+
* This is useful when the API model is for internal tooling only.
|
|
75
|
+
* @defaultValue true
|
|
76
|
+
*/
|
|
77
|
+
npmIgnore?: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Local paths to copy the API model and package.json to.
|
|
80
|
+
* Used for local testing with documentation systems.
|
|
81
|
+
*
|
|
82
|
+
* @remarks
|
|
83
|
+
* Each path must be a directory. The parent directory must exist,
|
|
84
|
+
* but the final directory will be created if it doesn't exist.
|
|
85
|
+
* Both api.model.json and the processed package.json will be copied.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* apiModel: {
|
|
90
|
+
* enabled: true,
|
|
91
|
+
* localPaths: ["../docs-site/lib/packages/my-package"],
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
localPaths?: string[];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Plugin to read package.json and configure entry points based on exports.
|
|
100
|
+
*
|
|
101
|
+
* @param options - Plugin configuration options
|
|
102
|
+
* @param options.exportsAsIndexes - When true, export paths create index files in nested directories
|
|
103
|
+
* @public
|
|
104
|
+
*/
|
|
105
|
+
export declare const AutoEntryPlugin: (options?: {
|
|
106
|
+
exportsAsIndexes?: boolean | undefined;
|
|
107
|
+
} | undefined) => RsbuildPlugin;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @public
|
|
111
|
+
*/
|
|
112
|
+
export declare type BuildTarget = "dev" | "npm";
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Recursively collects all .d.ts and .d.ts.map files from a directory.
|
|
116
|
+
*
|
|
117
|
+
* @public
|
|
118
|
+
*/
|
|
119
|
+
export declare function collectDtsFiles(dir: string, baseDir?: string): Promise<Array<{
|
|
120
|
+
path: string;
|
|
121
|
+
relativePath: string;
|
|
122
|
+
}>>;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Plugin to generate TypeScript declaration files using tsgo and emit them through Rslib's asset pipeline.
|
|
126
|
+
*
|
|
127
|
+
* @remarks
|
|
128
|
+
* This plugin uses tsgo (@typescript/native-preview) for faster declaration file generation
|
|
129
|
+
* and integrates with Rslib's build system by emitting generated files as compilation assets.
|
|
130
|
+
*
|
|
131
|
+
* ## Features
|
|
132
|
+
*
|
|
133
|
+
* - Uses tsgo exclusively for .d.ts generation (faster than tsc)
|
|
134
|
+
* - Emits .d.ts and .d.ts.map files through Rslib's asset pipeline
|
|
135
|
+
* - Optional bundling with API Extractor for rollup mode
|
|
136
|
+
* - Supports watch mode
|
|
137
|
+
* - Integrates seamlessly with other Rslib plugins
|
|
138
|
+
* - Respects tsconfig.json settings
|
|
139
|
+
*
|
|
140
|
+
* ## Build Workflow
|
|
141
|
+
*
|
|
142
|
+
* 1. **Config Phase** (`modifyRsbuildConfig`):
|
|
143
|
+
* - Finds and loads tsconfig.json
|
|
144
|
+
* - Creates temp directory for .d.ts generation
|
|
145
|
+
* - Validates configuration
|
|
146
|
+
*
|
|
147
|
+
* 2. **Asset Processing Phase** (`processAssets`):
|
|
148
|
+
* - Cleans temp directory at start of build (to remove stale files)
|
|
149
|
+
* - Spawns tsgo to generate .d.ts files to temp directory
|
|
150
|
+
* - Recursively collects all generated .d.ts and .d.ts.map files
|
|
151
|
+
* - If bundling: uses API Extractor to bundle declarations per entry point
|
|
152
|
+
* - Emits files through compilation.emitAsset()
|
|
153
|
+
* - Adds .d.ts files (not .d.ts.map) to the files array for npm publishing
|
|
154
|
+
* - Preserves temp directory after build for API Extractor documentation generation
|
|
155
|
+
*
|
|
156
|
+
* @param options - Plugin configuration options
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* import { DtsPlugin } from "@savvy-web/shared/rslib";
|
|
161
|
+
*
|
|
162
|
+
* export default {
|
|
163
|
+
* plugins: [
|
|
164
|
+
* DtsPlugin({
|
|
165
|
+
* abortOnError: true,
|
|
166
|
+
* bundle: true,
|
|
167
|
+
* bundledPackages: ['type-fest', '@commitlint/*']
|
|
168
|
+
* })
|
|
169
|
+
* ]
|
|
170
|
+
* };
|
|
171
|
+
* ```
|
|
172
|
+
*
|
|
173
|
+
* @public
|
|
174
|
+
*/
|
|
175
|
+
export declare const DtsPlugin: (options?: DtsPluginOptions) => RsbuildPlugin;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Options for configuring the DTS plugin.
|
|
179
|
+
* @public
|
|
180
|
+
*/
|
|
181
|
+
export declare interface DtsPluginOptions {
|
|
182
|
+
/**
|
|
183
|
+
* Path to TypeScript configuration file.
|
|
184
|
+
* If not specified, uses the tsconfigPath from Rsbuild config or searches for tsconfig.json.
|
|
185
|
+
*/
|
|
186
|
+
tsconfigPath?: string;
|
|
187
|
+
/**
|
|
188
|
+
* Custom output directory for declaration files relative to dist root.
|
|
189
|
+
* If not specified, uses the declarationDir from tsconfig.json or falls back to dist root.
|
|
190
|
+
*/
|
|
191
|
+
distPath?: string;
|
|
192
|
+
/**
|
|
193
|
+
* Whether to abort the build on TypeScript errors.
|
|
194
|
+
* @defaultValue true
|
|
195
|
+
*/
|
|
196
|
+
abortOnError?: boolean;
|
|
197
|
+
/**
|
|
198
|
+
* Custom file extension for declaration files.
|
|
199
|
+
* @defaultValue ".d.ts"
|
|
200
|
+
*/
|
|
201
|
+
dtsExtension?: string;
|
|
202
|
+
/**
|
|
203
|
+
* Whether to bundle declaration files using API Extractor.
|
|
204
|
+
* When true, generates a single .d.ts file per entry point.
|
|
205
|
+
* @defaultValue false
|
|
206
|
+
*/
|
|
207
|
+
bundle?: boolean;
|
|
208
|
+
/**
|
|
209
|
+
* Packages whose types should be bundled (inlined) into the output .d.ts files.
|
|
210
|
+
* Only applies when bundle is true.
|
|
211
|
+
* Supports glob patterns (e.g., '@commitlint/*', 'type-fest')
|
|
212
|
+
* @defaultValue []
|
|
213
|
+
*/
|
|
214
|
+
bundledPackages?: string[];
|
|
215
|
+
/**
|
|
216
|
+
* Banner text to add at the top of bundled declaration files.
|
|
217
|
+
* Only applies when bundle is true.
|
|
218
|
+
*/
|
|
219
|
+
banner?: string;
|
|
220
|
+
/**
|
|
221
|
+
* Footer text to add at the bottom of bundled declaration files.
|
|
222
|
+
* Only applies when bundle is true.
|
|
223
|
+
*/
|
|
224
|
+
footer?: string;
|
|
225
|
+
/**
|
|
226
|
+
* Build target (dev, npm).
|
|
227
|
+
* Used to generate the correct temp tsconfig when tsconfigPath is not provided.
|
|
228
|
+
*/
|
|
229
|
+
buildTarget?: "dev" | "npm";
|
|
230
|
+
/**
|
|
231
|
+
* Options for API model generation.
|
|
232
|
+
* When enabled, generates an api.model.json file in the dist directory.
|
|
233
|
+
* Only applies when bundle is true.
|
|
234
|
+
*/
|
|
235
|
+
apiModel?: ApiModelOptions | boolean;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Ensures a temp directory exists for declaration file generation.
|
|
240
|
+
* @internal
|
|
241
|
+
*/
|
|
242
|
+
export declare function ensureTempDeclarationDir(cwd: string, name: string): Promise<string>;
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Extracts TypeScript entry points from package.json for build configuration.
|
|
246
|
+
*
|
|
247
|
+
* @remarks
|
|
248
|
+
* This class analyzes package.json export and bin configurations to identify
|
|
249
|
+
* TypeScript source files that need to be built. It handles various export
|
|
250
|
+
* formats and automatically maps JavaScript output paths back to their
|
|
251
|
+
* TypeScript source files.
|
|
252
|
+
*
|
|
253
|
+
* **Export Path Mapping:**
|
|
254
|
+
* - Converts export keys to entry names (e.g., "./utils" -> "utils")
|
|
255
|
+
* - Maps the root export "." to "index" entry
|
|
256
|
+
* - Replaces path separators with hyphens for nested exports (default)
|
|
257
|
+
* - When `exportsAsIndexes` is true, preserves path structure
|
|
258
|
+
*
|
|
259
|
+
* **Source Path Resolution:**
|
|
260
|
+
* - Prioritizes TypeScript files (.ts/.tsx) over JavaScript files
|
|
261
|
+
* - Maps /dist/ JavaScript paths back to /src/ TypeScript sources
|
|
262
|
+
* - Supports conditional exports (import, default, types fields)
|
|
263
|
+
*
|
|
264
|
+
* @example
|
|
265
|
+
* ```typescript
|
|
266
|
+
* const extractor = new EntryExtractor();
|
|
267
|
+
*
|
|
268
|
+
* const packageJson = {
|
|
269
|
+
* exports: {
|
|
270
|
+
* ".": "./src/index.ts",
|
|
271
|
+
* "./utils": "./src/utils.ts",
|
|
272
|
+
* },
|
|
273
|
+
* bin: {
|
|
274
|
+
* "my-cli": "./src/bin/cli.ts"
|
|
275
|
+
* }
|
|
276
|
+
* };
|
|
277
|
+
*
|
|
278
|
+
* const result = extractor.extract(packageJson);
|
|
279
|
+
* console.log(result.entries);
|
|
280
|
+
* // {
|
|
281
|
+
* // "index": "./src/index.ts",
|
|
282
|
+
* // "utils": "./src/utils.ts",
|
|
283
|
+
* // "bin/my-cli": "./src/bin/cli.ts"
|
|
284
|
+
* // }
|
|
285
|
+
* ```
|
|
286
|
+
*
|
|
287
|
+
* @public
|
|
288
|
+
*/
|
|
289
|
+
export declare class EntryExtractor {
|
|
290
|
+
private readonly options;
|
|
291
|
+
constructor(options?: EntryExtractorOptions);
|
|
292
|
+
/**
|
|
293
|
+
* Extracts entry points from package.json exports and bin fields.
|
|
294
|
+
*
|
|
295
|
+
* @param packageJson - The package.json to extract entries from
|
|
296
|
+
* @returns Object containing the extracted entries
|
|
297
|
+
*/
|
|
298
|
+
extract(packageJson: PackageJson): ExtractedEntries;
|
|
299
|
+
/**
|
|
300
|
+
* Extracts entries from the exports field.
|
|
301
|
+
*/
|
|
302
|
+
private extractFromExports;
|
|
303
|
+
/**
|
|
304
|
+
* Extracts entries from the bin field.
|
|
305
|
+
*/
|
|
306
|
+
private extractFromBin;
|
|
307
|
+
/**
|
|
308
|
+
* Resolves a source path from various export value formats.
|
|
309
|
+
*/
|
|
310
|
+
private resolveSourcePath;
|
|
311
|
+
/**
|
|
312
|
+
* Resolves a path to its TypeScript source equivalent.
|
|
313
|
+
* Maps /dist/ JavaScript paths back to /src/ TypeScript sources.
|
|
314
|
+
*/
|
|
315
|
+
private resolveToTypeScript;
|
|
316
|
+
/**
|
|
317
|
+
* Checks if a path points to a TypeScript file.
|
|
318
|
+
*/
|
|
319
|
+
private isTypeScriptFile;
|
|
320
|
+
/**
|
|
321
|
+
* Creates an entry name from an export key.
|
|
322
|
+
*/
|
|
323
|
+
private createEntryName;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Options for entry extraction.
|
|
328
|
+
* @public
|
|
329
|
+
*/
|
|
330
|
+
export declare interface EntryExtractorOptions {
|
|
331
|
+
/**
|
|
332
|
+
* When true, export paths create index files in nested directories.
|
|
333
|
+
* "./foo/bar" becomes "foo/bar/index" instead of "foo-bar".
|
|
334
|
+
*/
|
|
335
|
+
exportsAsIndexes?: boolean;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Result of entry extraction.
|
|
340
|
+
* @public
|
|
341
|
+
*/
|
|
342
|
+
export declare interface ExtractedEntries {
|
|
343
|
+
/**
|
|
344
|
+
* Entry name to TypeScript source path mapping.
|
|
345
|
+
*/
|
|
346
|
+
entries: Record<string, string>;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Plugin to manage the files array in package.json
|
|
351
|
+
* Adds essential files like package.json, README.md, LICENSE and compiled outputs
|
|
352
|
+
* Other plugins can use api.useExposed("files-array") to add additional files
|
|
353
|
+
* @public
|
|
354
|
+
*/
|
|
355
|
+
export declare const FilesArrayPlugin: <TTarget extends string = string>(options?: FilesArrayPluginOptions<TTarget> | undefined) => RsbuildPlugin;
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* @public
|
|
359
|
+
*/
|
|
360
|
+
export declare interface FilesArrayPluginOptions<TTarget extends string = string> {
|
|
361
|
+
/**
|
|
362
|
+
* Optional callback to transform files after they're built but before the files array is finalized.
|
|
363
|
+
* Called in the additional stage, after all assets are created.
|
|
364
|
+
*/
|
|
365
|
+
transformFiles?: (context: {
|
|
366
|
+
/** Rspack compilation object with assets */
|
|
367
|
+
compilation: {
|
|
368
|
+
assets: Record<string, unknown>;
|
|
369
|
+
};
|
|
370
|
+
filesArray: Set<string>;
|
|
371
|
+
target: TTarget;
|
|
372
|
+
}) => void | Promise<void>;
|
|
373
|
+
/** Build target (dev/npm/jsr) */
|
|
374
|
+
target: TTarget;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Finds the TypeScript config file.
|
|
379
|
+
* @internal
|
|
380
|
+
*/
|
|
381
|
+
export declare function findTsConfig(cwd: string, tsconfigPath?: string): string | null;
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Flexible type definition for package.json exports field that accommodates various export formats.
|
|
385
|
+
*
|
|
386
|
+
* @remarks
|
|
387
|
+
* This type extends the standard PackageJson.Exports to allow for custom fields and nested
|
|
388
|
+
* structures commonly found in complex package configurations. It supports:
|
|
389
|
+
* - Standard exports objects with conditions
|
|
390
|
+
* - Custom field exports
|
|
391
|
+
* - Array-based exports (for fallbacks)
|
|
392
|
+
* - Null/undefined values for conditional exports
|
|
393
|
+
*/
|
|
394
|
+
declare type FlexibleExports = PackageJson.Exports | Record<string, unknown> | FlexibleExports[] | undefined | null;
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Generates command-line arguments for tsgo.
|
|
398
|
+
*
|
|
399
|
+
* @public
|
|
400
|
+
*/
|
|
401
|
+
export declare function generateTsgoArgs(options: {
|
|
402
|
+
configPath: string;
|
|
403
|
+
declarationDir: string;
|
|
404
|
+
rootDir?: string;
|
|
405
|
+
tsBuildInfoFile?: string;
|
|
406
|
+
}): string[];
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Gets the path to the tsgo (TypeScript native compiler) executable.
|
|
410
|
+
* Uses workspace-tools to find the workspace root and searches for tsgo binary.
|
|
411
|
+
* Supports npm, pnpm, yarn, rush, and lerna workspaces.
|
|
412
|
+
* @returns The absolute path to the tsgo binary
|
|
413
|
+
* @internal
|
|
414
|
+
*/
|
|
415
|
+
export declare function getTsgoBinPath(): string;
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* @public
|
|
419
|
+
* Node library builder class
|
|
420
|
+
*/
|
|
421
|
+
export declare class NodeLibraryBuilder {
|
|
422
|
+
static DEFAULT_OPTIONS: NodeLibraryBuilderOptions;
|
|
423
|
+
static mergeOptions(options?: Partial<NodeLibraryBuilderOptions>): NodeLibraryBuilderOptions;
|
|
424
|
+
/**
|
|
425
|
+
* Creates an async RSLib configuration function that determines build target from envMode.
|
|
426
|
+
* This provides a clean API where users don't need to handle environment logic.
|
|
427
|
+
*/
|
|
428
|
+
static create(options?: Partial<NodeLibraryBuilderOptions>): RslibConfigAsyncFn;
|
|
429
|
+
/**
|
|
430
|
+
* Creates a single-target build configuration.
|
|
431
|
+
* This allows proper plugin isolation per build target.
|
|
432
|
+
*/
|
|
433
|
+
static createSingleTarget(target: BuildTarget, opts: NodeLibraryBuilderOptions): Promise<RslibConfig>;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* @public
|
|
438
|
+
*/
|
|
439
|
+
export declare interface NodeLibraryBuilderOptions {
|
|
440
|
+
/** Override entry points (optional - will auto-detect from package.json) */
|
|
441
|
+
entry?: Record<string, string | string[]>;
|
|
442
|
+
/**
|
|
443
|
+
* When enabled, each export path will generate an index.js file in a directory
|
|
444
|
+
* structure matching the export path, rather than using the export name as the filename.
|
|
445
|
+
*
|
|
446
|
+
* @example
|
|
447
|
+
* When `exportsAsIndexes` is `true`, given this package.json configuration:
|
|
448
|
+
* ```json
|
|
449
|
+
* {
|
|
450
|
+
* "exports": {
|
|
451
|
+
* ".": "./src/entrypoint.ts",
|
|
452
|
+
* "./foo/bar": "./src/foo/bar.ts",
|
|
453
|
+
* "./foo/baz": "./src/foo/baz.ts"
|
|
454
|
+
* }
|
|
455
|
+
* }
|
|
456
|
+
* ```
|
|
457
|
+
*
|
|
458
|
+
* You would get this output file structure:
|
|
459
|
+
* ```
|
|
460
|
+
* dist/
|
|
461
|
+
* index.js
|
|
462
|
+
* foo/
|
|
463
|
+
* bar/
|
|
464
|
+
* index.js
|
|
465
|
+
* baz/
|
|
466
|
+
* index.js
|
|
467
|
+
* ```
|
|
468
|
+
*/
|
|
469
|
+
exportsAsIndexes?: boolean;
|
|
470
|
+
copyPatterns: (string | (Pick<RawCopyPattern, "from"> & Partial<Omit<RawCopyPattern, "from">>))[];
|
|
471
|
+
/** Additional plugins */
|
|
472
|
+
plugins: RsbuildPlugin[];
|
|
473
|
+
define: SourceConfig["define"];
|
|
474
|
+
/** Path to tsconfig for build (default: ./tsconfig.build.json) */
|
|
475
|
+
tsconfigPath: string | undefined;
|
|
476
|
+
/** Build targets to include (default: ["dev", "npm"]) */
|
|
477
|
+
targets?: BuildTarget[];
|
|
478
|
+
/**
|
|
479
|
+
* External dependencies that should not be bundled.
|
|
480
|
+
* These modules will be imported at runtime instead of being included in the bundle.
|
|
481
|
+
*
|
|
482
|
+
* @remarks
|
|
483
|
+
* This is useful for dependencies that are in devDependencies but needed at runtime,
|
|
484
|
+
* such as build tools that the package uses to build other packages.
|
|
485
|
+
*
|
|
486
|
+
* @example
|
|
487
|
+
* ```typescript
|
|
488
|
+
* NodeLibraryBuilder.create({
|
|
489
|
+
* externals: ['@rslib/core', '@rsbuild/core']
|
|
490
|
+
* })
|
|
491
|
+
* ```
|
|
492
|
+
*/
|
|
493
|
+
externals?: (string | RegExp)[];
|
|
494
|
+
/**
|
|
495
|
+
* Packages whose type declarations should be bundled into the output .d.ts files.
|
|
496
|
+
*
|
|
497
|
+
* @remarks
|
|
498
|
+
* By default, RSlib bundles types from packages in package.json. Use this to explicitly
|
|
499
|
+
* specify which packages (including transitive dependencies) should have their types bundled.
|
|
500
|
+
* This is particularly useful for ensuring devDependencies are fully inlined without external imports.
|
|
501
|
+
*
|
|
502
|
+
* Supports minimatch patterns (e.g., '@pnpm/**', 'picocolors')
|
|
503
|
+
*
|
|
504
|
+
* @example
|
|
505
|
+
* ```typescript
|
|
506
|
+
* NodeLibraryBuilder.create({
|
|
507
|
+
* dtsBundledPackages: ['@pnpm/lockfile.types', '@pnpm/types', 'picocolors']
|
|
508
|
+
* })
|
|
509
|
+
* ```
|
|
510
|
+
*/
|
|
511
|
+
dtsBundledPackages?: string[];
|
|
512
|
+
/**
|
|
513
|
+
* Optional callback to transform files after they're built but before the files array is finalized.
|
|
514
|
+
* Useful for copying/renaming files or adding additional files to the build output.
|
|
515
|
+
*
|
|
516
|
+
* @param context - Transform context containing compilation context and files set
|
|
517
|
+
* @param context.compilation - Rspack compilation object with assets
|
|
518
|
+
* @param context.filesArray - Set of files that will be included in package.json files field
|
|
519
|
+
* @param context.target - Current build target (dev/npm/jsr)
|
|
520
|
+
*
|
|
521
|
+
* @example
|
|
522
|
+
* ```typescript
|
|
523
|
+
* NodeLibraryBuilder.create({
|
|
524
|
+
* transformFiles({ compilation, filesArray, target }) {
|
|
525
|
+
* // Copy index.cjs to .pnpmfile.cjs
|
|
526
|
+
* const indexAsset = compilation.assets['index.cjs'];
|
|
527
|
+
* if (indexAsset) {
|
|
528
|
+
* compilation.assets['.pnpmfile.cjs'] = indexAsset;
|
|
529
|
+
* filesArray.add('.pnpmfile.cjs');
|
|
530
|
+
* }
|
|
531
|
+
* }
|
|
532
|
+
* })
|
|
533
|
+
* ```
|
|
534
|
+
*/
|
|
535
|
+
transformFiles?: (context: {
|
|
536
|
+
/** Rspack compilation object with assets */
|
|
537
|
+
compilation: {
|
|
538
|
+
assets: Record<string, unknown>;
|
|
539
|
+
};
|
|
540
|
+
filesArray: Set<string>;
|
|
541
|
+
target: BuildTarget;
|
|
542
|
+
}) => void | Promise<void>;
|
|
543
|
+
/**
|
|
544
|
+
* Optional transform function to modify package.json before it's saved.
|
|
545
|
+
* Called after all standard transformations are applied.
|
|
546
|
+
*
|
|
547
|
+
* @param context - Transform context containing the target and package.json
|
|
548
|
+
* @returns The modified package.json (mutations are also supported)
|
|
549
|
+
*
|
|
550
|
+
* @example
|
|
551
|
+
* ```typescript
|
|
552
|
+
* NodeLibraryBuilder.create({
|
|
553
|
+
* transform({ target, pkg }) {
|
|
554
|
+
* if (target === 'npm') {
|
|
555
|
+
* // Mutation approach
|
|
556
|
+
* delete pkg.devDependencies;
|
|
557
|
+
* delete pkg.scripts;
|
|
558
|
+
* }
|
|
559
|
+
* return pkg;
|
|
560
|
+
* }
|
|
561
|
+
* })
|
|
562
|
+
* ```
|
|
563
|
+
*/
|
|
564
|
+
transform?: TransformPackageJsonFn;
|
|
565
|
+
/**
|
|
566
|
+
* Options for API model generation.
|
|
567
|
+
* When enabled, generates an api.model.json file in the dist directory.
|
|
568
|
+
* Only applies when target is "npm".
|
|
569
|
+
*
|
|
570
|
+
* @remarks
|
|
571
|
+
* The generated api.model.json file contains the full API documentation
|
|
572
|
+
* in a machine-readable format for use by documentation generators.
|
|
573
|
+
* A .npmignore file is also generated to exclude the API model from npm publish.
|
|
574
|
+
*
|
|
575
|
+
* @example
|
|
576
|
+
* ```typescript
|
|
577
|
+
* // Enable API model generation with defaults
|
|
578
|
+
* NodeLibraryBuilder.create({
|
|
579
|
+
* apiModel: true,
|
|
580
|
+
* })
|
|
581
|
+
*
|
|
582
|
+
* // Enable with custom filename
|
|
583
|
+
* NodeLibraryBuilder.create({
|
|
584
|
+
* apiModel: {
|
|
585
|
+
* enabled: true,
|
|
586
|
+
* filename: "my-package.api.json",
|
|
587
|
+
* },
|
|
588
|
+
* })
|
|
589
|
+
* ```
|
|
590
|
+
*/
|
|
591
|
+
apiModel?: ApiModelOptions | boolean;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* Transforms package.json for build output and publishing.
|
|
596
|
+
*
|
|
597
|
+
* @remarks
|
|
598
|
+
* This class consolidates all package.json transformation logic including:
|
|
599
|
+
* - Path transformations (src/ -> dist/, .ts -> .js)
|
|
600
|
+
* - Export conditions generation (types, import, require)
|
|
601
|
+
* - Bin field transformations
|
|
602
|
+
* - PNPM catalog and workspace resolution
|
|
603
|
+
* - Field cleanup for publishing
|
|
604
|
+
*
|
|
605
|
+
* @example
|
|
606
|
+
* ```typescript
|
|
607
|
+
* const transformer = new PackageJsonTransformer({
|
|
608
|
+
* processTSExports: true,
|
|
609
|
+
* collapseIndex: true,
|
|
610
|
+
* });
|
|
611
|
+
*
|
|
612
|
+
* // For production builds (resolves catalog: and workspace: references)
|
|
613
|
+
* const prodPkg = await transformer.transform(packageJson, { isProduction: true });
|
|
614
|
+
*
|
|
615
|
+
* // For development builds (preserves workspace links)
|
|
616
|
+
* const devPkg = await transformer.transform(packageJson, { isProduction: false });
|
|
617
|
+
* ```
|
|
618
|
+
*
|
|
619
|
+
* @public
|
|
620
|
+
*/
|
|
621
|
+
export declare class PackageJsonTransformer {
|
|
622
|
+
private readonly options;
|
|
623
|
+
private readonly pnpmCatalog;
|
|
624
|
+
constructor(options?: PackageJsonTransformOptions);
|
|
625
|
+
/**
|
|
626
|
+
* Transforms a single export path for RSLib compatibility.
|
|
627
|
+
*
|
|
628
|
+
* @remarks
|
|
629
|
+
* Performs several transformations:
|
|
630
|
+
* 1. Strips source directory prefixes (exports/, public/, src/)
|
|
631
|
+
* 2. Converts TypeScript extensions to JavaScript
|
|
632
|
+
* 3. Preserves bin/ prefix for executables
|
|
633
|
+
*
|
|
634
|
+
* @param path - The file path to transform
|
|
635
|
+
* @returns The transformed path
|
|
636
|
+
*
|
|
637
|
+
* @example
|
|
638
|
+
* ```typescript
|
|
639
|
+
* transformer.transformExportPath("./src/index.ts"); // "./index.js"
|
|
640
|
+
* transformer.transformExportPath("./bin/cli.ts"); // "./bin/cli.js"
|
|
641
|
+
* ```
|
|
642
|
+
*/
|
|
643
|
+
transformExportPath(path: string): string;
|
|
644
|
+
/**
|
|
645
|
+
* Creates a TypeScript declaration file path from a JavaScript file path.
|
|
646
|
+
*
|
|
647
|
+
* @param jsPath - The JavaScript file path
|
|
648
|
+
* @returns The corresponding .d.ts file path
|
|
649
|
+
*
|
|
650
|
+
* @example
|
|
651
|
+
* ```typescript
|
|
652
|
+
* transformer.createTypePath("./index.js"); // "./index.d.ts"
|
|
653
|
+
* transformer.createTypePath("./rslib/index.js"); // "./rslib.d.ts" (bundled)
|
|
654
|
+
* ```
|
|
655
|
+
*/
|
|
656
|
+
createTypePath(jsPath: string): string;
|
|
657
|
+
/**
|
|
658
|
+
* Transforms package.json exports field recursively.
|
|
659
|
+
*
|
|
660
|
+
* @param exports - The exports value to transform
|
|
661
|
+
* @param exportKey - The current export key for context
|
|
662
|
+
* @returns The transformed exports value
|
|
663
|
+
*/
|
|
664
|
+
transformExports(exports: FlexibleExports, exportKey?: string): FlexibleExports;
|
|
665
|
+
/**
|
|
666
|
+
* Transforms the bin field for build output.
|
|
667
|
+
*
|
|
668
|
+
* @param bin - The bin field from package.json
|
|
669
|
+
* @returns The transformed bin field
|
|
670
|
+
*/
|
|
671
|
+
transformBin(bin: PackageJson["bin"]): PackageJson["bin"];
|
|
672
|
+
/**
|
|
673
|
+
* Performs the complete package.json transformation.
|
|
674
|
+
*
|
|
675
|
+
* @param packageJson - The source package.json
|
|
676
|
+
* @param context - Transform context
|
|
677
|
+
* @param context.isProduction - Whether this is a production build
|
|
678
|
+
* @param context.customTransform - Optional custom transform function
|
|
679
|
+
* @returns The transformed package.json
|
|
680
|
+
*/
|
|
681
|
+
transform(packageJson: PackageJson, context?: {
|
|
682
|
+
isProduction?: boolean;
|
|
683
|
+
customTransform?: (pkg: PackageJson) => PackageJson;
|
|
684
|
+
}): Promise<PackageJson>;
|
|
685
|
+
private applyPnpmTransformations;
|
|
686
|
+
/**
|
|
687
|
+
* Applies RSLib-specific transformations.
|
|
688
|
+
*/
|
|
689
|
+
private applyRslibTransformations;
|
|
690
|
+
/**
|
|
691
|
+
* Transforms a string export value.
|
|
692
|
+
*/
|
|
693
|
+
private transformStringExport;
|
|
694
|
+
/**
|
|
695
|
+
* Transforms an object export value.
|
|
696
|
+
*/
|
|
697
|
+
private transformObjectExports;
|
|
698
|
+
/**
|
|
699
|
+
* Determines if an export object represents export conditions.
|
|
700
|
+
*/
|
|
701
|
+
private isConditionsObject;
|
|
702
|
+
/**
|
|
703
|
+
* Transforms a single export entry.
|
|
704
|
+
*/
|
|
705
|
+
private transformExportEntry;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
/**
|
|
709
|
+
* Options for transforming package.json.
|
|
710
|
+
* @public
|
|
711
|
+
*/
|
|
712
|
+
export declare interface PackageJsonTransformOptions {
|
|
713
|
+
/**
|
|
714
|
+
* Whether to process TypeScript exports (convert .ts to .js and add types field).
|
|
715
|
+
* @defaultValue true
|
|
716
|
+
*/
|
|
717
|
+
processTSExports?: boolean;
|
|
718
|
+
/**
|
|
719
|
+
* Whether to collapse index files (./foo/index.ts -> ./foo.js).
|
|
720
|
+
* This is typically true for bundled builds.
|
|
721
|
+
* @defaultValue false
|
|
722
|
+
*/
|
|
723
|
+
collapseIndex?: boolean;
|
|
724
|
+
/**
|
|
725
|
+
* Map of export paths to entry files (from AutoEntryPlugin).
|
|
726
|
+
*/
|
|
727
|
+
entrypoints?: Map<string, string>;
|
|
728
|
+
/**
|
|
729
|
+
* Map of export paths to output files (for exportsAsIndexes mode).
|
|
730
|
+
*/
|
|
731
|
+
exportToOutputMap?: Map<string, string>;
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Plugin to process package.json for distribution
|
|
736
|
+
* @public
|
|
737
|
+
*/
|
|
738
|
+
export declare const PackageJsonTransformPlugin: (options?: PackageJsonTransformPluginOptions) => RsbuildPlugin;
|
|
739
|
+
|
|
740
|
+
/**
|
|
741
|
+
* @public
|
|
742
|
+
*/
|
|
743
|
+
export declare interface PackageJsonTransformPluginOptions {
|
|
744
|
+
/** Override the name property of the source package.json when building to a target */
|
|
745
|
+
name?: string | true;
|
|
746
|
+
/** Whether to force include private packages (with "private": true) in the output */
|
|
747
|
+
forcePrivate?: boolean;
|
|
748
|
+
/** Whether to process package.json exports of into */
|
|
749
|
+
processTSExports?: boolean;
|
|
750
|
+
/** Whether the build is in bundle mode (affects export path transformation) */
|
|
751
|
+
bundle?: boolean;
|
|
752
|
+
/** Build target (dev, npm) - used for custom transformations */
|
|
753
|
+
target?: string;
|
|
754
|
+
/** Optional transform function to modify package.json after standard transformations */
|
|
755
|
+
transform?: (pkg: PackageJson) => PackageJson;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* Manages PNPM catalog resolution with caching.
|
|
760
|
+
*
|
|
761
|
+
* @remarks
|
|
762
|
+
* This class handles the resolution of PNPM-specific dependency references:
|
|
763
|
+
* - `catalog:` references to centralized version definitions
|
|
764
|
+
* - `workspace:` references to local workspace packages
|
|
765
|
+
*
|
|
766
|
+
* The class caches the catalog data based on file modification time to avoid
|
|
767
|
+
* repeated filesystem operations during builds.
|
|
768
|
+
*
|
|
769
|
+
* @example
|
|
770
|
+
* ```typescript
|
|
771
|
+
* const catalog = new PnpmCatalog();
|
|
772
|
+
*
|
|
773
|
+
* // Get the catalog data
|
|
774
|
+
* const versions = await catalog.getCatalog();
|
|
775
|
+
* console.log(versions);
|
|
776
|
+
* // { "react": "^18.2.0", "typescript": "^5.0.0" }
|
|
777
|
+
*
|
|
778
|
+
* // Resolve package.json dependencies
|
|
779
|
+
* const resolved = await catalog.resolvePackageJson(packageJson);
|
|
780
|
+
* ```
|
|
781
|
+
*
|
|
782
|
+
* @public
|
|
783
|
+
*/
|
|
784
|
+
export declare class PnpmCatalog {
|
|
785
|
+
private catalogCache;
|
|
786
|
+
private catalogCacheMtime;
|
|
787
|
+
private cachedWorkspaceRoot;
|
|
788
|
+
/**
|
|
789
|
+
* Clears the cached catalog data.
|
|
790
|
+
*
|
|
791
|
+
* @remarks
|
|
792
|
+
* Useful in testing scenarios to ensure clean state between tests.
|
|
793
|
+
*/
|
|
794
|
+
clearCache(): void;
|
|
795
|
+
/**
|
|
796
|
+
* Gets the PNPM catalog from pnpm-workspace.yaml.
|
|
797
|
+
*
|
|
798
|
+
* @remarks
|
|
799
|
+
* The catalog is cached based on file modification time. If the file hasn't
|
|
800
|
+
* changed since the last read, the cached version is returned.
|
|
801
|
+
*
|
|
802
|
+
* @returns The catalog mapping dependency names to versions
|
|
803
|
+
*/
|
|
804
|
+
getCatalog(): Promise<Record<string, string>>;
|
|
805
|
+
/**
|
|
806
|
+
* Resolves catalog: and workspace: references in a package.json.
|
|
807
|
+
*
|
|
808
|
+
* @param packageJson - The package.json to resolve
|
|
809
|
+
* @param dir - The directory containing the package (defaults to cwd)
|
|
810
|
+
* @returns The resolved package.json
|
|
811
|
+
*
|
|
812
|
+
* @throws {Error} When resolution fails for critical dependencies
|
|
813
|
+
*/
|
|
814
|
+
resolvePackageJson(packageJson: PackageJson, dir?: string): Promise<PackageJson>;
|
|
815
|
+
/**
|
|
816
|
+
* Collects dependencies with a specific prefix (catalog: or workspace:).
|
|
817
|
+
*/
|
|
818
|
+
private collectDependencies;
|
|
819
|
+
/**
|
|
820
|
+
* Logs resolved dependencies in a formatted way.
|
|
821
|
+
*/
|
|
822
|
+
private logResolvedDependencies;
|
|
823
|
+
/**
|
|
824
|
+
* Validates that no unresolved catalog: or workspace: references remain.
|
|
825
|
+
*/
|
|
826
|
+
private validateNoUnresolvedReferences;
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
/**
|
|
830
|
+
* Async RSLib configuration function type.
|
|
831
|
+
* @public
|
|
832
|
+
*/
|
|
833
|
+
export declare type RslibConfigAsyncFn = (env: ConfigParams) => Promise<RslibConfig>;
|
|
834
|
+
|
|
835
|
+
/**
|
|
836
|
+
* Strips sourceMappingURL comment from declaration file content.
|
|
837
|
+
* This removes comments like: `//# source` + `MappingURL=index.d.ts.map`
|
|
838
|
+
*
|
|
839
|
+
* @remarks
|
|
840
|
+
* Source maps are preserved in the temp directory for API Extractor documentation
|
|
841
|
+
* but are excluded from the final dist output to reduce package size.
|
|
842
|
+
*
|
|
843
|
+
* @public
|
|
844
|
+
*/
|
|
845
|
+
export declare function stripSourceMapComment(content: string): string;
|
|
846
|
+
|
|
847
|
+
/**
|
|
848
|
+
* @public
|
|
849
|
+
*/
|
|
850
|
+
export declare type TransformPackageJsonFn = (context: {
|
|
851
|
+
target: BuildTarget;
|
|
852
|
+
pkg: PackageJson;
|
|
853
|
+
}) => PackageJson;
|
|
854
|
+
|
|
855
|
+
export { }
|