Skip to content

fix: the --devtool CLI flag not work as expected #10880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 12, 2025
Merged
5 changes: 5 additions & 0 deletions packages/rspack-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ export class RspackCLI {
if (typeof item.devtool === "undefined") {
item.devtool = isBuild ? "source-map" : "cheap-module-source-map";
}
// The CLI flag has a higher priority than the default devtool and devtool from the config.
if (typeof options.devtool !== "undefined") {
item.devtool = options.devtool as RspackOptions["devtool"];
}

if (isServe) {
const installed = (item.plugins ||= []).find(
item => item instanceof rspackCore.ProgressPlugin
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface RspackCLIOptions {

export interface RspackBuildCLIOptions extends RspackCLIOptions {
entry?: string[];
devtool?: boolean;
devtool?: string | boolean;
mode?: string;
watch?: boolean;
analyze?: boolean;
Expand Down
13 changes: 9 additions & 4 deletions packages/rspack-cli/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,15 @@ export const commonOptionsForBuildAndServe = (yargs: yargs.Argv) => {
describe: "env passed to config function"
},
devtool: {
type: "boolean",
default: false,
describe: "devtool",
alias: "d"
type: "string",
describe:
"Specify a developer tool for debugging. Defaults to `cheap-module-source-map` in development and `source-map` in production.",
alias: "d",
coerce: (arg): string | boolean => {
if (arg === "true") return "source-map";
if (arg === "false" || arg.trim() === "") return false;
return arg;
}
}
})
.alias({ v: "version", h: "help" });
Expand Down
13 changes: 11 additions & 2 deletions packages/rspack-cli/tests/build/basic/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,23 @@ describe("build command", () => {
}
);

it.each(["-d", "--devtool"])(
it.each([
["-d", "eval-source-map"],
["--devtool", "eval-source-map"],
["-d", ""],
["--devtool", ""],
["-d", "false"],
["--devtool", "false"]
])(
"devtool option %p should have higher priority than config",
async command => {
async (command, option) => {
const { exitCode, stderr, stdout } = await run(__dirname, [
command,
option,
"--config",
"./entry.config.js"
]);

const mainJs = await readFile(
resolve(__dirname, "dist/public/main.js"),
"utf-8"
Expand Down
2 changes: 1 addition & 1 deletion website/docs/en/config/devtool.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Use the [SourceMapDevToolPlugin](/plugins/webpack/source-map-dev-tool-plugin) or
type Devtool = 'string' | false;
```

- **Default:** `eval`
- **Default:** `cheap-module-source-map` in development mode and `source-map` in production mode

## Configuration guide

Expand Down
2 changes: 1 addition & 1 deletion website/docs/zh/config/devtool.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import WebpackLicense from '@components/WebpackLicense';
type Devtool = 'string' | false;
```

- **默认值:** `eval`
- **默认值:** development 模式为 `cheap-module-source-map`,production 模式 为 `source-map`

## 决策指南

Expand Down
Loading