Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 7 additions & 2 deletions packages/rspack-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ export class RspackCLI {
program.version(RSPACK_CLI_VERSION);
}

async createCompiler(
async buildCompilerConfig(
options: CommonOptionsForBuildAndServe,
rspackCommand: Command,
callback?: (e: Error | null, res?: Stats | MultiStats) => void,
) {
let { config, pathMap } = await this.loadConfig(options);
config = await this.buildConfig(config, pathMap, options, rspackCommand);
return config;
}

async createCompiler(
config: RspackOptions | MultiRspackOptions,
callback?: (e: Error | null, res?: Stats | MultiStats) => void,
) {
const isWatch = Array.isArray(config)
? (config as MultiRspackOptions).some((i) => i.watch)
: (config as RspackOptions).watch;
Expand Down
3 changes: 2 additions & 1 deletion packages/rspack-cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ async function runBuild(cli: RspackCLI, options: BuildOptions): Promise<void> {
}
};

const compiler = await cli.createCompiler(options, 'build', errorHandler);
const userOption = await cli.buildCompilerConfig(options, 'build');
const compiler = await cli.createCompiler(userOption, errorHandler);

if (!compiler || cli.isWatch(compiler)) {
return;
Expand Down
20 changes: 12 additions & 8 deletions packages/rspack-cli/src/commands/serve.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Compiler } from '@rspack/core';
import type { Compiler, MultiRspackOptions } from '@rspack/core';
import type { RspackDevServer as RspackDevServerType } from '@rspack/dev-server';
import type { RspackCLI } from '../cli';
import { DEFAULT_SERVER_HOT } from '../constants';
Expand Down Expand Up @@ -51,14 +51,17 @@ export class ServeCommand implements RspackCommand {
// Lazy import @rspack/dev-server to avoid loading it on build mode
const { RspackDevServer } = await import('@rspack/dev-server');

const compiler = await cli.createCompiler(cliOptions, 'serve');
const userConfig = await cli.buildCompilerConfig(cliOptions, 'serve');
const compiler = await cli.createCompiler(userConfig);
if (!compiler) {
return;
}
const isMultiCompiler = cli.isMultipleCompiler(compiler);

const compilers = cli.isMultipleCompiler(compiler)
? compiler.compilers
: [compiler];
const compilers = isMultiCompiler ? compiler.compilers : [compiler];
const userConfigs = isMultiCompiler
? (userConfig as MultiRspackOptions)
: ([userConfig] as MultiRspackOptions);

const possibleCompilers = compilers.filter(
(compiler: Compiler) => compiler.options.devServer,
Expand All @@ -80,7 +83,8 @@ export class ServeCommand implements RspackCommand {
/**
* Rspack relies on devServer.hot to enable HMR
*/
for (const compiler of compilers) {
for (const [index, compiler] of compilers.entries()) {
const userConfig = userConfigs[index];
const devServer = (compiler.options.devServer ??= {});
const isWebAppOnly =
compiler.platform.web &&
Expand All @@ -89,8 +93,8 @@ export class ServeCommand implements RspackCommand {
!compiler.platform.electron &&
!compiler.platform.webworker;

if (isWebAppOnly) {
compiler.options.lazyCompilation ??= {
if (isWebAppOnly && userConfig.lazyCompilation === undefined) {
compiler.options.lazyCompilation = {
imports: true,
entries: false,
};
Expand Down
Loading