Skip to content

Commit 3a96573

Browse files
fix: the --devtool CLI flag not work as expected (#10880)
* fix: devtool cmd line not work and default value * feat: Change English annotation * feat: change build value * feat: change build value * fix: type * fix: change test * docs: change doc * Apply suggestion from @chenjiahan * Apply suggestion from @chenjiahan * Apply suggestion from @chenjiahan * Apply suggestion from @chenjiahan --------- Co-authored-by: neverland <jait.chen@foxmail.com>
1 parent 9645e50 commit 3a96573

File tree

6 files changed

+28
-9
lines changed

6 files changed

+28
-9
lines changed

packages/rspack-cli/src/cli.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ export class RspackCLI {
172172
if (typeof item.devtool === "undefined") {
173173
item.devtool = isBuild ? "source-map" : "cheap-module-source-map";
174174
}
175+
// The CLI flag has a higher priority than the default devtool and devtool from the config.
176+
if (typeof options.devtool !== "undefined") {
177+
item.devtool = options.devtool as RspackOptions["devtool"];
178+
}
179+
175180
if (isServe) {
176181
const installed = (item.plugins ||= []).find(
177182
item => item instanceof rspackCore.ProgressPlugin

packages/rspack-cli/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export interface RspackCLIOptions {
3030

3131
export interface RspackBuildCLIOptions extends RspackCLIOptions {
3232
entry?: string[];
33-
devtool?: boolean;
33+
devtool?: string | boolean;
3434
mode?: string;
3535
watch?: boolean;
3636
analyze?: boolean;

packages/rspack-cli/src/utils/options.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,15 @@ export const commonOptionsForBuildAndServe = (yargs: yargs.Argv) => {
5858
describe: "env passed to config function"
5959
},
6060
devtool: {
61-
type: "boolean",
62-
default: false,
63-
describe: "devtool",
64-
alias: "d"
61+
type: "string",
62+
describe:
63+
"Specify a developer tool for debugging. Defaults to `cheap-module-source-map` in development and `source-map` in production.",
64+
alias: "d",
65+
coerce: (arg): string | boolean => {
66+
if (arg === "true") return "source-map";
67+
if (arg === "false" || arg.trim() === "") return false;
68+
return arg;
69+
}
6570
}
6671
})
6772
.alias({ v: "version", h: "help" });

packages/rspack-cli/tests/build/basic/basic.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,23 @@ describe("build command", () => {
116116
}
117117
);
118118

119-
it.each(["-d", "--devtool"])(
119+
it.each([
120+
["-d", "eval-source-map"],
121+
["--devtool", "eval-source-map"],
122+
["-d", ""],
123+
["--devtool", ""],
124+
["-d", "false"],
125+
["--devtool", "false"]
126+
])(
120127
"devtool option %p should have higher priority than config",
121-
async command => {
128+
async (command, option) => {
122129
const { exitCode, stderr, stdout } = await run(__dirname, [
123130
command,
131+
option,
124132
"--config",
125133
"./entry.config.js"
126134
]);
135+
127136
const mainJs = await readFile(
128137
resolve(__dirname, "dist/public/main.js"),
129138
"utf-8"

website/docs/en/config/devtool.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Use the [SourceMapDevToolPlugin](/plugins/webpack/source-map-dev-tool-plugin) or
1414
type Devtool = 'string' | false;
1515
```
1616

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

1919
## Configuration guide
2020

website/docs/zh/config/devtool.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import WebpackLicense from '@components/WebpackLicense';
1414
type Devtool = 'string' | false;
1515
```
1616

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

1919
## 决策指南
2020

0 commit comments

Comments
 (0)