@rsbuild/core 1.3.0-beta.2 → 1.3.0-beta.3

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.
@@ -1,4 +1,3 @@
1
- import { URL } from 'node:url';
2
1
  import type { Compiler as WebpackCompiler, MultiCompiler as WebpackMultiCompiler } from 'webpack';
3
2
  import color from '../../compiled/picocolors/index.js';
4
3
  import type RspackChain from '../../compiled/rspack-chain/index.js';
@@ -25,7 +24,6 @@ export declare const getPublicPathFromChain: (chain: RspackChain, withSlash?: bo
25
24
  export declare const getPublicPathFromCompiler: (compiler: Rspack.Compiler | Rspack.Compilation) => string;
26
25
  export declare const urlJoin: (base: string, path: string) => string;
27
26
  export declare const canParse: (url: string) => boolean;
28
- export declare const parseUrl: (url: string) => URL | null;
29
27
  export declare const ensureAssetPrefix: (url: string, assetPrefix?: Rspack.PublicPath) => string;
30
28
  export declare function getFilename(config: NormalizedConfig | NormalizedEnvironmentConfig, type: 'js', isProd: boolean, isServer?: boolean): Rspack.Filename;
31
29
  export declare function getFilename(config: NormalizedConfig | NormalizedEnvironmentConfig, type: 'css', isProd: boolean): Rspack.CssFilename;
@@ -1,28 +1,6 @@
1
- import type { DevConfig, Rspack, ServerConfig } from '../types';
1
+ import type { Rspack } from '../types';
2
2
  import { type InitConfigsOptions } from './initConfigs';
3
3
  export declare function createCompiler(options: InitConfigsOptions): Promise<{
4
4
  compiler: Rspack.Compiler | Rspack.MultiCompiler;
5
5
  rspackConfigs: Rspack.Configuration[];
6
6
  }>;
7
- export type MiddlewareCallbacks = {
8
- onInvalid: () => void;
9
- onDone: (stats: any) => void;
10
- };
11
- export type DevMiddlewareOptions = {
12
- /** To ensure HMR works, the devMiddleware need inject the HMR client path into page when HMR enable. */
13
- clientPaths?: string[];
14
- clientConfig: DevConfig['client'];
15
- publicPath?: string;
16
- /** When liveReload is disabled, the page does not reload. */
17
- liveReload?: boolean;
18
- etag?: 'weak' | 'strong';
19
- /** The options need by compiler middleware (like webpackMiddleware) */
20
- headers?: Record<string, string | string[]>;
21
- writeToDisk?: boolean | ((filename: string, compilationName?: string) => boolean);
22
- stats?: boolean;
23
- /** should trigger when compiler hook called */
24
- callbacks: MiddlewareCallbacks;
25
- /** whether use Server Side Render */
26
- serverSideRender?: boolean;
27
- serverConfig: ServerConfig;
28
- };
@@ -1,7 +1,6 @@
1
- import type { IncomingMessage } from 'node:http';
2
- import type { Socket } from 'node:net';
3
1
  import type { EnvironmentContext, DevConfig as OriginDevConfig, Rspack, ServerConfig } from '../types';
4
- import { type DevMiddlewareAPI } from './devMiddleware';
2
+ import { type CompilationMiddleware } from './compilationMiddleware';
3
+ import { SocketServer } from './socketServer';
5
4
  type Options = {
6
5
  publicPaths: string[];
7
6
  environments: Record<string, EnvironmentContext>;
@@ -14,18 +13,18 @@ type Options = {
14
13
  * 1. setup rsbuild-dev-middleware
15
14
  * 2. establish webSocket connect
16
15
  */
17
- export declare class CompilerDevMiddleware {
18
- middleware: DevMiddlewareAPI;
16
+ export declare class CompilationManager {
17
+ middleware: CompilationMiddleware;
18
+ outputFileSystem: Rspack.OutputFileSystem;
19
19
  private devConfig;
20
20
  private serverConfig;
21
- private compiler;
21
+ compiler: Rspack.Compiler | Rspack.MultiCompiler;
22
22
  private publicPaths;
23
- private socketServer;
23
+ socketServer: SocketServer;
24
24
  constructor({ dev, server, compiler, publicPaths, environments }: Options);
25
25
  init(): Promise<void>;
26
- upgrade(req: IncomingMessage, sock: Socket, head: any): void;
27
26
  close(): Promise<void>;
28
- sockWrite(type: string, data?: Record<string, any> | string | boolean): void;
29
- private setupDevMiddleware;
27
+ readFileSync: (fileName: string) => string;
28
+ private setupCompilationMiddleware;
30
29
  }
31
30
  export {};
@@ -0,0 +1,37 @@
1
+ import type { IncomingMessage, ServerResponse } from 'node:http';
2
+ import type { Compiler, MultiCompiler } from '@rspack/core';
3
+ import type { DevConfig, NextFunction, ServerConfig } from '../types';
4
+ export declare const isClientCompiler: (compiler: {
5
+ options: {
6
+ target?: Compiler["options"]["target"];
7
+ };
8
+ }) => boolean;
9
+ type ServerCallbacks = {
10
+ onInvalid: (compilationId?: string, fileName?: string | null) => void;
11
+ onDone: (stats: any) => void;
12
+ };
13
+ export declare const setupServerHooks: (compiler: Compiler, { onDone, onInvalid }: ServerCallbacks) => void;
14
+ type Middleware = (req: IncomingMessage, res: ServerResponse, next: NextFunction) => Promise<void>;
15
+ export type CompilationMiddlewareOptions = {
16
+ /**
17
+ * To ensure HMR works, the devMiddleware need inject the HMR client path into page when HMR enable.
18
+ */
19
+ clientPaths?: string[];
20
+ /**
21
+ * Should trigger when compiler hook called
22
+ */
23
+ callbacks: ServerCallbacks;
24
+ devConfig: DevConfig;
25
+ serverConfig: ServerConfig;
26
+ };
27
+ export type CompilationMiddleware = Middleware & {
28
+ close: (callback: (err: Error | null | undefined) => void) => any;
29
+ };
30
+ /**
31
+ * The CompilationMiddleware handles compiler setup for development:
32
+ * - Call `compiler.watch` (handled by rsbuild-dev-middleware)
33
+ * - Inject the HMR client path into page
34
+ * - Notify server when compiler hooks are triggered
35
+ */
36
+ export declare const getCompilationMiddleware: (compiler: Compiler | MultiCompiler, options: CompilationMiddlewareOptions) => Promise<CompilationMiddleware>;
37
+ export {};
@@ -0,0 +1,22 @@
1
+ import type { DevConfig, EnvironmentAPI, InternalContext, RequestHandler, ServerConfig } from '../types';
2
+ import type { CompilationManager } from './compilationManager';
3
+ import type { UpgradeEvent } from './helper';
4
+ export type RsbuildDevMiddlewareOptions = {
5
+ pwd: string;
6
+ dev: DevConfig;
7
+ server: ServerConfig;
8
+ context: InternalContext;
9
+ environments: EnvironmentAPI;
10
+ compilationManager?: CompilationManager;
11
+ /**
12
+ * Callbacks returned by the `onBeforeStartDevServer` hook.
13
+ */
14
+ postCallbacks: (() => void)[];
15
+ };
16
+ export type Middlewares = Array<RequestHandler | [string, RequestHandler]>;
17
+ export type GetDevMiddlewaresResult = {
18
+ close: () => Promise<void>;
19
+ onUpgrade: UpgradeEvent;
20
+ middlewares: Middlewares;
21
+ };
22
+ export declare const getDevMiddlewares: (options: RsbuildDevMiddlewareOptions) => Promise<GetDevMiddlewaresResult>;
@@ -1,5 +1,6 @@
1
1
  import type Connect from '../../compiled/connect/index.js';
2
- import type { EnvironmentAPI, HtmlFallback, RequestHandler as Middleware, Rspack } from '../types';
2
+ import type { EnvironmentAPI, HtmlFallback, RequestHandler as Middleware } from '../types';
3
+ import type { CompilationManager } from './compilationManager';
3
4
  export declare const faviconFallbackMiddleware: Middleware;
4
5
  export declare const getRequestLoggerMiddleware: () => Promise<Connect.NextHandleFunction>;
5
6
  export declare const notFoundMiddleware: Middleware;
@@ -9,8 +10,7 @@ export declare const optionsFallbackMiddleware: Middleware;
9
10
  */
10
11
  export declare const getHtmlCompletionMiddleware: (params: {
11
12
  distPath: string;
12
- callback: Middleware;
13
- outputFileSystem: Rspack.OutputFileSystem;
13
+ compilationManager: CompilationManager;
14
14
  }) => Middleware;
15
15
  /**
16
16
  * handle `server.base`
@@ -23,9 +23,8 @@ export declare const getBaseMiddleware: (params: {
23
23
  */
24
24
  export declare const getHtmlFallbackMiddleware: (params: {
25
25
  distPath: string;
26
- callback: Middleware;
26
+ compilationManager: CompilationManager;
27
27
  htmlFallback?: HtmlFallback;
28
- outputFileSystem: Rspack.OutputFileSystem;
29
28
  }) => Middleware;
30
29
  /**
31
30
  * Support viewing served files via `/rsbuild-dev-server` route
@@ -14,7 +14,7 @@ export declare class SocketServer {
14
14
  private initialChunks;
15
15
  private heartbeatTimer;
16
16
  constructor(options: DevConfig);
17
- upgrade(req: IncomingMessage, sock: Socket, head: any): void;
17
+ upgrade: (req: IncomingMessage, sock: Socket, head: any) => void;
18
18
  private checkSockets;
19
19
  private clearHeartbeatTimer;
20
20
  prepare(): Promise<void>;
@@ -1,11 +1,11 @@
1
1
  import type { FSWatcher } from '../../compiled/chokidar/index.js';
2
2
  import type { ChokidarOptions, DevConfig, ServerConfig } from '../types';
3
- import type { CompileMiddlewareAPI } from './getDevMiddlewares';
3
+ import type { CompilationManager } from './compilationManager';
4
4
  type WatchFilesOptions = {
5
5
  dev: DevConfig;
6
6
  server: ServerConfig;
7
- compileMiddlewareAPI?: CompileMiddlewareAPI;
8
7
  root: string;
8
+ compilationManager?: CompilationManager;
9
9
  };
10
10
  export type WatchFilesResult = {
11
11
  close(): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/core",
3
- "version": "1.3.0-beta.2",
3
+ "version": "1.3.0-beta.3",
4
4
  "description": "The Rspack-based build tool.",
5
5
  "homepage": "https://rsbuild.dev",
6
6
  "bugs": {
@@ -55,6 +55,7 @@
55
55
  "devDependencies": {
56
56
  "@rslib/core": "0.5.5",
57
57
  "@types/connect": "3.4.38",
58
+ "@types/cors": "^2.8.17",
58
59
  "@types/node": "^22.13.11",
59
60
  "@types/on-finished": "2.3.4",
60
61
  "@types/webpack-bundle-analyzer": "4.7.0",
package/types.d.ts CHANGED
@@ -5,6 +5,49 @@
5
5
  */
6
6
  interface ImportMetaEnv {
7
7
  [key: string]: any;
8
+ /**
9
+ * The value of the `mode` configuration.
10
+ * @example
11
+ * if (import.meta.env.MODE === 'development') {
12
+ * console.log('development mode');
13
+ * }
14
+ */
15
+ MODE: 'development' | 'production' | 'none';
16
+ /**
17
+ * If `mode` is `'development'`, the value is `true`; otherwise, it is `false`.
18
+ * @example
19
+ * if (import.meta.env.DEV) {
20
+ * console.log('development mode');
21
+ * }
22
+ */
23
+ DEV: boolean;
24
+ /**
25
+ * If `mode` is `'production'`, the value is `true`; otherwise, it is `false`.
26
+ * @example
27
+ * if (import.meta.env.PROD) {
28
+ * console.log('production mode');
29
+ * }
30
+ */
31
+ PROD: boolean;
32
+ /**
33
+ * The value of the `server.base` configuration.
34
+ * @example
35
+ * const image = new Image();
36
+ * // Equivalent to "/foo/favicon.ico"
37
+ * image.src = `${import.meta.env.BASE_URL}/favicon.ico`;
38
+ */
39
+ BASE_URL: string;
40
+ /**
41
+ * The URL prefix of static assets
42
+ * - In development, it is equivalent to the value set by `dev.assetPrefix`.
43
+ * - In production, it is equivalent to the value set by `output.assetPrefix`.
44
+ * - Rsbuild will automatically remove the trailing slash from `assetPrefix`
45
+ * to make string concatenation easier.
46
+ * @example
47
+ * const image = new Image();
48
+ * image.src = `${import.meta.env.ASSET_PREFIX}/favicon.ico`;
49
+ */
50
+ ASSET_PREFIX: string;
8
51
  }
9
52
  interface ImportMeta {
10
53
  readonly env: ImportMetaEnv;
@@ -150,9 +193,6 @@ declare module '*.opus' {
150
193
  export default src;
151
194
  }
152
195
 
153
- /**
154
- * Configuration files
155
- */
156
196
  /**
157
197
  * @requires [@rsbuild/plugin-yaml](https://www.npmjs.com/package/@rsbuild/plugin-yaml)
158
198
  */
@@ -160,6 +200,9 @@ declare module '*.yaml' {
160
200
  const content: Record<string, any>;
161
201
  export default content;
162
202
  }
203
+ /**
204
+ * @requires [@rsbuild/plugin-yaml](https://www.npmjs.com/package/@rsbuild/plugin-yaml)
205
+ */
163
206
  declare module '*.yml' {
164
207
  const content: Record<string, any>;
165
208
  export default content;
@@ -173,69 +216,37 @@ declare module '*.toml' {
173
216
  }
174
217
 
175
218
  /**
176
- * Queries
219
+ * Imports the file as a URL string.
220
+ * @note Only works for static assets by default.
221
+ * @example
222
+ * import logoUrl from './logo.png?url'
223
+ * console.log(logoUrl) // 'http://example.com/logo.123456.png'
177
224
  */
178
225
  declare module '*?url' {
179
226
  const content: string;
180
227
  export default content;
181
228
  }
182
- declare module '*?inline' {
183
- const content: string;
184
- export default content;
185
- }
186
229
 
187
230
  /**
188
- * Inline CSS
231
+ * Imports the file content as a base64 encoded string.
232
+ * @note Only works for static assets and CSS files by default.
233
+ * @example
234
+ * import logo from './logo.svg?inline'
235
+ * console.log(logo) // 'data:image/svg+xml;base64,...'
189
236
  */
190
- declare module '*.css?inline' {
191
- const content: string;
192
- export default content;
193
- }
194
- declare module '*.scss?inline' {
195
- const content: string;
196
- export default content;
197
- }
198
- declare module '*.sass?inline' {
199
- const content: string;
200
- export default content;
201
- }
202
- declare module '*.less?inline' {
203
- const content: string;
204
- export default content;
205
- }
206
- declare module '*.styl?inline' {
207
- const content: string;
208
- export default content;
209
- }
210
- declare module '*.stylus?inline' {
237
+ declare module '*?inline' {
211
238
  const content: string;
212
239
  export default content;
213
240
  }
214
241
 
215
242
  /**
216
- * Raw CSS
243
+ * Imports the raw content of the file as a string.
244
+ * @note Only works for static assets and CSS files by default.
245
+ * @example
246
+ * import raw from './logo.svg?raw'
247
+ * console.log(raw) // '<svg viewBox="0 0 24 24">...</svg>'
217
248
  */
218
- declare module '*.css?raw' {
219
- const content: string;
220
- export default content;
221
- }
222
- declare module '*.scss?raw' {
223
- const content: string;
224
- export default content;
225
- }
226
- declare module '*.sass?raw' {
227
- const content: string;
228
- export default content;
229
- }
230
- declare module '*.less?raw' {
231
- const content: string;
232
- export default content;
233
- }
234
- declare module '*.styl?raw' {
235
- const content: string;
236
- export default content;
237
- }
238
- declare module '*.stylus?raw' {
249
+ declare module '*?raw' {
239
250
  const content: string;
240
251
  export default content;
241
252
  }
@@ -257,6 +268,9 @@ declare module '*.module.scss' {
257
268
  const classes: CSSModuleClasses;
258
269
  export default classes;
259
270
  }
271
+ /**
272
+ * @requires [@rsbuild/plugin-sass](https://www.npmjs.com/package/@rsbuild/plugin-sass)
273
+ */
260
274
  declare module '*.module.sass' {
261
275
  const classes: CSSModuleClasses;
262
276
  export default classes;
@@ -275,6 +289,9 @@ declare module '*.module.styl' {
275
289
  const classes: CSSModuleClasses;
276
290
  export default classes;
277
291
  }
292
+ /**
293
+ * @requires [@rsbuild/plugin-stylus](https://www.npmjs.com/package/@rsbuild/plugin-stylus)
294
+ */
278
295
  declare module '*.module.stylus' {
279
296
  const classes: CSSModuleClasses;
280
297
  export default classes;
@@ -1,2 +0,0 @@
1
- import type { RsbuildPlugin } from '../types';
2
- export declare const pluginLazyCompilation: () => RsbuildPlugin;
@@ -1,27 +0,0 @@
1
- import type { IncomingMessage, ServerResponse } from 'node:http';
2
- import type { Compiler, MultiCompiler } from '@rspack/core';
3
- import type { DevMiddlewareOptions } from '../provider/createCompiler';
4
- import type { NextFunction } from '../types';
5
- type ServerCallbacks = {
6
- onInvalid: (compilationId?: string, fileName?: string | null) => void;
7
- onDone: (stats: any) => void;
8
- };
9
- export declare const isClientCompiler: (compiler: {
10
- options: {
11
- target?: Compiler["options"]["target"];
12
- };
13
- }) => boolean;
14
- export declare const setupServerHooks: (compiler: Compiler, hookCallbacks: ServerCallbacks) => void;
15
- export type Middleware = (req: IncomingMessage, res: ServerResponse, next: NextFunction) => Promise<void>;
16
- export type DevMiddlewareAPI = Middleware & {
17
- close: (callback: (err: Error | null | undefined) => void) => any;
18
- };
19
- /**
20
- * The rsbuild/server do nothing about compiler, the devMiddleware need do such things to ensure dev works well:
21
- * - Call compiler.watch (normally did by rsbuild-dev-middleware).
22
- * - Inject the HMR client path into page (the HMR client rsbuild/server already provide).
23
- * - Notify server when compiler hooks are triggered.
24
- */
25
- export type DevMiddleware = (options: DevMiddlewareOptions) => Promise<DevMiddlewareAPI>;
26
- export declare const getDevMiddleware: (multiCompiler: Compiler | MultiCompiler) => Promise<NonNullable<DevMiddleware>>;
27
- export {};
@@ -1,30 +0,0 @@
1
- import type { DevConfig, EnvironmentAPI, RequestHandler, Rspack, ServerConfig, SetupMiddlewaresServer } from '../types';
2
- import type { UpgradeEvent } from './helper';
3
- export type CompileMiddlewareAPI = {
4
- middleware: RequestHandler;
5
- sockWrite: SetupMiddlewaresServer['sockWrite'];
6
- onUpgrade: UpgradeEvent;
7
- close: () => Promise<void>;
8
- };
9
- export type RsbuildDevMiddlewareOptions = {
10
- pwd: string;
11
- dev: DevConfig;
12
- server: ServerConfig;
13
- environments: EnvironmentAPI;
14
- compileMiddlewareAPI?: CompileMiddlewareAPI;
15
- outputFileSystem: Rspack.OutputFileSystem;
16
- output: {
17
- distPath: string;
18
- };
19
- /**
20
- * Callbacks returned by the `onBeforeStartDevServer` hook.
21
- */
22
- postCallbacks: (() => void)[];
23
- };
24
- export type Middlewares = Array<RequestHandler | [string, RequestHandler]>;
25
- export type GetMiddlewaresResult = {
26
- close: () => Promise<void>;
27
- onUpgrade: UpgradeEvent;
28
- middlewares: Middlewares;
29
- };
30
- export declare const getMiddlewares: (options: RsbuildDevMiddlewareOptions) => Promise<GetMiddlewaresResult>;