diff --git a/e2e/cases/lazy-compilation/basic/index.test.ts b/e2e/cases/lazy-compilation/basic/index.test.ts index f7948aa666..d9ec81f79a 100644 --- a/e2e/cases/lazy-compilation/basic/index.test.ts +++ b/e2e/cases/lazy-compilation/basic/index.test.ts @@ -10,6 +10,50 @@ rspackOnlyTest( const rsbuild = await dev({ cwd: __dirname, + rsbuildConfig: { + dev: { + lazyCompilation: true, + }, + }, + }); + + await gotoPage(page, rsbuild, 'page1'); + await expect(page.locator('#test')).toHaveText('Page 1'); + await expectPoll(() => + rsbuild.logs.some((log) => log.includes('building src/page1/index.js')), + ).toBeTruthy(); + expect( + rsbuild.logs.some((log) => log.includes('building src/page2/index.js')), + ).toBeFalsy(); + + await gotoPage(page, rsbuild, 'page2'); + await expect(page.locator('#test')).toHaveText('Page 2'); + await expectPoll(() => + rsbuild.logs.some((log) => log.includes('building src/page2/index.js')), + ).toBeTruthy(); + + await rsbuild.close(); + }, +); + +rspackOnlyTest( + 'should allow to configure `tools.rspack.experiments.lazyCompilation`', + async ({ page }) => { + if (process.platform === 'win32') { + test.skip(); + } + + const rsbuild = await dev({ + cwd: __dirname, + rsbuildConfig: { + tools: { + rspack: { + experiments: { + lazyCompilation: true, + }, + }, + }, + }, }); await gotoPage(page, rsbuild, 'page1'); diff --git a/e2e/cases/lazy-compilation/basic/rsbuild.config.ts b/e2e/cases/lazy-compilation/basic/rsbuild.config.ts index 3bbb43120b..c203c35299 100644 --- a/e2e/cases/lazy-compilation/basic/rsbuild.config.ts +++ b/e2e/cases/lazy-compilation/basic/rsbuild.config.ts @@ -9,7 +9,4 @@ export default defineConfig({ page2: './src/page2/index.js', }, }, - dev: { - lazyCompilation: true, - }, }); diff --git a/packages/core/src/server/devMiddlewares.ts b/packages/core/src/server/devMiddlewares.ts index 9c72bfad85..1244d1d807 100644 --- a/packages/core/src/server/devMiddlewares.ts +++ b/packages/core/src/server/devMiddlewares.ts @@ -1,7 +1,7 @@ import { isAbsolute, join } from 'node:path'; import { rspack } from '@rspack/core'; import { normalizePublicDirs } from '../defaultConfig'; -import { castArray, pick } from '../helpers'; +import { castArray, isMultiCompiler, pick } from '../helpers'; import { logger } from '../logger'; import type { DevConfig, @@ -67,7 +67,6 @@ const applySetupMiddlewares = ( export type Middlewares = Array; const applyDefaultMiddlewares = async ({ - dev, devServerAPI, middlewares, server, @@ -125,14 +124,30 @@ const applyDefaultMiddlewares = async ({ if ( context.action === 'dev' && context.bundlerType === 'rspack' && - dev.lazyCompilation && compilationManager ) { - middlewares.push( - rspack.experiments.lazyCompilationMiddleware( - compilationManager.compiler, - ) as RequestHandler, - ); + const { compiler } = compilationManager; + + // We check the compiler options to determine whether lazy compilation is enabled. + // Rsbuild users can enable lazy compilation in two ways: + // 1. Use Rsbuild's `dev.lazyCompilation` option + // 2. Use Rspack's `experiments.lazyCompilation` option + const isLazyCompilationEnabled = () => { + if (isMultiCompiler(compiler)) { + return compiler.compilers.some( + (childCompiler) => childCompiler.options.experiments?.lazyCompilation, + ); + } + return compiler.options.experiments?.lazyCompilation; + }; + + if (isLazyCompilationEnabled()) { + middlewares.push( + rspack.experiments.lazyCompilationMiddleware( + compiler, + ) as RequestHandler, + ); + } } if (server.base && server.base !== '/') {