Skip to content

Commit de84da9

Browse files
authored
docs(builder): filename of async modules (#3691)
1 parent 09c7af6 commit de84da9

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

packages/document/builder-doc/docs/en/config/output/filename.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ const prodDefaultFilename = {
3737

3838
Sets the filename of dist files.
3939

40-
After the production build, there will be a hash in the middle of the filename by default. You can disable this behavior through the `output.disableFilenameHash` config.
40+
After the production build, there will be a hash in the middle of the filename by default. This behavior can be disabled through the `output.disableFilenameHash` config.
4141

42-
Detail:
42+
The following are the details of each filename:
4343

4444
- `js`: The name of the JavaScript file.
4545
- `css`: The name of the CSS style file.
@@ -50,7 +50,7 @@ Detail:
5050

5151
### Example
5252

53-
Set the name of the JavaScript file to `[name]_script.js`:
53+
To set the name of the JavaScript file to `[name]_script.js`, use the following configuration:
5454

5555
```js
5656
export default {
@@ -64,3 +64,24 @@ export default {
6464
},
6565
};
6666
```
67+
68+
### Filename of Async Modules
69+
70+
When you import a module via dynamic import, the module will be bundled into a single file, and its default naming rules are as follows:
71+
72+
- In the development environment, the filename will be generated based on the module path, such as `dist/static/js/async/src_add_ts.js`.
73+
- In the production environment, it will be a random numeric id, such as `dist/static/js/async/798.27e3083e.js`. This is to avoid leaking the source code path in the production environment, and the number of characters is also less.
74+
75+
```js title="src/index.ts"
76+
const { add } = await import('./add.ts');
77+
```
78+
79+
If you want to specify a fixed name for the async module, you can use the [magic comments](https://webpack.js.org/api/module-methods/#magic-comments) provided by the bundler to achieve this, using `webpackChunkName ` to specify the module name:
80+
81+
```js title="src/index.ts"
82+
const { add } = await import(
83+
/* webpackChunkName: "my-chunk-name" */ './add.ts'
84+
);
85+
```
86+
87+
After specifying the module name as above, the generated file will be `dist/static/js/async/my-chunk-name.js`.

packages/document/builder-doc/docs/zh/config/output/filename.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const prodDefaultFilename = {
3939

4040
在生产环境构建后,会自动在文件名中间添加 hash 值,如果不需要添加,可以通过 `output.disableFilenameHash` 配置来禁用该行为。
4141

42-
其中
42+
下面是各个文件类型的说明
4343

4444
- `js`:表示 JavaScript 文件的名称。
4545
- `css`:表示 CSS 样式文件的名称。
@@ -64,3 +64,24 @@ export default {
6464
},
6565
};
6666
```
67+
68+
### 异步模块的文件名
69+
70+
当你在代码中通过 dynamic import 的方式引入模块时,该模块会被单独打包成一个文件,它默认的命名规则如下:
71+
72+
- 在开发环境下会基于模块路径生成名称,比如 `dist/static/js/async/src_add_ts.js`
73+
- 在生产环境下会是一个随机的数字 id,比如 `dist/static/js/async/798.27e3083e.js`,这是为了避免在生产环境中泄露源代码的路径,同时字符数也更少。
74+
75+
```js title="src/index.ts"
76+
const { add } = await import('./add.ts');
77+
```
78+
79+
如果你希望为异步模块指定一个固定的名称,可以通过打包工具提供的 [magic comments](https://webpack.js.org/api/module-methods/#magic-comments) 来实现,通过 `webpackChunkName` 指定模块名称:
80+
81+
```js title="src/index.ts"
82+
const { add } = await import(
83+
/* webpackChunkName: "my-chunk-name" */ './add.ts'
84+
);
85+
```
86+
87+
通过以上写法指定模块名称后,生成的文件会是 `dist/static/js/async/my-chunk-name.js`

0 commit comments

Comments
 (0)