8000 refactor!: lazy compilation middleware supports multiCompiler and use config from compiler instance by JSerFeng · Pull Request #9828 · web-infra-dev/rspack · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
refactor: change middleware interface
  • Loading branch information
JSerFeng committed Jun 16, 2025
commit 2b855c7aca16753b4c84396850609c36cf1af082
4 changes: 2 additions & 2 deletions packages/rspack-test-tools/etc/test-tools.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

```ts

import type { Compiler } from '@rspack/core';
import { Compiler } from '@rspack/core';
import type { Compiler as Compiler_2 } from 'webpack';
import type { Configuration } from 'webpack';
import type EventEmitter from 'node:events';
import { IBasicGlobalContext as IBasicGlobalContext_2 } from '../../type';
import { IBasicModuleScope as IBasicModuleScope_2 } from '../../type';
import { ITestCompilerManager as ITestCompilerManager_2 } from '../type';
import type { MultiCompiler } from '@rspack/core';
import { MultiCompiler } from '@rspack/core';
import type { MultiStats } from '@rspack/core';
import type { MultiStats as MultiStats_2 } from 'webpack';
import type { RspackOptions } from '@rspack/core';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createServer } from "node:http";
import type { Socket } from "node:net";
import type { AddressInfo } from "node:net";
import type { Compiler, MultiCompiler } from "@rspack/core";
import { type Compiler, MultiCompiler } from "@rspack/core";
import { experiments } from "@rspack/core";

export class LazyCompilationTestPlugin {
Expand All @@ -22,11 +22,16 @@ export class LazyCompilationTestPlugin {
: addr.family === "IPv6"
? `${protocol}://[${addr.address}]:${addr.port}`
: `${protocol}://${addr.address}:${addr.port}`;
middleware = experiments.lazyCompilationMiddleware(compiler, {
// @ts-expect-error cacheable is hidden config only for tests
cacheable: false,
serverUrl: urlBase
});
if (compiler instanceof MultiCompiler) {
for (const c of compiler.compilers) {
if (c.options.experiments.lazyCompilation) {
c.options.experiments.lazyCompilation.serverUrl = urlBase;
}
}
} else if (compiler.options.experiments.lazyCompilation) {
compiler.options.experiments.lazyCompilation.serverUrl = urlBase;
}
middleware = experiments.lazyCompilationMiddleware(compiler);

