UNPKG

rspack-plugin-mock

Version:
77 lines (70 loc) 3.57 kB
import { RspackPluginInstance, Compiler, RspackOptionsNormalized } from '@rspack/core'; import { i as LogLevel, b as MockOptions, c as MockServerPluginOptions, S as ServerBuildOption } from './types-Aw0AciTG.cjs'; import { Server as Server$1 } from 'node:http'; import { Http2SecureServer } from 'node:http2'; import { FSWatcher } from 'node:fs'; import EventEmitter from 'node:events'; interface Logger { debug: (msg: string, level?: boolean | LogLevel) => void; info: (msg: string, level?: boolean | LogLevel) => void; warn: (msg: string, level?: boolean | LogLevel) => void; error: (msg: string, level?: boolean | LogLevel) => void; } declare const logLevels: Record<LogLevel, number>; declare function createLogger(prefix: string, defaultLevel?: LogLevel): Logger; interface MockCompilerOptions { alias?: Record<string, false | string | (string | false)[]>; plugins: RspackPluginInstance[]; cwd?: string; include: string | string[]; exclude: string | string[]; logger: Logger; } declare function createMockCompiler(options: MockCompilerOptions): MockCompiler; declare class MockCompiler extends EventEmitter { options: MockCompilerOptions; cwd: string; mockWatcher: FSWatcher; moduleType: 'cjs' | 'esm'; entryFile: string; private _mockData; private fileFilter; private watchInfo?; compiler?: Compiler | null; constructor(options: MockCompilerOptions); get mockData(): Record<string, MockOptions>; run(): Promise<void>; close(): void; updateAlias(alias: Record<string, false | string | (string | false)[]>): void; updateMockEntry(): Promise<void>; getMockFiles(): Promise<string[]>; watchMockFiles(): void; } interface ResolvedCompilerOptions { alias: Record<string, false | string | (string | false)[]>; proxies: (string | ((pathname: string, req: any) => boolean))[]; wsProxies: (string | ((pathname: string, req: any) => boolean))[]; plugins: RspackPluginInstance[]; context?: string; } type ResolvePluginOptions = Required<Omit<MockServerPluginOptions, 'build'>> & ResolvedCompilerOptions & { logger: Logger; build: false | ServerBuildOption; }; interface MiddlewareOptions { alias: Record<string, false | string | (string | false)[]>; proxies: (string | ((pathname: string, req: any) => boolean))[]; context?: string; plugins: RspackPluginInstance[]; } declare function createMockMiddleware(compiler: MockCompiler, options: ResolvePluginOptions): (middlewares: Middleware[], reload?: () => void) => Middleware[]; type SetupMiddlewaresFn = NonNullable<NonNullable<RspackOptionsNormalized['devServer']>['setupMiddlewares']>; type Middleware = SetupMiddlewaresFn extends (middlewares: (infer T)[], devServer: any) => void ? T : never; type Server = SetupMiddlewaresFn extends (middlewares: any, devServer: infer T) => void ? T : never; interface MockSocketOptions { wsProxies: (string | ((pathname: string, req: any) => boolean))[]; cookiesOptions: MockServerPluginOptions['cookiesOptions']; logger: Logger; } declare function mockWebSocket(compiler: MockCompiler, httpServer: Server$1 | Http2SecureServer, { wsProxies: proxies, cookiesOptions, logger, }: MockSocketOptions): void; export { type Logger as L, type MockCompilerOptions as M, type ResolvePluginOptions as R, type Server as S, MockCompiler as a, type MiddlewareOptions as b, createMockCompiler as c, createMockMiddleware as d, type Middleware as e, type MockSocketOptions as f, createLogger as g, logLevels as l, mockWebSocket as m };