0
# Rslib Core
1
2
Rslib is a comprehensive library development tool built on top of Rsbuild that enables developers to create high-quality JavaScript and TypeScript libraries with minimal configuration. It provides out-of-the-box build capabilities supporting multiple output formats (ESM, CJS, UMD), compilation of diverse languages, declaration file generation, and advanced features like Module Federation.
3
4
## Package Information
5
6
- **Package Name**: @rslib/core
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install @rslib/core`
10
- **Node.js**: >=18.12.0
11
- **Homepage**: https://rslib.rs
12
13
## Core Imports
14
15
```typescript
16
import { build, defineConfig, loadConfig, logger } from "@rslib/core";
17
import type { RslibConfig, LibConfig } from "@rslib/core";
18
```
19
20
For CommonJS:
21
22
```javascript
23
const { build, defineConfig, loadConfig, logger } = require("@rslib/core");
24
```
25
26
Re-exported Rsbuild APIs:
27
28
```typescript
29
import { rsbuild, rspack } from "@rslib/core";
30
// Or access specific Rsbuild functionality
31
import type { Rspack } from "@rslib/core";
32
```
33
34
## Basic Usage
35
36
```typescript
37
import { defineConfig, build, loadConfig } from "@rslib/core";
38
39
// Define configuration
40
export default defineConfig({
41
lib: [
42
{
43
format: "esm",
44
dts: true,
45
},
46
{
47
format: "cjs",
48
}
49
]
50
});
51
52
// Load and build programmatically
53
const { content: config } = await loadConfig();
54
const rsbuildInstance = await build(config);
55
```
56
57
## Architecture
58
59
Rslib is built around several key components:
60
61
- **Build System**: Core compilation and bundling functionality built on Rsbuild
62
- **Configuration System**: Type-safe configuration with multiple format support and file loading
63
- **CLI Interface**: Command-line tools for development workflow (`build`, `dev`, `inspect`, etc.)
64
- **Plugin System**: Integration with Rsbuild's plugin ecosystem
65
- **Format Support**: Multiple output formats (ESM, CJS, UMD, Module Federation, IIFE)
66
- **TypeScript Integration**: Full TypeScript support with declaration file generation
67
68
## Capabilities
69
70
### Core Build Functions
71
72
Primary build functionality for compiling library projects using Rsbuild as the underlying build system.
73
74
```typescript { .api }
75
function build(config: RslibConfig, options?: BuildOptions): Promise<RsbuildInstance>;
76
77
interface BuildOptions extends CommonOptions {
78
watch?: boolean;
79
}
80
```
81
82
[Build System](./build.md)
83
84
### Configuration Management
85
86
Type-safe configuration definition and loading system with support for both static and function-based configurations.
87
88
```typescript { .api }
89
function defineConfig(config: RslibConfig): RslibConfig;
90
function defineConfig(config: RslibConfigSyncFn): RslibConfigSyncFn;
91
function defineConfig(config: RslibConfigAsyncFn): RslibConfigAsyncFn;
92
93
function loadConfig(options?: LoadConfigOptions): Promise<{
94
content: RslibConfig;
95
filePath: string;
96
}>;
97
98
interface LoadConfigOptions {
99
cwd?: string;
100
path?: string;
101
envMode?: string;
102
}
103
104
interface RslibConfig extends RsbuildConfig {
105
lib: LibConfig[];
106
output?: RslibOutputConfig;
107
}
108
```
109
110
[Configuration](./configuration.md)
111
112
### CLI Operations
113
114
Command-line interface providing development workflow tools including build, development server, inspection, and Module Federation support.
115
116
```typescript { .api }
117
function runCli(): void;
118
function inspect(config: RslibConfig, options?: InspectOptions): Promise<RsbuildInstance>;
119
function startMFDevServer(config: RslibConfig, options?: CommonOptions): Promise<RsbuildInstance | undefined>;
120
function prepareCli(): void;
121
```
122
123
[CLI Operations](./cli.md)
124
125
## Common Types
126
127
```typescript { .api }
128
interface CommonOptions {
129
root?: string;
130
config?: string;
131
envDir?: string;
132
envMode?: string;
133
lib?: string[];
134
}
135
136
interface LibConfig extends EnvironmentConfig {
137
id?: string;
138
format?: Format;
139
bundle?: boolean;
140
autoExtension?: boolean;
141
autoExternal?: AutoExternal;
142
syntax?: Syntax;
143
dts?: Dts;
144
shims?: Shims;
145
redirect?: Redirect;
146
umdName?: Rspack.LibraryName;
147
banner?: BannerAndFooter;
148
footer?: BannerAndFooter;
149
}
150
151
type Format = 'esm' | 'cjs' | 'umd' | 'mf' | 'iife';
152
```
153
154
## Utilities
155
156
```typescript { .api }
157
const logger: Logger;
158
const version: string;
159
```
160
161
## Re-exported APIs
162
163
Rslib re-exports the complete Rsbuild API for advanced usage:
164
165
```typescript { .api }
166
// Complete Rsbuild namespace
167
import { rsbuild } from "@rslib/core";
168
169
// Rspack types and functionality
170
import { rspack, type Rspack } from "@rslib/core";
171
172
// Re-exported Rsbuild types (available from @rslib/core)
173
interface RsbuildInstance {
174
// Rsbuild instance methods
175
build(options?: { watch?: boolean }): Promise<{ close(): Promise<void> }>;
176
// Additional Rsbuild methods available
177
}
178
179
interface RsbuildConfig {
180
// Base Rsbuild configuration options
181
mode?: 'development' | 'production';
182
root?: string;
183
plugins?: RsbuildPlugin[];
184
dev?: any;
185
server?: any;
186
environments?: Record<string, EnvironmentConfig>;
187
}
188
189
interface EnvironmentConfig {
190
// Rsbuild environment configuration
191
source?: any;
192
output?: any;
193
tools?: any;
194
[key: string]: any;
195
}
196
197
type RsbuildMode = 'development' | 'production';
198
```