@rsbuild/core 1.2.7 → 1.2.8

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.
@@ -4,7 +4,7 @@ import type { ChainIdentifier } from '../configChain';
4
4
  import type { ModifyRspackConfigUtils, NormalizedConfig, NormalizedEnvironmentConfig, RsbuildConfig } from './config';
5
5
  import type { RsbuildContext } from './context';
6
6
  import type { EnvironmentContext, ModifyBundlerChainFn, ModifyChainUtils, ModifyEnvironmentConfigFn, ModifyHTMLTagsFn, ModifyRsbuildConfigFn, OnAfterBuildFn, OnAfterCreateCompilerFn, OnAfterEnvironmentCompileFn, OnAfterStartDevServerFn, OnAfterStartProdServerFn, OnBeforeBuildFn, OnBeforeCreateCompilerFn, OnBeforeEnvironmentCompileFn, OnBeforeStartDevServerFn, OnBeforeStartProdServerFn, OnCloseBuildFn, OnCloseDevServerFn, OnDevCompileDoneFn, OnExitFn } from './hooks';
7
- import type { RsbuildTarget } from './rsbuild';
7
+ import type { RsbuildInstance, RsbuildTarget } from './rsbuild';
8
8
  import type { Rspack } from './rspack';
9
9
  import type { HtmlRspackPlugin } from './thirdParty';
10
10
  import type { Falsy } from './utils';
@@ -63,42 +63,7 @@ export type PluginMeta = {
63
63
  environment: string;
64
64
  instance: RsbuildPlugin;
65
65
  };
