Skip to content

Commit af51213

Browse files
SyMindstormslowlychenjiahan
authored
docs: improve devtool docs (#9832)
* docs: improve devtool docs * docs: improve devtool docs --------- Co-authored-by: pshu <[email protected]> Co-authored-by: neverland <[email protected]>
1 parent 7033258 commit af51213

File tree

2 files changed

+142
-72
lines changed

2 files changed

+142
-72
lines changed

website/docs/en/config/devtool.mdx

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,93 @@ import WebpackLicense from '@components/WebpackLicense';
44

55
# Devtool
66

7-
The `devtool` configuration is used to control the behavior of the Source Map generation.
7+
Choose a style of source mapping to enhance the debugging process. These values can affect build and rebuild speed dramatically.
8+
9+
Use the [SourceMapDevToolPlugin](/plugins/webpack/source-map-dev-tool-plugin) or [EvalSourceMapDevToolPlugin](/plugins/webpack/eval-source-map-dev-tool-plugin) for a more fine grained configuration.
810

911
- **Type:**
1012

1113
```ts
12-
type Devtool =
13-
| false
14-
| 'eval'
15-
| 'cheap-source-map'
16-
| 'cheap-module-source-map'
17-
| 'source-map'
18-
| 'inline-cheap-source-map'
19-
| 'inline-cheap-module-source-map'
20-
| 'inline-source-map'
21-
| 'inline-nosources-cheap-source-map'
22-
| 'inline-nosources-cheap-module-source-map'
23-
| 'inline-nosources-source-map'
24-
| 'nosources-cheap-source-map'
25-
| 'nosources-cheap-module-source-map'
26-
| 'nosources-source-map'
27-
| 'hidden-nosources-cheap-source-map'
28-
| 'hidden-nosources-cheap-module-source-map'
29-
| 'hidden-nosources-source-map'
30-
| 'hidden-cheap-source-map'
31-
| 'hidden-cheap-module-source-map'
32-
| 'hidden-source-map'
33-
| 'eval-cheap-source-map'
34-
| 'eval-cheap-module-source-map'
35-
| 'eval-source-map'
36-
| 'eval-nosources-cheap-source-map'
37-
| 'eval-nosources-cheap-module-source-map'
38-
| 'eval-nosources-source-map';
14+
type Devtool = 'string' | false;
3915
```
4016

4117
- **Default:** `eval`
4218

43-
The main types of Source Map generated behaviors are `source-map`, `eval`, `cheap`, `module`, `inline`, `nosources` and `hidden`, and they can be combined.
19+
## Configuration Guide
20+
21+
### Step 1: Determine Debugging Needs
22+
23+
- **Not required** → Set `devtool: false`
24+
- Disables all debugging information
25+
- Zero build overhead with maximum build speed
26+
- **Required** → Proceed to [Step 2](#step-2-define-debugging-requirements)
27+
28+
### Step 2: Define Debugging Requirements
29+
30+
- **Module-level positioning only** → Set `devtool: 'eval'`
31+
- Each module executed via `eval()` with `//# sourceURL` comment
32+
- Extremely fast build speed
33+
- **Full source code mapping needed** → Proceed to [Step 3](#step-3-configure-sourcemap)
34+
35+
### Step 3: Configure SourceMap
36+
37+
Set `devtool: 'source-map'`, A full SourceMap is emitted as a separate file. It adds a `//# sourceMapURL` comment to the bundle so development tools know where to find it.
38+
39+
It also supports combination with the following modifiers to improve performance and control SourceMap generation.
40+
41+
Performance optimization modifiers, to speed up the build, usually used in development environments:
42+
43+
| Modifier | Effect | Performance improvement |
44+
| -------- | -------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------- |
45+
| eval | Each module is executed with `eval()` and a SourceMap is added as a DataUrl to the `eval()`, avoiding chunk-level multiple SourceMap concate | ⚡⚡⚡ |
46+
| cheap | Maps line numbers only (no columns), ignores source maps from loaders | ⚡⚡ |
47+
| module | Processes source maps from loaders to map to original code (line-only mapping) ||
48+
49+
Functional modifiers, to control SourceMap generation, usually used in production environments:
50+
51+
| Modifier | Effect |
52+
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
53+
| hidden | SourceMap is emitted as a separate file, but no `//# sourceMappingURL=[url]` comment is added to the bundle, protecting source code privacy |
54+
| inline | SourceMap is added as a DataUrl to the bundle |
55+
| nosources | SourceMap is created without the `sourcesContent` in it |
56+
| debugids | SourceMap is created with the `debugId` in it |
57+
58+
We expect a certain pattern when validate devtool name, pay attention and don't mix up the sequence of devtool string. The pattern is: `[inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map[-debugids]`.
59+
60+
## Recommended configurations
61+
62+
### Development
63+
64+
The following options are ideal for development:
65+
66+
`eval` - Each module is executed with `eval()` and `//# sourceURL`. This is pretty fast. The main disadvantage is that it doesn't display line numbers correctly since it gets mapped to transpiled code instead of the original code (No Source Maps from Loaders).
67+
68+
`eval-source-map` - Each module is executed with `eval()` and a SourceMap is added as a DataUrl to the `eval()`. Initially it is slow, but it provides fast rebuild speed and yields real files. Line numbers are correctly mapped since it gets mapped to the original code. It yields the best quality SourceMaps for development.
69+
70+
`eval-cheap-source-map` - Similar to `eval-source-map`, each module is executed with `eval()`. It is "cheap" because it doesn't have column mappings, it only maps line numbers. It ignores SourceMaps from Loaders and only display transpiled code similar to the eval devtool.
71+
72+
`eval-cheap-module-source-map` - Similar to `eval-cheap-source-map`, however, in this case Source Maps from Loaders are processed for better results. However Loader Source Maps are simplified to a single mapping per line.
73+
74+
### Production
75+
76+
These options are typically used in production:
4477

45-
- `source-map` is the most basic behavior, indicating the generation of Source Map, which has a partial overhead on build performance when Source Map is turned on.
78+
'false' - No SourceMap is emitted. This is a good option to start with.
4679

47-
- `eval` wraps the module generated code with `eval()`, so Rspack can internally cache the module generated results, so when `eval` is used in combination with `source-map`, it optimizes the speed of Source Map generation when rebuilding.
80+
`source-map` - A full SourceMap is emitted as a separate file. It adds a reference comment to the bundle so development tools know where to find it.
4881

49-
- `cheap` means that the Source Map will only generate the mapping of rows, ignoring the mapping of columns, in order to speed up the generation of the Source Map.
82+
:::warning
83+
You should configure your server to disallow access to the Source Map file for normal users!
84+
:::
5085

51-
- `module` is used to control whether the loader needs to return the Source Map, so without `module`, the Source Map can only map code that will be processed by the loader, and because the loader does not need to process the Source Map, the Source Map generation speed will be improved.
86+
`hidden-source-map` - Same as `source-map`, but doesn't add a reference comment to the bundle. Useful if you only want SourceMaps to map error stack traces from error reports, but don't want to expose your SourceMap for the browser development tools.
5287

53-
- `inline` indicates whether the generated Source Map is inlined into the end of the file via the data url.
88+
:::warning
89+
You should not deploy the Source Map file to the webserver. Instead only use it for error report tooling.
90+
:::
5491

55-
- `nosources` is used to control whether the generated Source Map contains source code content to reduce the size of the generated Source Map.
92+
`nosources-source-map` - A SourceMap is created without the `sourcesContent` in it. It can be used to map stack traces on the client without exposing all of the source code. You can deploy the Source Map file to the webserver.
5693

57-
- `hidden` is used to control whether the end of the generated file contains the `# sourceMappingURL=...` annotation. The browser developer tools and VS Code etc. will look for the Source Map by the path or data url of this annotation in order to map the product's row number back to its location in the source code during debugging.
94+
:::warning
95+
It still exposes original filenames and structure, but it doesn't expose the original code.
96+
:::

website/docs/zh/config/devtool.mdx

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,85 @@ import WebpackLicense from '@components/WebpackLicense';
44

55
# Devtool
66

7-
调试:该选项用于控制 Source Map 的生成行为。
7+
该选项用于控制调试信息生成策略,影响调试能力与构建性能。
8+
9+
需要精细控制时,直接使用 [SourceMapDevToolPlugin](/plugins/webpack/source-map-dev-tool-plugin)[EvalSourceMapDevToolPlugin](/plugins/webpack/eval-source-map-dev-tool-plugin)
810

911
- **类型:**
1012

1113
```ts
12-
type Devtool =
13-
| false
14-
| 'eval'
15-
| 'cheap-source-map'
16-
| 'cheap-module-source-map'
17-
| 'source-map'
18-
| 'inline-cheap-source-map'
19-
| 'inline-cheap-module-source-map'
20-
| 'inline-source-map'
21-
| 'inline-nosources-cheap-source-map'
22-
| 'inline-nosources-cheap-module-source-map'
23-
| 'inline-nosources-source-map'
24-
| 'nosources-cheap-source-map'
25-
| 'nosources-cheap-module-source-map'
26-
| 'nosources-source-map'
27-
| 'hidden-nosources-cheap-source-map'
28-
| 'hidden-nosources-cheap-module-source-map'
29-
| 'hidden-nosources-source-map'
30-
| 'hidden-cheap-source-map'
31-
| 'hidden-cheap-module-source-map'
32-
| 'hidden-source-map'
33-
| 'eval-cheap-source-map'
34-
| 'eval-cheap-module-source-map'
35-
| 'eval-source-map'
36-
| 'eval-nosources-cheap-source-map'
37-
| 'eval-nosources-cheap-module-source-map'
38-
| 'eval-nosources-source-map';
14+
type Devtool = 'string' | false;
3915
```
4016

4117
- **默认值:** `eval`
4218

43-
Source Map 生成主要有 `source-map``eval``cheap``module``inline``nosources``hidden` 这几种风格,它们之间可以进行组合。
19+
## 决策指南
20+
21+
### 步骤1:是否需要调试
22+
23+
- **不需要** → 设置 `devtool: false`
24+
- 禁用所有调试信息
25+
- 零额外构建开销,极致构建速度
26+
- **需要** → 进入[步骤2](#步骤2明确调试需求)
27+
28+
### 步骤2:明确调试需求
29+
30+
- **仅需定位模块** → 设置 `devtool: 'eval'`
31+
- 每个模块都使用 `eval()` 执行,并且都有 `//# sourceURL`
32+
- 极快的构建速度
33+
- **需要源码映射** → 进入[步骤3](#步骤3配置-source-map)
34+
35+
### 步骤3:配置 SourceMap
36+
37+
设置 `devtool: 'source-map'` 时,SourceMap 作为一个单独的文件生成。bundle 添加 `//# sourceMapURL` 注释,以便开发工具知道在哪里可以找到它。
38+
39+
它还支持与以下修饰符进行组合,以提升性能或控制 SourceMap 生成。
40+
41+
性能优化修饰符,提升构建速度,通常用于开发环境:
42+
43+
| 修饰符 | 效果 | 性能提升 |
44+
| ------ | ------------------------------------------------------------------------------------------------------------------------ | -------- |
45+
| eval | 每个模块都使用 `eval()` 执行,并且将模块 SourceMap 转换为 DataUrl 后添加到 `eval()` 中,避免 chunk 级 SourceMap 合并计算 | ⚡⚡ |
46+
| cheap | 仅计算行映射,忽略列映射。仅映射到转译后的代码,忽略来自 loader 的 SourceMap | ⚡⚡ |
47+
| module | 处理来自 loader 的 SourceMap,以映射到源代码。仅计算行映射,忽略列映射 ||
48+
49+
功能修饰符,按需控制 SourceMap 的生成,通常用于生产环境:
50+
51+
| 修饰符 | 效果 |
52+
| --------- | ------------------------------------------------------------------------------------------------------ |
53+
| hidden | SourceMap 作为一个单独的文件生成,但不会为 bundle 添加 `//# sourceMappingURL=[url]` 注释,保护源码隐私 |
54+
| inline | SourceMap 以 base64 内联到 bundle 中,不生成单独的文件 |
55+
| nosources | 创建的 SourceMap 不包含 `sourcesContent`(源代码内容) |
56+
| debugids | 创建的 SourceMap 增加 `debugId` 字段 |
57+
58+
验证 `devtool` 的配置时,我们期望的格式是 `[inline-|hidden-|eval-][nosources-][cheap-[module-]]source-map[-debugids]`.
59+
60+
## 推荐配置
61+
62+
### 开发环境
63+
64+
以下选项非常适合开发环境:
65+
66+
`eval` - 每个模块都使用 `eval()` 执行,并且都有 `//# sourceURL`。优点是构建性能极快,缺点是,不会映射到源代码。
67+
68+
`eval-source-map` - 每个模块使用 `eval()` 执行,并且将模块 SourceMap 转换为 DataUrl 后添加到 `eval()` 中,避免 chunk 级 SourceMap 合并计算。
69+
70+
`eval-cheap-source-map` - 类似 `eval-source-map`,每个模块使用 `eval()` 执行。SourceMap 不计算列映射,只是映射行。忽略源自 loader 的 SourceMap,仅映射到转译后的代码。
71+
72+
`eval-cheap-module-source-map` - 类似 `eval-cheap-source-map`,并且,但会处理源自 loader 的 SourceMap,映射到源代码。然而,SourceMap 不计算列映射,只是映射行。
4473

45-
- `source-map` 是最基础的行为,表示生成 Source Map,当开启 Source Map 时会对构建性能有部分开销。
74+
### 生产环境
4675

47-
- `eval` 会通过 `eval()` 包裹模块生成的代码,因此 Rspack 可以在内部对模块的生成结果做缓存,所以当 `eval``source-map` 组合使用时会优化重新构建时的 Source Map 生成速度。
76+
这些选项通常用于生产环境中:
4877

49-
- `cheap` 表示 Source Map 只会生成行的映射,忽略列的映射关系,以加快 Source Map 的生成速度
78+
`false` - 不生成 SourceMap
5079

51-
- `module` 用来控制 loader 是否需要返回 Source Map,所以不开 `module` 时,Source Map 只能映射会 loader 处理之后的代码,同时因为 loader 不需要处理 Source Map,所以 Source Map 生成速度也会有所提升
80+
`source-map` - SourceMap 作为一个单独的文件生成。bundle 添加 `//# sourceMapURL` 注释,以便开发工具知道在哪里可以找到它
5281

53-
- `inline` 表示生成的 Source Map 是否通过 data url 内联到文件末尾。
82+
:::warning
83+
你应该配置你的服务器,不允许普通用户访问 SourceMap 文件!
84+
:::
5485

55-
- `nosources` 用来控制生成的 Source Map 中是否包含源码内容,以减小生成的 Source Map 体积
86+
`hidden-source-map` - 与 `source-map` 相同,但不会为 bundle 添加 `//# sourceMapURL` 注释。你可以将 SourceMap 用于错误报告工具,来映射源自客户端的错误堆栈,而不必向浏览器暴露你的 SourceMap
5687

57-
- `hidden` 用来控制生成的文件尾部是否包含 `# sourceMappingURL=...` 注释。浏览器的开发者工具和 VS Code 等会通过这段注释的路径或 data url 来寻找 Source Map,以便在调试时将产物的行列号映射回源码中的位置
88+
`nosources-source-map` - 创建的 SourceMap 不包含 `sourcesContent`(源代码内容)。用来映射客户端上的错误堆栈,而不会暴露源代码,但仍然会暴露原始的文件名和结构

0 commit comments

Comments
 (0)