diff --git a/packages/rspack/etc/core.api.md b/packages/rspack/etc/core.api.md index 66254568d5de..4365abc5bccb 100644 --- a/packages/rspack/etc/core.api.md +++ b/packages/rspack/etc/core.api.md @@ -32,7 +32,7 @@ import type { ExternalObject } from '@rspack/binding'; import { fs } from 'fs'; import { default as fs_2 } from 'graceful-fs'; import { HookMap } from '@rspack/lite-tapable'; -import { IncomingMessage as IncomingMessage_2 } from 'http'; +import { IncomingMessage } from 'http'; import { inspect } from 'node:util'; import type { JsAddingRuntimeModule } from '@rspack/binding'; import type { JsAfterEmitData } from '@rspack/binding'; @@ -85,7 +85,7 @@ import { Server } from 'net'; import { Server as Server_2 } from 'tls'; import { Server as Server_3 } from 'http'; import { ServerOptions as ServerOptions_2 } from 'https'; -import { ServerResponse as ServerResponse_2 } from 'http'; +import { ServerResponse } from 'http'; import { SourceMapDevToolPluginOptions } from '@rspack/binding'; import sources = require('../compiled/webpack-sources'); import { StatSyncFn } from 'fs'; @@ -1808,7 +1808,7 @@ export { Dependency } type DependencyLocation = any; // @public (undocumented) -type DevMiddlewareContext = { +type DevMiddlewareContext = { state: boolean; stats: Stats | MultiStats | undefined; callbacks: Callback_2[]; @@ -1820,7 +1820,7 @@ type DevMiddlewareContext = { +type DevMiddlewareOptions = { mimeTypes?: { [key: string]: string; } | undefined; @@ -1847,6 +1847,9 @@ type DevMiddlewareOptions = MiddlewareObject | MiddlewareHandler; + // @public (undocumented) type DevServerOptions> = { ipc?: string | boolean | undefined; @@ -1870,7 +1873,7 @@ type DevServerOptions | undefined) => Headers_2) | undefined; onListening?: ((devServer: Server_4) => void) | undefined; - setupMiddlewares?: ((middlewares: Middleware[], devServer: Server_4) => Middleware[]) | undefined; + setupMiddlewares?: ((middlewares: DevServerMiddleware[], devServer: Server_4) => DevServerMiddleware[]) | undefined; }; // @public @@ -3249,7 +3252,7 @@ interface ImportNamespaceSpecifier extends Node_4, HasSpan { type ImportSpecifier = NamedImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier; // @public (undocumented) -type IncomingMessage = IncomingMessage_2; +type IncomingMessage_2 = IncomingMessage; // @public export type Incremental = { @@ -4133,7 +4136,7 @@ interface LabeledStatement extends Node_4, HasSpan { export type Layer = string | null; // @public (undocumented) -const lazyCompilationMiddleware: (compiler: Compiler, userOptions?: LazyCompilationOptions | boolean) => Middleware; +const lazyCompilationMiddleware: (compiler: Compiler, userOptions?: LazyCompilationOptions | boolean) => DevServerMiddleware; // @public export type LazyCompilationOptions = { @@ -4626,16 +4629,13 @@ interface MethodProperty extends PropBase, Fn { } // @public (undocumented) -type Middleware = MiddlewareObject | MiddlewareHandler; - -// @public (undocumented) -type MiddlewareHandler = any; +type MiddlewareHandler = (req: RequestInternal, res: ResponseInternal, next: NextFunction) => void | Promise; // @public (undocumented) -type MiddlewareObject = { +type MiddlewareObject = { name?: string; path?: string; - middleware: MiddlewareHandler; + middleware: MiddlewareHandler; }; // @public (undocumented) @@ -4648,7 +4648,7 @@ type MkdirSync = (path: PathLike, options: MakeDirectoryOptions) => undefined | export type Mode = "development" | "production" | "none"; // @public (undocumented) -type ModifyResponseData = (req: RequestInternal, res: ResponseInternal, data: Buffer | ReadStream, byteLength: number) => ResponseData; +type ModifyResponseData = (req: RequestInternal, res: ResponseInternal, data: Buffer | ReadStream, byteLength: number) => ResponseData; export { Module } @@ -6016,7 +6016,7 @@ const RemoveDuplicateModulesPlugin: { }; // @public (undocumented) -type Request_2 = IncomingMessage; +type Request_2 = IncomingMessage_2; // @public export type Resolve = ResolveOptions; @@ -6121,7 +6121,7 @@ export type ResourceDataWithData = ResourceData & { }; // @public (undocumented) -type Response_2 = ServerResponse; +type Response_2 = ServerResponse_2; // @public (undocumented) type ResponseData = { @@ -6587,6 +6587,7 @@ declare namespace rspackExports { Watch, WatchOptions, DevServer, + DevServerMiddleware, IgnoreWarnings, Profile, Amd, @@ -7034,7 +7035,7 @@ type ServerOptions = ServerOptions_2 & { }; // @public (undocumented) -type ServerResponse = ServerResponse_2; +type ServerResponse_2 = ServerResponse; // @public (undocumented) type ServerType> = "http" | "https" | "spdy" | "http2" | string | ((arg0: ServerOptions, arg1: A) => S); diff --git a/packages/rspack/src/config/devServer.ts b/packages/rspack/src/config/devServer.ts index 1d871dad855e..101795ba3c34 100644 --- a/packages/rspack/src/config/devServer.ts +++ b/packages/rspack/src/config/devServer.ts @@ -12,9 +12,9 @@ type Logger = ReturnType; type MultiWatching = MultiCompiler["watch"]; type BasicServer = import("net").Server | import("tls").Server; -type ReadStream = typeof import("fs").ReadStream; -type IncomingMessage = typeof import("http").IncomingMessage; -type ServerResponse = typeof import("http").ServerResponse; +type ReadStream = import("fs").ReadStream; +type IncomingMessage = import("http").IncomingMessage; +type ServerResponse = import("http").ServerResponse; type ServerOptions = import("https").ServerOptions & { spdy?: { plain?: boolean | undefined; @@ -143,7 +143,10 @@ type Static = { type ServerType< A extends BasicApplication = BasicApplication, - S extends BasicServer = import("http").Server + S extends BasicServer = import("http").Server< + typeof import("http").IncomingMessage, + typeof import("http").ServerResponse + > > = | "http" | "https" @@ -154,7 +157,10 @@ type ServerType< type ServerConfiguration< A extends BasicApplication = BasicApplication, - S extends BasicServer = import("http").Server + S extends BasicServer = import("http").Server< + typeof import("http").IncomingMessage, + typeof import("http").ServerResponse + > > = { type?: ServerType | undefined; options?: ServerOptions | undefined; @@ -202,13 +208,30 @@ type DevMiddlewareContext< }; type Server = any; -type MiddlewareHandler = any; -type MiddlewareObject = { +type MiddlewareHandler< + RequestInternal extends Request = Request, + ResponseInternal extends Response = Response +> = ( + req: RequestInternal, + res: ResponseInternal, + next: NextFunction +) => void | Promise; + +type MiddlewareObject< + RequestInternal extends Request = Request, + ResponseInternal extends Response = Response +> = { name?: string; path?: string; - middleware: MiddlewareHandler; + middleware: MiddlewareHandler; }; -export type Middleware = MiddlewareObject | MiddlewareHandler; +export type Middleware< + RequestInternal extends Request = Request, + ResponseInternal extends Response = Response +> = + | MiddlewareObject + | MiddlewareHandler; + type OpenApp = { name?: string | undefined; arguments?: string[] | undefined; @@ -244,7 +267,10 @@ type ClientConfiguration = { export type DevServerOptions< A extends BasicApplication = BasicApplication, - S extends BasicServer = import("http").Server + S extends BasicServer = import("http").Server< + typeof import("http").IncomingMessage, + typeof import("http").ServerResponse + > > = { ipc?: string | boolean | undefined; host?: string | undefined; diff --git a/packages/rspack/src/config/types.ts b/packages/rspack/src/config/types.ts index 61be58bad221..1ab0929a8b3d 100644 --- a/packages/rspack/src/config/types.ts +++ b/packages/rspack/src/config/types.ts @@ -2749,6 +2749,8 @@ export type WatchOptions = { * Options for devServer, it based on `webpack-dev-server@5` * */ export interface DevServer extends DevServerOptions {} + +export type { Middleware as DevServerMiddleware } from "./devServer"; //#endregion //#region IgnoreWarnings