66
- export type PluginManager = {
67
- getPlugins: (options?: {
68
- /**
69
- * Get the plugins in the specified environment.
70
- *
71
- * If environment is not specified, get the global plugins.
72
- */
73
- environment: string;
74
- }) => RsbuildPlugin[];
75
- addPlugins: (plugins: Array<RsbuildPlugin | Falsy>, options?: {
76
- /**
77
- * Insert before the specified plugin.
78
- */
79
- before?: string;
80
- /**
81
- * Add a plugin for the specified environment.
82
- * If environment is not specified, it will be registered as a global plugin (effective in all environments)
83
- */
84
- environment?: string;
85
- }) => void;
86
- removePlugins: (pluginNames: string[], options?: {
87
- /**
88
- * Remove the plugin in the specified environment.
89
- *
90
- * If environment is not specified, remove it in all environments.
91
- */
92
- environment: string;
93
- }) => void;
94
- isPluginExists: (pluginName: string, options?: {
95
- /**
96
- * Whether it exists in the specified environment.
97
- *
98
- * If environment is not specified, determine whether the plugin is a global plugin.
99
- */
100
- environment: string;
101
- }) => boolean;
66
+ export type PluginManager = Pick<RsbuildInstance, 'getPlugins' | 'addPlugins' | 'isPluginExists' | 'removePlugins'> & {
102
67
  /** Get all plugins with environment info */
103
68
  getAllPluginsWithMeta: () => PluginMeta[];
104
69
  };
@@ -300,50 +265,157 @@ declare function getNormalizedConfig(options: {
300
265
  * Define a generic Rsbuild plugin API that provider can extend as needed.
301
266
  */
302
267
  export type RsbuildPluginAPI = Readonly<{
268
+ /**
269
+ * A read-only object that provides some context information.
270
+ */
303
271
  context: Readonly<RsbuildContext>;
304
- isPluginExists: PluginManager['isPluginExists'];
305
- onExit: PluginHook<OnExitFn>;
306
- onCloseBuild: PluginHook<OnCloseBuildFn>;
272
+ /**
273
+ * Explicitly expose some properties or methods of the current plugin,
274
+ * and other plugins can get these APIs through `api.useExposed`.
275
+ */
276
+ expose: <T = any>(id: string | symbol, api: T) => void;
277
+ /**
278
+ * Get the Rsbuild config, this method must be called after the
279
+ * `modifyRsbuildConfig` hook is executed.
280
+ */
281
+ getRsbuildConfig: GetRsbuildConfig;
282
+ /**
283
+ * Get the all normalized Rsbuild config or the Rsbuild config of a specified
284
+ * environment, this method must be called after the
285
+ * `modifyRsbuildConfig` hook is executed.
286
+ */
287
+ getNormalizedConfig: typeof getNormalizedConfig;
288
+ /**
289
+ * Determines if a plugin has been registered in the current Rsbuild instance.
290
+ */
291
+ isPluginExists: (pluginName: string, options?: {
292
+ /**
293
+ * Whether it exists in the specified environment.
294
+ * If environment is not specified, determine whether the plugin is a global plugin.
295
+ */
296
+ environment: string;
297
+ }) => boolean;
298
+ /**
299
+ * Allows you to modify the Rspack configuration using the `rspack-chain` API,
300
+ * providing the same functionality as `tools.bundlerChain`.
301
+ */
302
+ modifyBundlerChain: PluginHook<ModifyBundlerChainFn>;
303
+ /**
304
+ * Modify the Rsbuild configuration of a specific environment.
305
+ */
306
+ modifyEnvironmentConfig: PluginHook<ModifyEnvironmentConfigFn>;
307
+ /**
308
+ * Modify the tags that are injected into the HTML.
309
+ */
310
+ modifyHTMLTags: PluginHook<ModifyHTMLTagsFn>;
311
+ /**
312
+ * To modify the Rspack config, you can directly modify the config object,
313
+ * or return a new object to replace the previous object.
314
+ */
315
+ modifyRspackConfig: PluginHook<ModifyRspackConfigFn>;
316
+ /**
317
+ * Modify the config passed to the Rsbuild, you can directly modify the config object,
318
+ * or return a new object to replace the previous object.
319
+ */
320
+ modifyRsbuildConfig: PluginHook<ModifyRsbuildConfigFn>;
321
+ /**
322
+ * Allows you to modify the webpack configuration using the `rspack-chain` API,
323
+ * providing the same functionality as `tools.bundlerChain`.
324
+ */
325
+ modifyWebpackChain: PluginHook<ModifyWebpackChainFn>;
326
+ /**
327
+ * To modify the webpack config, you can directly modify the config object,
328
+ * or return a new object to replace the previous object.
329
+ */
330
+ modifyWebpackConfig: PluginHook<ModifyWebpackConfigFn>;
331
+ /**
332
+ * A callback function that is triggered after running the production build.
333
+ * You can access the build result information via the
334
+ * [stats](https://rspack.dev/api/javascript-api/stats) parameter.
335
+ */
307
336
  onAfterBuild: PluginHook<OnAfterBuildFn>;
308
- onBeforeBuild: PluginHook<OnBeforeBuildFn>;
337
+ /**
338
+ * A callback function that is triggered after the compiler instance has been
339
+ * created, but before the build process. This hook is called when you run
340
+ * `rsbuild.startDevServer`, `rsbuild.build`, or `rsbuild.createCompiler`.
341
+ */
342
+ onAfterCreateCompiler: PluginHook<OnAfterCreateCompilerFn>;
343
+ /**
344
+ * A callback function that is triggered after the compilation of a single environment.
345
+ * You can access the build result information via the
346
+ * [stats](https://rspack.dev/api/javascript-api/stats) parameter.
347
+ */
309
348
  onAfterEnvironmentCompile: PluginHook<OnAfterEnvironmentCompileFn>;
310
- onBeforeEnvironmentCompile: PluginHook<OnBeforeEnvironmentCompileFn>;
311
- onCloseDevServer: PluginHook<OnCloseDevServerFn>;
312
- onDevCompileDone: PluginHook<OnDevCompileDoneFn>;
349
+ /**
350
+ * Called after starting the dev server, you can get the port number with the
351
+ * `port` parameter, and the page routes info with the `routes` parameter.
352
+ */
313
353
  onAfterStartDevServer: PluginHook<OnAfterStartDevServerFn>;
314
- onBeforeStartDevServer: PluginHook<OnBeforeStartDevServerFn>;
354
+ /**
355
+ * Called after starting the production preview server, you can get the port
356
+ * number with the `port` parameter, and the page routes info with the
357
+ * `routes` parameter.
358
+ */
315
359
  onAfterStartProdServer: PluginHook<OnAfterStartProdServerFn>;
316
- onBeforeStartProdServer: PluginHook<OnBeforeStartProdServerFn>;
317
- onAfterCreateCompiler: PluginHook<OnAfterCreateCompilerFn>;
360
+ /**
361
+ * A callback function that is triggered before the production build is executed.
362
+ */
363
+ onBeforeBuild: PluginHook<OnBeforeBuildFn>;
364
+ /**
365
+ * A callback function that is triggered after the Compiler instance has been
366
+ * created, but before the build process begins. This hook is called when you
367
+ * run `rsbuild.startDevServer`, `rsbuild.build`, or `rsbuild.createCompiler`.
368
+ */
318
369
  onBeforeCreateCompiler: PluginHook<OnBeforeCreateCompilerFn>;
319
- modifyHTMLTags: PluginHook<ModifyHTMLTagsFn>;
320
- modifyRsbuildConfig: PluginHook<ModifyRsbuildConfigFn>;
321
- modifyEnvironmentConfig: PluginHook<ModifyEnvironmentConfigFn>;
322
- modifyBundlerChain: PluginHook<ModifyBundlerChainFn>;
323
- /** Only works when bundler is Rspack */
324
- modifyRspackConfig: PluginHook<ModifyRspackConfigFn>;
325
- /** Only works when bundler is webpack */
326
- modifyWebpackChain: PluginHook<ModifyWebpackChainFn>;
327
- /** Only works when bundler is webpack */
328
- modifyWebpackConfig: PluginHook<ModifyWebpackConfigFn>;
329
- getRsbuildConfig: GetRsbuildConfig;
330
- getNormalizedConfig: typeof getNormalizedConfig;
331
370
  /**
332
- * For plugin communication
371
+ * A callback function that is triggered before the compilation of a single environment.
333
372
  */
334
- expose: <T = any>(id: string | symbol, api: T) => void;
335
- useExposed: <T = any>(id: string | symbol) => T | undefined;
373
+ onBeforeEnvironmentCompile: PluginHook<OnBeforeEnvironmentCompileFn>;
336
374
  /**
337
- * Used to transform the code of modules.
375
+ * Called before starting the dev server.
338
376
  */
339
- transform: TransformFn;
377
+ onBeforeStartDevServer: PluginHook<OnBeforeStartDevServerFn>;
378
+ /**
379
+ * Called before starting the production preview server.
380
+ */
381
+ onBeforeStartProdServer: PluginHook<OnBeforeStartProdServerFn>;
382
+ /**
383
+ * Called when closing the build instance. Can be used to perform cleanup
384
+ * operations when the building is closed.
385
+ */
386
+ onCloseBuild: PluginHook<OnCloseBuildFn>;
387
+ /**
388
+ * Called when closing the dev server. Can be used to perform cleanup
389
+ * operations when the dev server is closed.
390
+ */
391
+ onCloseDevServer: PluginHook<OnCloseDevServerFn>;
392
+ /**
393
+ * Called after each development mode build, you can use `isFirstCompile`
394
+ * to determine whether it is the first build.
395
+ */
396
+ onDevCompileDone: PluginHook<OnDevCompileDoneFn>;
397
+ /**
398
+ * Called when the process is going to exit, this hook can only execute
399
+ * synchronous code.
400
+ */
401
+ onExit: PluginHook<OnExitFn>;
340
402
  /**
341
- * Process all the asset generated by Rspack.
403
+ * Modify assets before emitting, the same as Rspack's
404
+ * [compilation.hooks.processAssets](https://rspack.dev/api/plugin-api/compilation-hooks#processassets) hook.
342
405
  */
343
406
  processAssets: ProcessAssetsFn;
344
407
  /**
345
- * Defines a custom resolver. A resolver can be useful for e.g. locating third-party dependencies.
408
+ * Intercept and modify module request information before module resolution begins.
409
+ * The same as Rspack's [normalModuleFactory.hooks.resolve](https://rspack.dev/api/plugin-api/normal-module-factory-hooks#resolve) hook.
346
410
  */
347
411
  resolve: ResolveFn;
412
+ /**
413
+ * Used to transform the code of modules.
414
+ */
415
+ transform: TransformFn;
416
+ /**
417
+ * Get the properties or methods exposed by other plugins.
418
+ */
419
+ useExposed: <T = any>(id: string | symbol) => T | undefined;
348
420
  }>;
349
421
  export {};
@@ -4,10 +4,11 @@ import type { RsbuildDevServer } from '../server/devServer';
4
4
  import type { StartServerResult } from '../server/helper';
5
5
  import type { RsbuildConfig } from './config';
6
6
  import type { NormalizedConfig, NormalizedEnvironmentConfig } from './config';
7
- import type { InternalContext, RsbuildContext } from './context';
8
- import type { PluginManager, RsbuildPluginAPI } from './plugin';
7
+ import type { InternalContext } from './context';
8
+ import type { PluginManager, RsbuildPlugin, RsbuildPluginAPI } from './plugin';
9
9
  import type { Rspack } from './rspack';
10
10
  import type { WebpackConfig } from './thirdParty';
11
+ import type { Falsy } from './utils';
11
12
  export type Bundler = 'rspack' | 'webpack';
12
13
  export type StartDevServerOptions = {
13
14
  /**
@@ -23,7 +24,6 @@ export type StartDevServerOptions = {
23
24
  export type CreateDevServerOptions = StartDevServerOptions & {
24
25
  /**
25
26
  * Whether to trigger Rsbuild compilation
26
- *
27
27
  * @default true
28
28
  */
29
29
  runCompile?: boolean;
@@ -118,33 +118,100 @@ export type RsbuildProvider<B extends 'rspack' | 'webpack' = 'rspack'> = (option
118
118
  helpers: RsbuildProviderHelpers;
119
119
  }) => Promise<ProviderInstance<B>>;
120
120
  export type RsbuildInstance = {
121
- context: RsbuildContext;
122
- addPlugins: PluginManager['addPlugins'];
123
- getPlugins: PluginManager['getPlugins'];
124
- removePlugins: PluginManager['removePlugins'];
125
- isPluginExists: PluginManager['isPluginExists'];
126
- build: ProviderInstance['build'];
121
+ /**
122
+ * Register one or more Rsbuild plugins, which can be called multiple times.
123
+ * This method needs to be called before compiling. If it is called after
124
+ * compiling, it will not affect the compilation result.
125
+ */
126
+ addPlugins: (plugins: Array<RsbuildPlugin | Falsy>, options?: {
127
+ /**
128
+ * Insert before the specified plugin.
129
+ */
130
+ before?: string;
131
+ /**
132
+ * Add a plugin for the specified environment.
133
+ * If environment is not specified, it will be registered as a global plugin (effective in all environments)
134
+ */
135
+ environment?: string;
136
+ }) => void;
137
+ /**
138
+ * Get all the Rsbuild plugins registered in the current Rsbuild instance.
139
+ */
140
+ getPlugins: (options?: {
141
+ /**
142
+ * Get the plugins in the specified environment.
143
+ * If environment is not specified, get the global plugins.
144
+ */
145
+ environment: string;
146
+ }) => RsbuildPlugin[];
147
+ /**
148
+ * Removes one or more Rsbuild plugins, which can be called multiple times.
149
+ * This method needs to be called before compiling. If it is called after
150
+ * compiling, it will not affect the compilation result.
151
+ */
152
+ removePlugins: (pluginNames: string[], options?: {
153
+ /**
154
+ * Remove the plugin in the specified environment.
155
+ * If environment is not specified, remove it in all environments.
156
+ */
157
+ environment: string;
158
+ }) => void;
159
+ /**
160
+ * Perform a production mode build. This method will generate optimized
161
+ * production bundles and emit them to the output directory.
162
+ */
163
+ build: Build;
164
+ /**
165
+ * Start a server to preview the production build locally.
166
+ * This method should be executed after `rsbuild.build`.
167
+ */
127
168
  preview: (options?: PreviewOptions) => Promise<StartServerResult>;
128
- initConfigs: ProviderInstance['initConfigs'];
129
- inspectConfig: ProviderInstance['inspectConfig'];
130
- createCompiler: ProviderInstance['createCompiler'];
131
- createDevServer: ProviderInstance['createDevServer'];
132
- startDevServer: ProviderInstance['startDevServer'];
133
- getRsbuildConfig: RsbuildPluginAPI['getRsbuildConfig'];
134
- getNormalizedConfig: RsbuildPluginAPI['getNormalizedConfig'];
135
- onBeforeBuild: RsbuildPluginAPI['onBeforeBuild'];
136
- onBeforeCreateCompiler: RsbuildPluginAPI['onBeforeCreateCompiler'];
137
- onBeforeStartDevServer: RsbuildPluginAPI['onBeforeStartDevServer'];
138
- onBeforeStartProdServer: RsbuildPluginAPI['onBeforeStartProdServer'];
139
- onAfterBuild: RsbuildPluginAPI['onAfterBuild'];
140
- onAfterCreateCompiler: RsbuildPluginAPI['onAfterCreateCompiler'];
141
- onAfterStartDevServer: RsbuildPluginAPI['onAfterStartDevServer'];
142
- onAfterStartProdServer: RsbuildPluginAPI['onAfterStartProdServer'];
143
- onCloseDevServer: RsbuildPluginAPI['onCloseDevServer'];
144
- onDevCompileDone: RsbuildPluginAPI['onDevCompileDone'];
145
- onExit: RsbuildPluginAPI['onExit'];
146
- onCloseBuild: RsbuildPluginAPI['onCloseBuild'];
147
- };
169
+ /**
170
+ * Initialize and return the internal Rspack configurations used by Rsbuild.
171
+ * This method processes all plugins and configurations to generate the final
172
+ * Rspack configs. Note: You typically don't need to call this method directly
173
+ * since it's automatically invoked by methods like `rsbuild.build` and
174
+ * `rsbuild.startDevServer`.
175
+ */
176
+ initConfigs: () => Promise<Rspack.Configuration[]>;
177
+ /**
178
+ * Inspect and debug Rsbuild's internal configurations. It provides access to:
179
+ * - The resolved Rsbuild configuration
180
+ * - The environment-specific Rsbuild configurations
181
+ * - The generated Rspack configurations
182
+ *
183
+ * The method serializes these configurations to strings and optionally writes
184
+ * them to disk for inspection.
185
+ */
186
+ inspectConfig: (options?: InspectConfigOptions) => Promise<InspectConfigResult>;
187
+ /**
188
+ * Create an Rspack [Compiler](https://rspack.dev/api/javascript-api/compiler)
189
+ * instance. If there are multiple [environments](/config/environments) for
190
+ * this build, the return value is `MultiCompiler`.
191
+ */
192
+ createCompiler: CreateCompiler;
193
+ /**
194
+ * Rsbuild includes a built-in dev server designed to improve the development
195
+ * experience. When you run the `rsbuild dev` command, the server starts
196
+ * automatically and provides features such as page preview, routing, and hot
197
+ * module reloading.
198
+ *
199
+ * If you want to integrate the Rsbuild dev server into a custom server, you
200
+ * can use the `createDevServer` method to create a dev server instance and
201
+ * call its methods as needed.
202
+ *
203
+ * If you want to directly start the Rsbuild dev server, use the
204
+ * `rsbuild.startDevServer` method.
205
+ */
206
+ createDevServer: CreateDevServer;
207
+ /**
208
+ * Start the local dev server. This method will:
209
+ *
210
+ * 1. Start a development server that serves your application.
211
+ * 2. Watch for file changes and trigger recompilation.
212
+ */
213
+ startDevServer: StartDevServer;
214
+ } & Pick<RsbuildPluginAPI, 'context' | 'getNormalizedConfig' | 'getRsbuildConfig' | 'isPluginExists' | 'onAfterBuild' | 'onAfterCreateCompiler' | 'onAfterStartDevServer' | 'onAfterStartProdServer' | 'onBeforeBuild' | 'onBeforeCreateCompiler' | 'onBeforeStartDevServer' | 'onBeforeStartProdServer' | 'onCloseBuild' | 'onCloseDevServer' | 'onDevCompileDone' | 'onExit'>;
148
215
  export type RsbuildTarget = 'web' | 'node' | 'web-worker';
149
216
  export type RsbuildEntryDescription = Rspack.EntryDescription & {
150
217
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/core",
3
- "version": "1.2.7",
3
+ "version": "1.2.8",
4
4
  "description": "The Rspack-based build tool.",
5
5
  "homepage": "https://rsbuild.dev",
6
6
  "bugs": {
@@ -76,7 +76,7 @@
76
76
  "on-finished": "2.4.1",
77
77
  "open": "^8.4.0",
78
78
  "picocolors": "^1.1.1",
79
- "postcss": "^8.5.1",
79
+ "postcss": "^8.5.2",
80
80
  "postcss-load-config": "6.0.1",
81
81
  "postcss-loader": "8.1.1",
82
82
  "prebundle": "1.2.7",
@@ -89,7 +89,7 @@
89
89
  "style-loader": "3.3.4",
90
90
  "tinyglobby": "^0.2.10",
91
91
  "typescript": "^5.7.3",
92
- "webpack": "^5.97.1",
92
+ "webpack": "^5.98.0",
93
93
  "webpack-bundle-analyzer": "^4.10.2",
94
94
  "webpack-merge": "6.0.1",
95
95
  "ws": "^8.18.0"