resolve(null);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4150,7 +4150,7 @@ interface LabeledStatement extends Node_4, HasSpan {
export type Layer = string | null;

// @public (undocumented)
const lazyCompilationMiddleware: (compiler: Compiler | MultiCompiler, userOptions?: LazyCompilationOptions | boolean) => Middleware;
const lazyCompilationMiddleware: (compiler: Compiler | MultiCompiler) => MiddlewareHandler;

// @public
export type LazyCompilationOptions = {
Expand Down
88 changes: 28 additions & 60 deletions packages/rspack/src/builtin-plugin/lazy-compilation/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IncomingMessage, ServerResponse } from "node:http";
import { type Compiler, MultiCompiler } from "../..";
import type { LazyCompilationOptions } from "../../config";
import type { Middleware } from "../../config/devServer";
import type { MiddlewareHandler } from "../../config/devServer";
import { BuiltinLazyCompilationPlugin } from "./lazyCompilation";

export const LAZY_COMPILATION_PREFIX = "/lazy-compilation-using-";
Expand Down Expand Up @@ -37,20 +37,10 @@ const getFullServerUrl = ({ serverUrl, prefix }: LazyCompilationOptions) => {
};

export const lazyCompilationMiddleware = (
compiler: Compiler | MultiCompiler,
rawOptions?: LazyCompilationOptions | boolean
): Middleware => {
let userOptions = rawOptions;
if (userOptions === false) {
return noop;
}

if (userOptions === true) {
userOptions = {};
}

compiler: Compiler | MultiCompiler
): MiddlewareHandler => {
if (compiler instanceof MultiCompiler) {
const middlewareByCompiler: Map<string, Middleware> = new Map();
const middlewareByCompiler: Map<string, MiddlewareHandler> = new Map();

let i = 0;
for (const c of compiler.compilers) {
Expand All @@ -59,8 +49,7 @@ export const lazyCompilationMiddleware = (
}

const options = {
...c.options.experiments.lazyCompilation,
...userOptions
...c.options.experiments.lazyCompilation
};

const prefix = options.prefix || LAZY_COMPILATION_PREFIX;
Expand All @@ -78,40 +67,11 @@ export const lazyCompilationMiddleware = (
)
);

const compilerConfig = c.options.experiments.lazyCompilation
? c.options.experiments.lazyCompilation
: {};

const testConfig = compilerConfig.test ?? options.test;

const plugin = new BuiltinLazyCompilationPlugin(
({ module, path }) => {
const key = encodeURIComponent(
module.replace(/\\/g, "/").replace(/@/g, "_")
)
// module identifier may contain query, bang(!) or split(|),
// should do our best to ensure it's the same with which comes
// from server url
.replace(/%(2F|3A|24|26|2B|2C|3B|3D)/g, decodeURIComponent);
filesByKey.set(key, path);
const active = activeModules.get(key) === true;
return {
client: `${options.client || getDefaultClient(c)}?${encodeURIComponent(getFullServerUrl(options))}`,
data: key,
active
};
},
// @ts-expect-error internal option
compilerConfig.cacheable ?? options.cacheable ?? true,
compilerConfig.entries ?? options.entries ?? true,
compilerConfig.imports ?? options.imports ?? true,
testConfig
);
plugin.apply(c);
applyPlugin(c, options, activeModules, filesByKey);
}

const keys = [...middlewareByCompiler.keys()];
return (req: IncomingMessage, res: ServerResponse, next?: () => void) => {
return (req: IncomingMessage, res: ServerResponse, next: () => void) => {
const key = keys.find(key => req.url?.startsWith(key));
if (!key) {
return next?.();
Expand All @@ -123,17 +83,33 @@ export const lazyCompilationMiddleware = (
};
}

if (!compiler.options.experiments.lazyCompilation && !userOptions) {
if (!compiler.options.experiments.lazyCompilation) {
return noop;
}

const activeModules: Map<string, boolean> = new Map();
const filesByKey: Map<string, string> = new Map();

const options = {
...compiler.options.experiments.lazyCompilation,
...userOptions
...compiler.options.experiments.lazyCompilation
};
applyPlugin(compiler, options, activeModules, filesByKey);

const lazyCompilationPrefix = options.prefix || LAZY_COMPILATION_PREFIX;
return lazyCompilationMiddlewareInternal(
compiler,
activeModules,
filesByKey,
lazyCompilationPrefix
);
};

function applyPlugin(
compiler: Compiler,
options: LazyCompilationOptions,
activeModules: Map<string, boolean>,
filesByKey: Map<string, string>
) {
const plugin = new BuiltinLazyCompilationPlugin(
({ module, path }) => {
const key = encodeURIComponent(
Expand All @@ -158,23 +134,15 @@ export const lazyCompilationMiddleware = (
options.test
);
plugin.apply(compiler);

const lazyCompilationPrefix = options.prefix || LAZY_COMPILATION_PREFIX;
return lazyCompilationMiddlewareInternal(
compiler,
activeModules,
filesByKey,
lazyCompilationPrefix
);
};
}

// used for reuse code, do not export this
const lazyCompilationMiddlewareInternal = (
compiler: Compiler | MultiCompiler,
activeModules: Map<string, boolean>,
filesByKey: Map<string, string>,
lazyCompilationPrefix: string
): Middleware => {
): MiddlewareHandler => {
const logger = compiler.getInfrastructureLogger("LazyCompilation");

return (req: IncomingMessage, res: ServerResponse, next?: () => void) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/config/devServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ type DevMiddlewareContext<
};
type Server = any;

type MiddlewareHandler<
export type MiddlewareHandler<
RequestInternal extends Request = Request,
ResponseInternal extends Response = Response
> = (
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/fixtures/rspack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Rspack {
});
const DevServerConstructor = RspackDevServer;
if (compiler.options.experiments.lazyCompilation) {
const middleware = experiments.lazyCompilationMiddleware(compiler, compiler.options.experiments.lazyCompilation)
const middleware = experiments.lazyCompilationMiddleware(compiler)
compiler.options.devServer ??= {};
const setupMiddlewares = compiler.options.devServer.setupMiddlewares;
compiler.options.devServer.setupMiddlewares = (middlewares, server) => {
Expand Down Expand Up @@ -84,7 +84,7 @@ class Rspack {
const DevServerConstructor = RspackDevServer;

if (compiler.options.experiments.lazyCompilation) {
const middleware = experiments.lazyCompilationMiddleware(compiler, compiler.options.experiments.lazyCompilation)
const middleware = experiments.lazyCompilationMiddleware(compiler)
compiler.options.devServer ??= {};
const setupMiddleware = compiler.options.devServer.setupMiddlewares;
compiler.options.devServer.setupMiddlewares = (middlewares, server) => {
Expand Down
8 changes: 2 additions & 6 deletions website/docs/en/guide/features/lazy-compilation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ import DevServer from 'webpack-dev-server';

const compiler = rspack(config);

const middleware = experiments.lazyCompilationMiddleware(
compiler,
config.experiments.lazyCompilation,
);
const middleware = experiments.lazyCompilationMiddleware(compiler);

const server = new DevServer(
{
Expand All @@ -125,10 +122,9 @@ const server = new DevServer(
server.start();
```

`lazyCompilationMiddleware` accepts two parameters:
`lazyCompilationMiddleware` accepts one parameter:

- `compiler`: The current [Compiler](/api/javascript-api/compiler) instance
- `options`: The lazy compilation options, same as [experiments.lazyCompilation](/config/experiments#experimentslazycompilation)

## Customizing lazy compilation endpoint

Expand Down
8 changes: 2 additions & 6 deletions website/docs/zh/guide/features/lazy-compilation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ import DevServer from 'webpack-dev-server';

const compiler = rspack(config);

const middleware = experiments.lazyCompilationMiddleware(
compiler,
config.experiments.lazyCompilation,
);
const middleware = experiments.lazyCompilationMiddleware(compiler);

const server = new DevServer(
{
Expand All @@ -125,10 +122,9 @@ const server = new DevServer(
server.start();
```

`lazyCompilationMiddleware` 接受两个参数
`lazyCompilationMiddleware` 接受一个参数

- `compiler`: 当前的 [Compiler](/api/javascript-api/compiler) 实例
- `options`: 懒编译的配置,与 [experiments.lazyCompilation](/config/experiments#experimentslazycompilation) 相同

## 自定义懒编译端点

Expand Down
Loading
0