Fast Rust-based web bundler with webpack-compatible API
npx @tessl/cli install tessl/npm-rspack--core@1.5.00
# Rspack Core
1
2
Rspack is a high-performance JavaScript bundler built in Rust that offers strong compatibility with the webpack ecosystem. It serves as a drop-in replacement for webpack with significantly faster build speeds, featuring lightning-fast Hot Module Replacement (HMR), built-in incremental compilation, and first-class Module Federation support.
3
4
## Package Information
5
6
- **Package Name**: @rspack/core
7
- **Package Type**: npm
8
- **Language**: JavaScript/TypeScript with Rust bindings
9
- **Installation**: `npm install @rspack/core`
10
11
## Core Imports
12
13
```typescript
14
import rspack from "@rspack/core";
15
import { Compiler, Compilation, Configuration } from "@rspack/core";
16
```
17
18
For CommonJS:
19
20
```javascript
21
const rspack = require("@rspack/core");
22
const { Compiler, Compilation } = require("@rspack/core");
23
```
24
25
## Basic Usage
26
27
```typescript
28
import rspack from "@rspack/core";
29
30
// Simple build configuration
31
const config = {
32
entry: "./src/index.js",
33
output: {
34
path: __dirname + "/dist",
35
filename: "bundle.js",
36
},
37
mode: "production",
38
};
39
40
// Create and run compiler
41
const compiler = rspack(config);
42
compiler.run((err, stats) => {
43
if (err) {
44
console.error(err);
45
return;
46
}
47
console.log(stats?.toString());
48
});
49
```
50
51
## Architecture
52
53
Rspack is built around several key components:
54
55
- **Compiler**: Main compilation controller that manages the build process and hooks system
56
- **Compilation**: Individual build process instance with assets, modules, and chunks
57
- **Configuration System**: Comprehensive options for build behavior, optimization, and plugins
58
- **Plugin System**: Tapable-based hooks allowing deep customization of the build process
59
- **Module System**: Module resolution, loading, and transformation pipeline
60
- **Hot Module Replacement**: Development-time module swapping without full page reload
61
- **Module Federation**: Micro-frontend architecture for sharing code between applications
62
63
## Capabilities
64
65
### Core Bundling
66
67
Main bundling functions and compiler creation for building JavaScript applications.
68
69
```typescript { .api }
70
function rspack(options: Configuration): Compiler;
71
function rspack(options: Configuration[]): MultiCompiler;
72
function rspack(
73
options: Configuration | Configuration[],
74
callback: (err: Error | null, stats?: Stats | MultiStats) => void
75
): Compiler | MultiCompiler | null;
76
77
function createCompiler(options: Configuration): Compiler;
78
function createMultiCompiler(options: Configuration[]): MultiCompiler;
79
```
80
81
[Core Bundling](./core-bundling.md)
82
83
### Configuration
84
85
Comprehensive configuration options covering entry points, output, optimization, plugins, and development settings.
86
87
```typescript { .api }
88
interface Configuration {
89
entry?: Entry;
90
output?: Output;
91
mode?: Mode;
92
target?: Target;
93
module?: ModuleOptions;
94
resolve?: ResolveOptions;
95
plugins?: Plugin[];
96
optimization?: Optimization;
97
experiments?: Experiments;
98
devServer?: DevServer;
99
devtool?: DevTool;
100
externals?: Externals;
101
stats?: StatsValue;
102
}
103
104
type Mode = "development" | "production" | "none";
105
type Entry = string | string[] | EntryObject | EntryFunction;
106
```
107
108
[Configuration](./configuration.md)
109
110
### Plugin System
111
112
Built-in plugins for optimization, development, asset processing, and build customization.
113
114
```typescript { .api }
115
interface RspackPluginInstance {
116
apply(compiler: Compiler): void;
117
}
118
119
type RspackPluginFunction = (this: Compiler, compiler: Compiler) => void;
120
type Plugin = RspackPluginInstance | RspackPluginFunction | Falsy;
121
122
// Core plugins
123
class DefinePlugin extends RspackBuiltinPlugin {
124
constructor(options: DefinePluginOptions);
125
}
126
127
class HotModuleReplacementPlugin extends RspackBuiltinPlugin {}
128
class ProvidePlugin extends RspackBuiltinPlugin {
129
constructor(options: ProvidePluginOptions);
130
}
131
```
132
133
[Plugin System](./plugins.md)
134
135
### Hot Module Replacement
136
137
Development-time module replacement system for fast iteration without losing application state.
138
139
```typescript { .api }
140
interface Hot {
141
accept(
142
modules?: string | string[],
143
callback?: (outdatedDependencies: string[]) => void,
144
errorHandler?: (err: Error, context: any) => void
145
): void;
146
decline(module?: string | string[]): void;
147
dispose(callback: (data: any) => void): void;
148
status(): HotUpdateStatus;
149
check(autoApply?: boolean): Promise<(string | number)[] | null>;
150
apply(options?: ApplyOptions): Promise<(string | number)[] | null>;
151
data: any;
152
}
153
154
type HotUpdateStatus = "idle" | "check" | "prepare" | "ready" | "dispose" | "apply" | "abort" | "fail";
155
```
156
157
[Hot Module Replacement](./hmr.md)
158
159
### Module Federation
160
161
Micro-frontend architecture enabling code sharing between independent applications at runtime.
162
163
```typescript { .api }
164
class ModuleFederationPlugin extends RspackBuiltinPlugin {
165
constructor(options: ModuleFederationPluginOptions);
166
}
167
168
interface ModuleFederationPluginOptions {
169
name: string;
170
filename?: string;
171
exposes?: Exposes;
172
remotes?: Remotes;
173
shared?: Shared;
174
runtimePlugins?: string[];
175
shareStrategy?: "version-first" | "loaded-first";
176
}
177
178
class ContainerPlugin extends RspackBuiltinPlugin {
179
constructor(options: ContainerPluginOptions);
180
}
181
182
class ContainerReferencePlugin extends RspackBuiltinPlugin {
183
constructor(options: ContainerReferencePluginOptions);
184
}
185
```
186
187
[Module Federation](./module-federation.md)
188
189
### Loaders
190
191
Built-in loaders for transforming modules during the build process.
192
193
```typescript { .api }
194
interface SwcLoaderOptions {
195
jsc?: SwcLoaderJscConfig;
196
module?: SwcLoaderModuleConfig;
197
env?: SwcLoaderEnvConfig;
198
isModule?: boolean;
199
rspackExperiments?: {
200
import?: any[];
201
collectTypeScriptInfo?: boolean;
202
};
203
}
204
205
interface LightningcssLoaderOptions {
206
minify?: boolean;
207
targets?: any;
208
include?: number;
209
exclude?: number;
210
drafts?: any;
211
nonStandard?: any;
212
pseudoClasses?: any;
213
}
214
```
215
216
[Loaders](./loaders.md)
217
218
## Types
219
220
### Core Types
221
222
```typescript { .api }
223
class Compiler {
224
options: Configuration;
225
hooks: CompilerHooks;
226
outputPath: string;
227
name?: string;
228
229
run(callback: (err: Error | null, stats?: Stats) => void): void;
230
watch(
231
options: WatchOptions,
232
callback: (err: Error | null, stats?: Stats) => void
233
): Watching;
234
close(callback: () => void): void;
235
}
236
237
class Compilation {
238
assets: Record<string, Asset>;
239
chunks: Set<Chunk>;
240
modules: Set<Module>;
241
moduleGraph: ModuleGraph;
242
hooks: CompilationHooks;
243
}
244
245
class MultiCompiler {
246
compilers: Compiler[];
247
hooks: MultiCompilerHooks;
248
249
run(callback: (err: Error | null, stats?: MultiStats) => void): void;
250
watch(
251
options: WatchOptions | WatchOptions[],
252
callback: (err: Error | null, stats?: MultiStats) => void
253
): MultiWatching;
254
}
255
256
class Stats {
257
compilation: Compilation;
258
toString(options?: StatsOptions): string;
259
toJson(options?: StatsOptions): StatsCompilation;
260
}
261
```
262
263
### Asset and Module Types
264
265
```typescript { .api }
266
interface Asset {
267
source(): string | Buffer;
268
size(): number;
269
info?: AssetInfo;
270
}
271
272
interface AssetInfo {
273
immutable?: boolean;
274
minimized?: boolean;
275
development?: boolean;
276
hotModuleReplacement?: boolean;
277
sourceFilename?: string;
278
javascriptModule?: boolean;
279
}
280
281
class Module {
282
type: string;
283
context?: string;
284
resource?: string;
285
request?: string;
286
}
287
288
class Chunk {
289
name?: string;
290
id?: string | number;
291
ids: (string | number)[];
292
files: Set<string>;
293
runtime: Set<string>;
294
}
295
```
296
297
### Error Types
298
299
```typescript { .api }
300
class RspackError extends Error {
301
name: string;
302
message: string;
303
stack?: string;
304
}
305
306
class ValidationError extends Error {
307
name: "ValidationError";
308
errors: string[];
309
}
310
311
type RspackSeverity = "error" | "warning";
312
```