Skip to content

Commit d6e552b

Browse files
authored
docs(en): merge webpack.js.org into docschina/cn @ 0f751ed
2 parents 450b072 + ca17b91 commit d6e552b

File tree

9 files changed

+345
-236
lines changed

9 files changed

+345
-236
lines changed

src/content/api/cli.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: 命令行接口(CLI)
33
sort: 1
44
contributors:
55
- anshumanv
6+
- snitin315
67
- evenstensberg
78
- simon04
89
- tbroadley
@@ -61,7 +62,7 @@ webpack-cli 提供了许多 flag 来使 webpack 的工作变得简单。默认
6162
| `--no-color` | boolean | 禁用控制台颜色 |
6263
| `--merge, -m` | boolean | 使用 webpack-merge 合并两个配置文件,例如 `-c ./webpack.config.js -c ./webpack.test.config.js` |
6364
| `--env` | string[] | 当它是一个函数时,传递给配置的环境变量 |
64-
| `--progress` | boolean | 在构建过程中打印编译进度 |
65+
| `--progress` | boolean, string | 在构建过程中打印编译进度 |
6566
| `--help` | boolean | 输出所有支持的 flag 和命令 |
6667
| `--output-path, -o` | string | webpack 生成文件的输出位置,例如 `./dist` |
6768
| `--target, -t` | string | 设置要构建的 target |
@@ -244,10 +245,11 @@ webpack --env production # sets env.production == true
244245

245246
`--env` 参数可以接收多个值:
246247

247-
| Invocation | Resulting environment |
248-
| ------------------------------ | --------------------------- |
249-
| `webpack --env prod` | `{ prod: true }` |
250-
| `webpack --env prod --env min` | `{ prod: true, min: true }` |
248+
| Invocation | Resulting environment |
249+
| --------------------------------------------- | --------------------------------------- |
250+
| `webpack --env prod` | `{ prod: true }` |
251+
| `webpack --env prod --env min` | `{ prod: true, min: true }` |
252+
| `webpack --env platform=app --env production` | `{ platform: "app", production: true }` |
251253

252254
T> 请查阅 [environment 变量指南](/guides/environment-variables/)了解更多信息及用法。
253255

@@ -270,6 +272,20 @@ webpack --analyze
270272

271273
W> 请确保你的项目中安装了 `webpack-bundle-analyzer`,否则 CLI 会提示你安装它。
272274

275+
## Progress {#progress}
276+
277+
如需查看 webpack 的编译进度,你可以使用 `--progress` flag。
278+
279+
```bash
280+
webpack --progress
281+
```
282+
283+
如需收集编译过程中每一步的 profile 数据,你可以将 `profile` 作为值传递给 `--progress` flag。
284+
285+
```bash
286+
webpack --progress=profile
287+
```
288+
273289
## 将 CLI 参数传递给 Node.js {#pass-cli-arguments-to-nodejs}
274290

275291
将参数直接传递给 Node.js 进程,你可以使用 `NODE_OPTIONS` 选项。

src/content/api/resolvers.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ group: Plugins
44
sort: 13
55
contributors:
66
- EugeneHlushko
7+
- chenxsan
78
---
89

910
解析器是使用 `enhanced-resolve` 库创建的。`Resolver`
@@ -28,17 +29,17 @@ contributors:
2829
可以通过插件进行定制:
2930

3031
``` js
31-
compiler.resolverFactory.plugin('resolver [type]', resolver => {
32-
resolver.hooks.resolve.tapAsync('MyPlugin', params => {
33-
// ...
32+
compiler.resolverFactory.hooks.resolver.for('[type]').tap('name', resolver => {
33+
// you can tap into resolver.hooks now
34+
resolver.hooks.result.tap('MyPlugin', result => {
35+
return result;
3436
});
3537
});
3638
```
3739

3840
其中,`[type]` 是上述三个解析器之一。
3941

