Skip to content

您 -> 你 & 统一为:本指南继续沿用[xxx](/guides/xxx)中的代码示例 #440

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 134 commits into from
Jan 13, 2018
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
134 commits
Select commit Hold shift + click to select a range
f2a258a
update /content/loaders & /content/plugins
hello-lizhihua Sep 3, 2017
a5725bb
Merge remote-tracking branch 'upstream/translation' into translation
hello-lizhihua Sep 3, 2017
248be26
update /content/loaders & /content/plugins
hello-lizhihua Sep 19, 2017
36c53b8
Merge remote-tracking branch 'upstream/translation' into translation
hello-lizhihua Sep 19, 2017
25a3d6d
Merge branch 'master' into cn
hello-lizhihua Oct 10, 2017
14bff9f
update /content/loaders & /content/plugins
hello-lizhihua Oct 10, 2017
debfddd
Merge remote-tracking branch 'upstream/cn' into cn
hello-lizhihua Oct 10, 2017
57fb4d4
update contributors
hello-lizhihua Oct 10, 2017
99bf48d
Merge branch 'master' into translation
hello-lizhihua Oct 10, 2017
7fcd796
Merge remote-tracking branch 'upstream/translation' into translation
hello-lizhihua Oct 11, 2017
78e3cee
update /content/loaders & /content/plugins
hello-lizhihua Oct 15, 2017
8a1b2c9
Merge branch 'master' into translation
hello-lizhihua Oct 15, 2017
34575cd
Merge remote-tracking branch 'upstream/translation' into translation
hello-lizhihua Oct 15, 2017
3251e8a
Merge remote-tracking branch 'upstream/cn' into cn
hello-lizhihua Oct 15, 2017
7396c0d
Merge branch 'master' into cn
hello-lizhihua Oct 15, 2017
c0dc1be
fix LinkDropdown
hello-lizhihua Oct 15, 2017
4809e4a
Merge branch 'translation' into cn
hello-lizhihua Oct 15, 2017
178eeeb
修复 npm 命令错误导致编译不成功的问题
hello-lizhihua Oct 17, 2017
8ec33ad
update /content/loaders & /content/plugins
hello-lizhihua Oct 17, 2017
cc7b5e1
Merge remote-tracking branch 'upstream/translation' into translation
hello-lizhihua Oct 17, 2017
93ebe40
Merge remote-tracking branch 'upstream/cn' into cn
hello-lizhihua Oct 29, 2017
1ca293a
update /content/loaders & /content/plugins
hello-lizhihua Oct 30, 2017
a132154
Merge branch 'master' into translation
hello-lizhihua Oct 30, 2017
9fbf758
Merge remote-tracking branch 'upstream/translation' into translation
hello-lizhihua Oct 30, 2017
a40704a
update /content/loaders & /content/plugins
hello-lizhihua Nov 1, 2017
53d47d1
Merge remote-tracking branch 'upstream/translation' into translation
hello-lizhihua Nov 1, 2017
68c4bdd
Merge remote-tracking branch 'upstream/cn' into cn
hello-lizhihua Nov 1, 2017
d622470
docs(plugins): fix typo in module-concatenation-plugin.md (#1683)
mattdsteele Nov 1, 2017
52eb2a4
docs(concepts): simplify the introduction (#1673)
TheDutchCoder Nov 1, 2017
6e8d65d
docs(plugins): add “scope hoisting” intro in module-concatenation-plu…
iamakulov Nov 3, 2017
66443e6
docs(api): fix some method signatures in loaders.md (#1685)
aparajita Nov 3, 2017
b13613e
update /content/loaders & /content/plugins
Nov 4, 2017
0e407de
Merge remote-tracking branch 'upstream/translation' into translation
Nov 4, 2017
d6295cc
Merge remote-tracking branch 'upstream/master'
Nov 4, 2017
955b221
docs(guides): consistent quoute use in typescript.md (#1687)
ulrikstrid Nov 5, 2017
ac739ee
docs(api/guides): document new --concatenate-modules flag (#1686)
iamakulov Nov 5, 2017
6d30cac
docs(guides): fix issues with examples in shimming.md (#1680)
svyandun Nov 5, 2017
ccac872
docs(guides): add middleware tip to the hmr guide
skipjack Nov 5, 2017
5dcd8ac
Merge remote-tracking branch 'upstream/cn' into cn
hello-lizhihua Nov 5, 2017
0f539cb
Merge remote-tracking branch 'upstream/master'
hello-lizhihua Nov 5, 2017
c71abf9
Merge remote-tracking branch 'upstream/cn' into cn
hello-lizhihua Nov 5, 2017
c9cdb7e
Revert "A new --concatenate-modules flag" (#1692)
skipjack Nov 5, 2017
ebd64c9
update master
hello-lizhihua Nov 6, 2017
796d087
Merge remote-tracking branch 'upstream/master'
hello-lizhihua Nov 6, 2017
4a2422b
Merge branch 'master' into cn
hello-lizhihua Nov 6, 2017
fe130ff
update master
hello-lizhihua Nov 6, 2017
19fc4f6
update /content/loaders & /content/plugins
hello-lizhihua Nov 7, 2017
b9d59a4
Merge remote-tracking branch 'upstream/translation' into translation
hello-lizhihua Nov 7, 2017
38fc0d4
Merge branch 'translation' into cn
hello-lizhihua Nov 7, 2017
34dd7b4
docs(concepts): fix grammar in output.md (#1694)
reergymerej Nov 10, 2017
29899d8
docs(contribute): update writing-a-loader (#1691)
ThaNarie Nov 10, 2017
5a7cbe7
docs(plugins): add external example in SourceMapDevToolPlugin (#1676)
hkrutzer Nov 10, 2017
4760409
docs(config): update dev-server open option (#1693)
applemate Nov 11, 2017
63574ba
fix bugs
hello-lizhihua Nov 12, 2017
192c38e
update /content/loaders & /content/plugins
hello-lizhihua Nov 12, 2017
1511650
docs(api): improve formatting and grammar in loaders.md
skipjack Nov 11, 2017
ab6f188
docs(api): clarify fourth parameter of `this.callback` in loaders.md
skipjack Nov 11, 2017
e6c94a4
docs(api): populate missing link in loaders.md
skipjack Nov 12, 2017
4b80c8b
docs(plugins): correct example in html-webpack-plugin (#1698)
Jocs Nov 13, 2017
b24ea5e
docs(guides): update an example in production.md (#1696)
ogonkov Nov 13, 2017
0fb8a35
fix(markdown): fix overflowing inline code (#1701)
kamleshchandnani Nov 15, 2017
251f704
docs(concepts): update concepts wording (#1702)
TheLarkInn Nov 15, 2017
4f2f269
Merge remote-tracking branch 'upstream/cn' into cn
hello-lizhihua Nov 18, 2017
c5142de
Merge remote-tracking branch 'upstream/master'
hello-lizhihua Nov 18, 2017
5a59bb7
update /content/loaders & /content/plugins
hello-lizhihua Nov 18, 2017
8597638
Merge remote-tracking branch 'upstream/translation' into translation
hello-lizhihua Nov 18, 2017
4d2e2b8
docs(config): fix dead link to webpack-dev-server example (#1704)
loilo Nov 19, 2017
549d14c
docs(concepts): use fragment links in usage instructions (#1705)
CentroDL Nov 19, 2017
9f473f5
docs(guides): add windows usage tip in getting-started (#1671)
Vanguard90 Nov 23, 2017
b8e8365
doc(guides): fix grammatical error in build-performance (#1709)
davesidious Nov 23, 2017
067cb0d
docs(guides): correct two small typos
mbsrc Nov 21, 2017
e57a11b
docs(api): remove inadvertent double verb (#1714)
MajorBreakfast Nov 23, 2017
2efe399
docs(contribute): fix grammar in writing-a-plugin (#1715)
MajorBreakfast Nov 23, 2017
05714b5
docs(config): add semicolon for consistency (#1716)
connorholyday Nov 26, 2017
93e4b4d
docs(contributing): add note about debian OS (#1721)
sukrosono Dec 2, 2017
4eefcee
docs(guides): add output example to shimming doc (#1720)
agudulin Dec 2, 2017
afe298d
docs(plugins): use `.includes` over `.indexOf` (#1719)
mattvagni Dec 2, 2017
955ff3f
docs(guides): use `npx` in getting-started (#1708)
MajorBreakfast Dec 2, 2017
efcce14
Merge remote-tracking branch 'upstream/cn' into cn
hello-lizhihua Dec 3, 2017
0f36909
Merge remote-tracking branch 'upstream/master'
hello-lizhihua Dec 3, 2017
27991dc
update /src/content/loaders & /src/content/plugins
hello-lizhihua Dec 3, 2017
3ff5dfe
Merge remote-tracking branch 'upstream/translation' into translation
hello-lizhihua Dec 3, 2017
1e898cb
Merge remote-tracking branch 'upstream/cn' into cn
hello-lizhihua Dec 3, 2017
46e6781
Merge branch 'translation' into cn
hello-lizhihua Dec 3, 2017
4fe9b6f
Merge remote-tracking branch 'upstream/cn' into cn
hello-lizhihua Dec 4, 2017
b5f821d
Merge branch 'master' into cn
hello-lizhihua Dec 4, 2017
8a3c989
docs(contribute): link compiler docs in writing-a-plugin (#1700)
chrmod Dec 5, 2017
968c0d1
docs(guides): fix `source-map` discrepancy in production.md (#1711)
mbsrc Dec 5, 2017
e911daf
docs(guides): rename 'runtime' to 'manifest' in caching.md (#1713)
mbsrc Dec 5, 2017
3d3ab97
docs(guides): update output to webpack 3.9.1 (#1724)
plug-n-play Dec 5, 2017
a4bdaa2
docs(plugins): update the source-map-devtool-plugin (#1707)
EugeneHlushko Dec 5, 2017
5ff8058
docs(contribute): correct an example in the writers-guide (#1727)
lhoff Dec 5, 2017
4d82b1c
docs(config): improve formatting and add note about library entry poi…
skipjack Dec 6, 2017
f0b140a
docs(guides): mention complex entry point configuration in author-lib…
skipjack Dec 6, 2017
099d928
docs(config): improve formatting and clarify the string usage in exte…
skipjack Dec 6, 2017
d6dee86
docs(config): fix minor proselint error in externals
skipjack Dec 6, 2017
c0db521
docs(api): clarify the `pitch` method and how it can be used in loade…
skipjack Dec 6, 2017
9e77d93
docs(contribute): fix `import` statement in writing-a-loader.md (#1732)
sanchitnevgi Dec 8, 2017
2c47976
docs(config): update devtool production recommendations
neonick Dec 6, 2017
9b8653f
docs(api): remove obsolete cli flag (#1733)
ooflorent Dec 9, 2017
8b5fd79
docs(concepts): add warning about incompatible `import` statements
skipjack Dec 9, 2017
4902981
docs(config): fix typescript setup in configuration-languages (#1734)
youta1119 Dec 11, 2017
093722e
docs: use full `npm install` over `npm i` (#1740)
Chocobo1 Dec 14, 2017
1c17320
docs(guides): fix diff display for webpack.config.js entry object
JGJP Dec 14, 2017
4405fee
docs: fix typos
tbroadley Dec 16, 2017
df39d5f
docs: add tbroadley to contributor lists
tbroadley Dec 16, 2017
7018c23
docs(contribute): correct schema example in `writing-a-loader.md` (#1…
warmrobot Dec 18, 2017
f0ffd1f
docs(guides): add pwa guide (#1737)
johnnyreilly Dec 18, 2017
41615e4
docs(guides): highlight css splitting in production
skipjack Dec 18, 2017
1b5cf43
docs(plugins): use `.includes` over `.indexOf`
baldurh Dec 18, 2017
41a5096
docs(config): omit invalid `detailed` option in stats.md (#1757)
Dec 20, 2017
7078bd7
fix(mobile): correctly sort pages in mobile sidebar (#1759)
pierreneter Dec 20, 2017
a5566a1
chore(vote): port voting app, update deps, and simplify config (#1717)
skipjack Dec 23, 2017
679ed0a
Merge remote-tracking branch 'upstream/cn' into cn
hello-lizhihua Dec 23, 2017
e8b2e06
Merge remote-tracking branch 'upstream/master'
hello-lizhihua Dec 23, 2017
0d6cb2e
docs(readme): reformat at 80 characters and include browserstack link
skipjack Dec 22, 2017
aceaf6e
refactor(notification): simplify notification message and bump the ve…
skipjack Dec 26, 2017
fd4548f
fix(splash): fix visualization artifacts (#1762)
pierreneter Dec 27, 2017
1ec37d9
chore: pick up vote fix and update yarn lock file
skipjack Dec 27, 2017
6e06115
chore(vote): pick up another vote patch
skipjack Dec 28, 2017
f375e60
Merge remote-tracking branch 'upstream/cn' into cn
hello-lizhihua Dec 28, 2017
eb02652
Update index.md (#423)
DanielXH Dec 28, 2017
cd39b03
Update .travis.yml
hello-lizhihua Dec 29, 2017
9a14935
docs: fix formatting and grammar (#1765)
yangshun Jan 7, 2018
232bf00
docs(concepts): change loader order from chonological to reverse (#1767)
Jan 7, 2018
e5a36ae
Merge remote-tracking branch 'upstream/master'
hello-lizhihua Jan 13, 2018
17e1fc8
Merge branch 'master' into cn
hello-lizhihua Jan 13, 2018
c46b22a
update UPDATE.md
hello-lizhihua Jan 13, 2018
7d79c30
https://github.com/webpack-china/webpack.js.org/pull/422
hello-lizhihua Jan 13, 2018
48d38a0
Merge remote-tracking branch 'upstream/cn' into cn
hello-lizhihua Jan 13, 2018
8685372
https://github.com/webpack-china/webpack.js.org/issues/40
hello-lizhihua Jan 13, 2018
7e71b5d
Merge remote-tracking branch 'upstream/cn' into cn
hello-lizhihua Jan 13, 2018
8fb61a3
您 -> 你 & 统一为:本指南继续沿用[xxx](/guides/xxx)中的代码示例
hello-lizhihua Jan 13, 2018
be132a2
Merge remote-tracking branch 'upstream/cn' into cn
hello-lizhihua Jan 13, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/content/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ webpack.js index=./src/index.js index2=./src/index2.js --output-path='./dist' --
`--optimize-max-chunks` | 限制 chunk 的数量 | [LimitChunkCountPlugin](/plugins/limit-chunk-count-plugin)
`--optimize-min-chunk-size` | 限制 chunk 的最小体积 | [MinChunkSizePlugin](/plugins/min-chunk-size-plugin)
`--optimize-minimize` | 压缩混淆 javascript,并且把 loader 设置为 minimizing | [UglifyJsPlugin](/plugins/uglifyjs-webpack-plugin/) & [LoaderOptionsPlugin](/plugins/loader-options-plugin/)
`--concatenate-modules` | 连结 ES 模块,尽可能移除模块包装容器(module wrappers) | [ModuleConcatenationPlugin](/plugins/module-concatenation-plugin/)


### Resolve 配置
Expand Down Expand Up @@ -294,7 +295,7 @@ webpack.js index=./src/index.js index2=./src/index2.js --output-path='./dist' --
简写 | 含义
---------|----------------------------
-d | `--debug --devtool cheap-module-eval-source-map --output-pathinfo`
-p | `--optimize-minimize --define process.env.NODE_ENV="production"`, see [building for production](/guides/production)
-p | `--optimize-minimize --define process.env.NODE_ENV="production" --concatenate-modules`, see [building for production](/guides/production)


### Profiling
Expand Down
4 changes: 2 additions & 2 deletions src/content/api/loaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ T> loader 最初被设计为可以同时当 Babel transform 用。如果你编
### `this.emitWarning`

```typescript
emitWarning(message: string)
emitWarning(warning: Error)
```

发出一个警告。
Expand All @@ -270,7 +270,7 @@ emitWarning(message: string)
### `this.emitError`

```typescript
emitError(message: string)
emitError(error: Error)
```

发出一个错误。
Expand Down
58 changes: 31 additions & 27 deletions src/content/concepts/index.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
---
title: 核心概念
title: 概念
sort: 1
contributors:
- TheLarkInn
- jhnns
- grgur
- johnstew
- jimrfenner
- TheDutchCoder
---

*webpack* 是一个现代 JavaScript 应用程序的_模块打包器(module bundler)_。当 webpack 处理应用程序时,它会递归地构建一个_依赖关系图(dependency graph)_,其中包含应用程序需要的每个模块,然后将所有这些模块打包成少量的 _bundle_ - 通常只有一个,由浏览器加载
从其提供的核心功能来看,*webpack* 是一个现代 JavaScript 应用程序的_模块打包器(module bundler)_。当 webpack 处理应用程序时,它会递归地构建一个_依赖关系图(dependency graph)_,其中包含应用程序需要的每个模块,然后将所有这些模块打包成一个或多个 _bundle_。

它是[高度可配置的](/configuration),但是,在开始前你需要先理解**四个核心概念**:入口(entry)、输出(output)、loader、插件(plugins)。
它是[高度可配置的](/configuration),但是,在开始前你需要先理解四个**核心概念**:

- 入口(entry)
- 输出(output)
- loader
- 插件(plugins)

本文档旨在给出这些概念的**高度**概述,同时提供具体概念的详尽相关用例。


## 入口(Entry)
## 入口(entry)

webpack 创建应用程序所有依赖的关系图(dependency graph)。图的起点被称之为_入口起点(entry point)_。_入口起点_告诉 webpack _从哪里开始_,并根据依赖关系图确定_需要打包的内容_。可以将应用程序的_入口起点_认为是**根上下文(contextual root)** 或 **app 第一个启动文件**
**入口起点(entry point)**指示 webpack 应该使用哪个模块,来作为构建其内部*依赖图*的开始。进入入口起点后,webpack 会找出有哪些模块和库是入口起点(直接和间接)依赖的

在 webpack 中,我们使用 [webpack 配置对象(webpack configuration object)](/configuration) 中的 `entry` 属性来定义_入口_
然后,处理每个依赖,并将其输出到名为 *bundles* 的文件中,我们将在下一章节详细讨论这个过程

接下来我们看一个最简单的例子:
可以通过在 [webpack 配置](/configuration)中配置 `entry` 属性,来指定一个入口起点(或多个入口起点)。

**webpack.config.js**
接下来我们看一个 `entry` 配置的最简单例子:

```javascript
__webpack.config.js__

``` js
module.exports = {
entry: './path/to/my/entry/file.js'
};
```

根据不同应用程序的需要,声明 `entry` 属性有多种方式
T> 根据应用程序的特定需求,可以以多种方式配置 `entry` 属性。从[入口起点](/concepts/entry-points)章节可以了解更多信息

[了解更多!](/concepts/entry-points)

## 出口(output)

## 出口(Output)
**output** 属性告诉 webpack 在哪里输出它所创建的 *bundles*,以及如何命名这些文件。你可以通过在配置中指定一个 `output` 字段,来配置这些处理过程:

将所有的资源(assets)归拢在一起后,还需要告诉 webpack **在哪里**打包应用程序。webpack 的 `output` 属性描述了**如何处理归拢在一起的代码**(bundled code)。

**webpack.config.js**
__webpack.config.js__

```javascript
const path = require('path');
Expand All @@ -59,20 +65,18 @@ module.exports = {

T> 你可能看到项目**生成(emitted 或 emit)**贯穿我们整个文档和[插件 API](/api/plugins)。它是“生产(produced)”或“排放(discharged)”的特殊术语。

`output` 属性还有[更多可配置的特性](/configuration/output),但让我们花一些时间先了解一些 `output` 属性最常见的用例。

[了解更多!](/concepts/output)
`output` 属性还有[更多可配置的特性](/configuration/output),如果你想要了解更多关于 `output` 属性的概念,你可以通过[阅读核心章节]more in the concepts section](/concepts/output)来了解更多。


## Loader
## loader

webpack 的目标是,让 **webpack** 聚焦于项目中的所有资源(asset),而浏览器不需要关注考虑这些(明确的说,这并不意味着所有资源(asset)都必须打包在一起)。webpack 把[每个文件(.css, .html, .scss, .jpg, etc.) 都作为模块](/concepts/modules)处理。然而 webpack 自身**只理解 JavaScript**
*loader* 能够让 webpack 额外去处理那些非 JavaScript 文件(webpack 自身只理解 JavaScript)。loader 可以将所有类型的文件转换为有效的[模块](/concepts/modules),然后再利用 webpack 的打包能力,对它们进行处理

**webpack loader 在文件被添加到依赖图中时,_其转换为模块_。**
本质上,webpack loader 将所有类型的文件,转换为应用程序的依赖图可以直接引用的模块。

在更高层面,在 webpack 的配置中 **loader** 有两个目标。

1. 识别出(identify)应该被对应的 loader 进行转换(transform)的那些文件。(`test` 属性)
1. 识别出应该被对应的 loader 进行转换的那些文件。(使用 `test` 属性)
2. 转换这些文件,从而使其能够被添加到依赖图中(并且最终添加到 bundle 中)(`use` 属性)

**webpack.config.js**
Expand Down Expand Up @@ -107,17 +111,17 @@ loader 还有更多我们尚未提到的具体配置属性。
[了解更多!](/concepts/loaders)


## 插件(Plugins)
## 插件(plugins)

然而由于 loader 仅在每个文件的基础上执行转换,而 `插件(plugins)` 更常用于(但不限于)在打包模块的 “compilation” 和 “chunk” 生命周期执行操作和自定义功能[(查看更多)](/concepts/plugins)。webpack 的插件系统[极其强大和可定制化](/api/plugins)。
loader 习惯用于转换某些类型的模块,而插件则可以用于执行范围更广的任务。插件的范围包括,从打包优化和压缩,一直到重新定义环境中的变量。[插件接口](/api/plugins)功能极其强大,可以用来处理各种各样的任务

想要使用一个插件,你只需要 `require()` 它,然后把它添加到 `plugins` 数组中。多数插件可以通过选项(option)自定义。你也可以在一个配置文件中因为不同目的而多次使用同一个插件,这时需要通过使用 `new` 来创建它的一个实例
想要使用一个插件,你只需要 `require()` 它,然后把它添加到 `plugins` 数组中。多数插件可以通过选项(option)自定义。你也可以在一个配置文件中因为不同目的而多次使用同一个插件,这时需要通过使用 `new` 操作符来创建它的一个实例

**webpack.config.js**

```javascript
const HtmlWebpackPlugin = require('html-webpack-plugin'); //installed via npm
const webpack = require('webpack'); //to access built-in plugins
const HtmlWebpackPlugin = require('html-webpack-plugin'); // 通过 npm 安装
const webpack = require('webpack'); // 用于访问内置插件
const path = require('path');

const config = {
Expand Down
2 changes: 2 additions & 0 deletions src/content/guides/hot-module-replacement.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ server.listen(5000, 'localhost', () => {
});
```

T> If you're [using `webpack-dev-middleware`](/guides/development#using-webpack-dev-middleware), check out the [`webpack-hot-middleware`](https://github.com/glenjamin/webpack-hot-middleware) package to enable HMR on your custom dev server.


## 问题

Expand Down
44 changes: 42 additions & 2 deletions src/content/guides/production.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ contributors:
- xgqfrms
- kelset
- xgirma
- iamakulov
---

在本指南中,我们将深入一些最佳实践,并且使用工具,将网站或应用程序构建到生产环境中。
Expand Down Expand Up @@ -199,7 +200,8 @@ __webpack.prod.js__
plugins: [
new UglifyJSPlugin({
sourceMap: true
}),
- })
+ }),
+ new webpack.DefinePlugin({
+ 'process.env': {
+ 'NODE_ENV': JSON.stringify('production')
Expand Down Expand Up @@ -236,10 +238,48 @@ __src/index.js__
document.body.appendChild(component());
```

## Module Concatenation

By default, webpack wraps each module into a function. Wrapping helps to isolate them but brings additional overhead. You can try reducing this overhead by enabling the [`ModuleConcatenationPlugin`](/plugins/module-concatenation-plugin), which, where possible, merges multiple modules in a single module wrapper:

__webpack.prod.js__

```diff
const webpack = require('webpack');
const merge = require('webpack-merge');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const common = require('./webpack.common.js');

module.exports = merge(common, {
devtool: 'cheap-module-source-map',
plugins: [
new UglifyJSPlugin({
sourceMap: true
}),
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
}
- })
+ }),
+ new webpack.optimize.ModuleConcatenationPlugin()
]
})
```

Read more about module concatenation [in the plugin docs](/plugins/module-concatenation-plugin).


## CLI 替代选项

以上描述也可以通过命令行实现。例如,`--optimize-minimize` 标记将在后台引用 `UglifyJSPlugin`。和以上描述的 `DefinePlugin` 实例相同,`--define process.env.NODE_ENV="'production'"` 也会做同样的事情。并且,`webpack -p` 将自动地调用上述这些标记,从而调用需要引入的插件。

以上描述也可以通过命令行实现:

- `--optimize-minimize` 标记将在后台引用 `UglifyJSPlugin`;
- `--define process.env.NODE_ENV="'production'"` 将复制上面的 `DefinePlugin` 实例;
- `--concatenate-modules` 将启用 `ModuleConcatenationPlugin`.

并且,`webpack -p` 将自动地调用上述这些标记,从而调用需要引入的插件。

这些简便方式虽然都很不错,但是我们通常建议只使用配置方式,因为在这两种场景中下,配置方式能够更好地帮助你了解自己正在做的事情。配置方式还可以让你更方便地控制这两个插件中的其他选项。

Expand Down
10 changes: 8 additions & 2 deletions src/content/guides/shimming.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ contributors:
- jhnns
- simon04
- jeremenichelli
- svyandun
related:
- title: Reward modern browser users script
url: https://hackernoon.com/10-things-i-learned-making-the-fastest-site-in-the-world-18a0e1cdf4a7#c665
Expand Down Expand Up @@ -65,6 +66,7 @@ __webpack.config.js__

``` diff
const path = require('path');
+ const webpack = require('webpack');

module.exports = {
entry: './src/index.js',
Expand All @@ -75,7 +77,7 @@ __webpack.config.js__
+ },
+ plugins: [
+ new webpack.ProvidePlugin({
+ lodash: 'lodash'
+ _: 'lodash'
+ })
+ ]
};
Expand Down Expand Up @@ -112,6 +114,7 @@ __webpack.config.js__

``` diff
const path = require('path');
const webpack = require('webpack');

module.exports = {
entry: './src/index.js',
Expand All @@ -121,7 +124,7 @@ __webpack.config.js__
},
plugins: [
new webpack.ProvidePlugin({
- lodash: 'lodash'
- _: 'lodash'
+ join: ['lodash', 'join']
})
]
Expand Down Expand Up @@ -156,6 +159,7 @@ __webpack.config.js__

``` diff
const path = require('path');
const webpack = require('webpack');

module.exports = {
entry: './src/index.js',
Expand Down Expand Up @@ -213,6 +217,7 @@ __webpack.config.js__

``` diff
const path = require('path');
const webpack = require('webpack');

module.exports = {
entry: './src/index.js',
Expand Down Expand Up @@ -323,6 +328,7 @@ __webpack.config.js__

``` diff
const path = require('path');
const webpack = require('webpack');

module.exports = {
- entry: './src/index.js',
Expand Down
4 changes: 2 additions & 2 deletions src/content/guides/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module.exports = {
]
},
resolve: {
extensions: [ ".tsx", ".ts", ".js" ]
extensions: [ '.tsx', '.ts', '.js' ]
},
output: {
filename: 'bundle.js',
Expand Down Expand Up @@ -139,7 +139,7 @@ __webpack.config.js__
]
},
resolve: {
extensions: [ ".tsx", ".ts", ".js" ]
extensions: [ '.tsx', '.ts', '.js' ]
},
output: {
filename: 'bundle.js',
Expand Down
8 changes: 5 additions & 3 deletions src/content/plugins/module-concatenation-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ related:
new webpack.optimize.ModuleConcatenationPlugin()
```

> 由于实现 ECMAScript 模块语法,作用域提升(Scope Hoisting)这个特定于此语法的功能才成为可能。`webpack` 可能会根据你正在使用的模块类型和[其他的情况](https://medium.com/webpack/webpack-freelancing-log-book-week-5-7-4764be3266f5),回退到普通打包。
> 这种连结行为被称为“作用域提升(scope hoisting)”。
>
> 由于实现 ECMAScript 模块语法,作用域提升(scope hoisting)这个特定于此语法的功能才成为可能。`webpack` 可能会根据你正在使用的模块类型和[其他的情况](https://medium.com/webpack/webpack-freelancing-log-book-week-5-7-4764be3266f5),回退到普通打包。


## 绑定失败的优化[Optimization Bailouts]
Expand Down Expand Up @@ -72,8 +74,8 @@ function tryToAdd(group, module) {
if (!result) {
return false;
}
module.dependencies.forEach(depenency => {
tryToAdd(group, depenency);
module.dependencies.forEach(dependency => {
tryToAdd(group, dependency);
});
group.merge(nextGroup);
return true;
Expand Down