40-
请参阅 [`enhanced-resolve` documentation](https://github.com/webpack/enhanced-resolve) 以获得钩子的完整列表以及它们的
41-
介绍。
42+
请参阅 [`enhanced-resolve` documentation](https://github.com/webpack/enhanced-resolve) 以获得钩子的完整列表以及它们的介绍。
4243

4344

4445
## 配置选项 {#configuration-options}

src/content/blog/2020-10-10-webpack-5-release.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Webpack 5 发布 (2020-10-10)
33
sort: -202010100
44
contributors:
55
- sokra
6+
- chenxsan
67
---
78

89
webpack 4 于 2018 年 2 月发布。
@@ -1096,7 +1097,7 @@ Stats 的 `preset`,`default`,`json` 和 `toString` 现已由插件系统内
10961097

10971098
### 全新的监听 {#new-watching}
10981099

1099-
webpack 所使用的监听已重构。它之前使用的是 `chokidar` 和原生依赖 `fsevents`(仅在 OSX 上)。现在它在只基于原生的 Node.js 中的 `fs`。这意味着在 webpack 中已经没有原生依赖了。
1100+
webpack 所使用的监听已重构。它之前使用的是 `chokidar` 和原生依赖 `fsevents`(仅在 macOS 上)。现在它在只基于原生的 Node.js 中的 `fs`。这意味着在 webpack 中已经没有原生依赖了。
11001101

11011102
它还能在监听时捕捉更多关于文件系统的信息。目前,它还可以捕获 mtimes 和监视事件时间,以及丢失文件的信息。为此,`WatchFileSystem` API 做了一点小改动。在修改的同时,我们还将 Arrays 转换为 Sets,Objects 转换为 Maps。
11021103

@@ -1220,7 +1221,7 @@ webpack 曾经有一个单一的方法和类型来表示依赖关系的引用(
12201221

12211222
## 其他微小改动 {#other-minor-changes}
12221223

1223-
- 移除 build 目录,用运行时代替 build
1224+
- 移除内置目录,用运行时代替内置目录
12241225
- 移除不适用的特性
12251226
- BannerPlugin 目前只支持一个参数,这个参数可以是对象,字符串或函数
12261227
- 移除 `CachePlugin`
@@ -1395,7 +1396,7 @@ webpack 曾经有一个单一的方法和类型来表示依赖关系的引用(
13951396
- DependencyReference 现将函数传递给模块,而非模块。
13961397
- 移除了 `HarmonyImportSpecifierDependency.redirectedId`
13971398
- 迁移:使用 `setId` 代替
1398-
- acorn 5 -> 7
1399+
- acorn 5 -> 8
13991400
- 测试
14001401
- HotTestCases 现可为多个目标运行,包括 `async-node` `node` `web` `webworker`
14011402
- TestCases 现在可以用 `store: "instant"``store: "pack"` 来运行系统缓存。
@@ -1407,7 +1408,7 @@ webpack 曾经有一个单一的方法和类型来表示依赖关系的引用(
14071408
- loader-runner 已升级:https://github.com/webpack/loader-runner/releases/tag/v3.0.0
14081409
- `Compilation` 中的 `file/context/missingDependencies` 因性能问题不再排序
14091410
- 不要依赖排序
1410-
- webpack-sources 已升级https://github.com/webpack/webpack-sources/releases/tag/v2.0.0-beta.0
1411+
- webpack-sources 升级至 version 2https://github.com/webpack/webpack-sources/releases/tag/v2.0.1
14111412
- 删除了对 webpack-command 的支持
14121413
- 使用 schema-utils@2 进行模式校验
14131414
- `Compiler.assetEmitted` 改进了参数二,增加了更多信息

src/content/configuration/optimization.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ module.exports = {
478478

479479
## `optimization.sideEffects` {#optimizationsideeffects}
480480

481-
`boolean = true`
481+
`boolean = true` `string: 'flag'`
482482

483483
告知 webpack 去辨识 `package.json` 中的 [`副作用`](https://github.com/webpack/webpack/blob/master/examples/side-effects/README.md) 标记或规则,以跳过那些当导出不被使用且被标记不包含副作用的模块。
484484

@@ -509,6 +509,19 @@ module.exports = {
509509
};
510510
```
511511

512+
只使用手动 flag,并且不对源码进行分析:
513+
514+
```js
515+
module.exports = {
516+
//...
517+
optimization: {
518+
sideEffects: 'flag'
519+
}
520+
};
521+
```
522+
523+
此处的 `'flag'` 值在非生产环境默认使用。
524+
512525
## `optimization.portableRecords` {#optimizationportablerecords}
513526

514527
`boolean`

src/content/contribute/writing-a-loader.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ __loader.js__
152152

153153
```js
154154
import { getOptions } from 'loader-utils';
155-
import validateOptions from 'schema-utils';
155+
import { validate } from 'schema-utils';
156156

157157
const schema = {
158158
type: 'object',
@@ -166,7 +166,10 @@ const schema = {
166166
export default function(source) {
167167
const options = getOptions(this);
168168

169-
validateOptions(schema, options, 'Example Loader');
169+
validate(schema, options, {
170+
name: 'Example Loader',
171+
baseDataPath: 'options'
172+
});
170173

171174
// Apply some transformations to the source...
172175

@@ -370,9 +373,9 @@ import compiler from './compiler.js';
370373

371374
test('Inserts name and outputs JavaScript', async () => {
372375
const stats = await compiler('example.txt', { name: 'Alice' });
373-
const output = stats.toJson().modules[0].source;
376+
const output = stats.toJson({source: true}).modules[0].source;
374377

375-
expect(output).toBe('export default "Hey Alice!\"');
378+
expect(output).toBe('export default "Hey Alice!\\n"');
376379
});
377380
```
378381

src/content/guides/asset-modules.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,56 @@ related:
2626
- `asset/source` 导出资源的源代码。之前通过使用 `raw-loader` 实现。
2727
- `asset` 在导出一个 data URI 和发送一个单独的文件之间自动选择。之前通过使用 `url-loader`,并且配置资源体积限制实现。
2828

29+
当在 webpack 5 中使用旧的 assets loader(如 `file-loader`/`url-loader`/`raw-loader` 等)和 asset 模块时,你可能想停止当前 asset 模块的处理,并再次启动处理,这可能会导致 asset 重复,你可以通过将 asset 模块的类型设置为 `'javascript/auto'` 来解决。
30+
31+
__webpack.config.js__
32+
33+
``` diff
34+
module.exports = {
35+
module: {
36+
rules: [
37+
{
38+
test: /\.(png|jpg|gif)$/i,
39+
use: [
40+
{
41+
loader: 'url-loader',
42+
options: {
43+
limit: 8192,
44+
}
45+
},
46+
],
47+
+ type: 'javascript/auto'
48+
},
49+
]
50+
},
51+
}
52+
```
53+
54+
如需从 asset loader 中排除来自新 URL 处理的 asset,请添加 `dependency: { not: ['url'] }` 到 loader 配置中。
55+
56+
__webpack.config.js__
57+
58+
``` diff
59+
module.exports = {
60+
module: {
61+
rules: [
62+
{
63+
test: /\.(png|jpg|gif)$/i,
64+
+ dependency: { not: ['url'] },
65+
use: [
66+
{
67+
loader: 'url-loader',
68+
options: {
69+
limit: 8192,
70+
},
71+
},
72+
],
73+
},
74+
],
75+
}
76+
}
77+
```
78+
2979
## Resource 资源 {#resource-assets}
3080

3181
__webpack.config.js__
@@ -163,7 +213,7 @@ __src/index.js__
163213

164214
```diff
165215
- import mainImage from './images/main.png';
166-
+ import metroMap from './images/matro.svg';
216+
+ import metroMap from './images/metro.svg';
167217

168218
- img.src = mainImage; // '/dist/151cfcfa1bd74779aadb.png'
169219
+ block.style.background = `url(${metroMap})`; // url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDo...vc3ZnPgo=)
@@ -250,7 +300,7 @@ Hello world
250300
__src/index.js__
251301

252302
```diff
253-
- import metroMap from './images/matro.svg';
303+
- import metroMap from './images/metro.svg';
254304
+ import exampleText from './example.txt';
255305

256306
- block.style.background = `url(${metroMap}); // url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDo...vc3ZnPgo=)

0 commit comments

Comments
 (0)