Skip to content

Commit 1abd94d

Browse files
Merge pull request webpack#130 from zgayjjf/cn
翻译 guides/code-splitting-libraries;guides/development
2 parents 5718a43 + 65e3628 commit 1abd94d

File tree

2 files changed

+74
-75
lines changed

2 files changed

+74
-75
lines changed

content/guides/code-splitting-libraries.md

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: 代码拆分 - Libraries
2+
title: 代码拆分 - 库(Libraries)
33
sort: 32
44
contributors:
55
- pksjce
@@ -8,20 +8,19 @@ contributors:
88
- rafde
99
- bartushek
1010
---
11+
一个典型的应用程序会使用第三方框架、工具库。这些库一般使用特定的版本并且其代码不经常改变。然而,应用本身的代码会频繁改变。
1112

12-
A typical application uses third party libraries for framework/functionality needs. Particular versions of these libraries are used and code here does not change often. However, the application code changes frequently.
13+
把第三方代码和应用本身的代码一起打包是非常低效的。因为浏览器会根据缓存头来缓存资源文件,如果文件没有被改变,文件将会被缓存从而不用去再次请求 cdn。为了利用这一特性,我们希望不管应用本身的代码如何改变,vendor 文件的 hash 始终恒定不变。
1314

14-
Bundling application code with third party code would be inefficient. This is because the browser can cache asset files based on the cache header and files can be cached without needing to call the cdn again if its contents don't change. To take advantage of this, we want to keep the hash of the vendor files constant regardless of application code changes.
15+
只有当我们把 vendor 和应用代码的 bundle 分离时,才能实现这一点。
1516

16-
We can do this only when we separate the bundles for vendor and application code.
17+
让我们考虑一个使用了 [momentjs](https://www.npmjs.com/package/moment)(一种常用的时间格式化库)的示例应用程序。
1718

18-
Let's consider a sample application that uses [momentjs](https://www.npmjs.com/package/moment), a commonly used time formatting library.
19-
20-
Install `moment` as follows in your application directory.
19+
在你的应用路径下安装 `moment`,如下:
2120

2221
`npm install --save moment`
2322

24-
The index file will require `moment` as a dependency and log the current date as follows
23+
index.js 文件会引用 `moment` 并且输出当前的时间,代码如下:
2524

2625
__index.js__
2726
```javascript
@@ -30,7 +29,7 @@ console.log(moment().format());
3029

3130
```
3231

33-
We can bundle the application with webpack using the following config
32+
我们可以用 webpack 通过如下的配置来打包(bundle)该应用:
3433

3534
__webpack.config.js__
3635

@@ -48,13 +47,13 @@ module.exports = function(env) {
4847
}
4948
```
5049

51-
On running `webpack` in your application, if you inspect the resulting bundle, you will see that `moment` and `index.js` have been bundled in `bundle.js`.
50+
在项目中运行 `webpack`,如果你查看生成的包,会发现 `moment` `index.js` 都被打包进了 `bundle.js`
5251

53-
This is not ideal for the application. If the code in `index.js` changes, then the whole bundle is rebuilt. The browser will have to load a new copy of the new bundle even though most of it hasn't changed at all.
52+
这对于该应用来说是很不理想的。如果 `index.js` 中的代码改变了,那么整个 bundle 都会重新构建。浏览器就需要加载新的 bundle,即使其中大部分代码都没改变。
5453

55-
## Multiple Entries
54+
## 多入口
5655

57-
Let's try to mitigate this by adding a separate entry point for `moment` and name it `vendor`
56+
让我们尝试通过为 `moment` 添加一个单独的入口点并将其命名为 `vendor` 来缓解这一情况。
5857

5958
__webpack.config.js__
6059

@@ -75,15 +74,15 @@ module.exports = function(env) {
7574
}
7675
```
7776

78-
On running `webpack` now, we see that two bundles have been created. If you inspect these though, you will find that the code for `moment` is present in both the files!
77+
再次运行 `webpack`,可以发现生成了两个 bundle。然而如果查看他们的代码,会发现 `moment` 的代码在两个文件中都出现了!
7978

80-
It is for this reason, that we will need to use the [CommonsChunkPlugin](/plugins/commons-chunk-plugin).
79+
正是由于这个原因,我们需要使用 [CommonsChunkPlugin](/plugins/commons-chunk-plugin)
8180

8281
## `CommonsChunkPlugin`
8382

84-
This is a pretty complex plugin. It fundamentally allows us to extract all the common modules from different bundles and add them to the common bundle. If a common bundle does not exist, then it creates a new one.
83+
这是一个非常复杂的插件。它从根本上允许我们从不同的 bundle 中提取所有的公共模块,并且将他们加入公共 bundle 中。如果公共 bundle 不存在,那么它将会创建一个出来。
8584

86-
We can modify our webpack config file to use the `CommonsChunkPlugin` as follows
85+
我们可以通过修改 webpack 配置文件来使用 `CommonsChunkPlugin`,代码如下:
8786

8887
__webpack.config.js__
8988

@@ -103,18 +102,18 @@ module.exports = function(env) {
103102
},
104103
plugins: [
105104
new webpack.optimize.CommonsChunkPlugin({
106-
name: 'vendor' // Specify the common bundle's name.
105+
name: 'vendor' // 指定公共 bundle 的名字。
107106
})
108107
]
109108
}
110109
}
111110
```
112-
Now run `webpack` on your application. Bundle inspection shows that `moment` code is present only in the vendor bundle.
113-
114-
## Implicit Common Vendor Chunk
111+
现在运行 `webpack`。查看结果会发现 `moment` 代码只会出现在 vendor bundle 中。
115112

116-
You can configure a `CommonsChunkPlugin` instance to only accept vendor libraries.
113+
## 隐式公共 vendor chunk
117114

115+
你可以将 `CommonsChunkPlugin` 配置为只接受 vendor 库。
116+
118117
__webpack.config.js__
119118

120119
```javascript
@@ -134,23 +133,23 @@ module.exports = function() {
134133
new webpack.optimize.CommonsChunkPlugin({
135134
name: 'vendor',
136135
minChunks: function (module) {
137-
// this assumes your vendor imports exist in the node_modules directory
136+
// 该配置假定你引入的 vendor 存在于 node_modules 目录中
138137
return module.context && module.context.indexOf('node_modules') !== -1;
139138
}
140139
})
141140
]
142141
};
143142
}
144143
```
144+
## Manifest 文件
145145

146-
## Manifest File
146+
但是,如果我们改变应用的代码并且再次运行 `webpack`,可以看到 vendor 文件的 hash 改变了。即使我们把 `vendor``main` 的 bundle 分开了,也会发现 `vendor` bundle 会随着应用代码改变。
147147

148-
But, if we change application code and run `webpack` again, we see that the hash for the vendor file changes. Even though we achieved separate bundles for `vendor` and `main` bundles, we see that the `vendor` bundle changes when the application code changes.
149-
This means that we still don't reap the benefits of browser caching because the hash for vendor file changes on every build and the browser will have to reload the file.
148+
这意味着我们任然无法从浏览器缓存机制中受益,因为 vendor 的 hash 在每次构建中都会改变,浏览器也必须重新加载文件。
150149

151-
The issue here is that on every build, webpack generates some webpack runtime code, which helps webpack do it's job. When there is a single bundle, the runtime code resides in it. But when multiple bundles are generated, the runtime code is extracted into the common module, here the `vendor` file.
150+
这里的问题在于,每次构建时,webpack 生成了一些 webpack runtime 代码,用来帮助 webpack 完成其工作。当只有一个 bundle 的时候,runtime 代码驻留在其中。但是当生成多个 bundle 的时候,运行时代码被提取到了公共模块中,在这里就是 `vendor` 文件。
152151

153-
To prevent this, we need extract out the runtime into a separate manifest file. Even though we are creating another bundle, the overhead is offset by the long term caching benefits that we obtain on the `vendor` file.
152+
为了防止这种情况,我们需要将运行时代码提取到一个单独的 manifest 文件中。尽管我们又创建了另一个 bundle,其开销也被我们在 `vendor` 文件的长期缓存中获得的好处所抵消。
154153

155154
__webpack.config.js__
156155

@@ -170,16 +169,16 @@ module.exports = function(env) {
170169
},
171170
plugins: [
172171
new webpack.optimize.CommonsChunkPlugin({
173-
names: ['vendor', 'manifest'] // Specify the common bundle's name.
172+
names: ['vendor', 'manifest'] // 指定公共 bundle 的名字。
174173
})
175174
]
176175
}
177176
};
178177
```
179178

180-
With the above webpack config, we see three bundles being generated. `vendor`, `main` and `manifest` bundles.
179+
使用上面的 webpack 配置,我们看到生成了三个bundle:`vendor``main``manifest`
181180

182-
T> Note that long-term bundle caching is achieved with content-based hashing policy `chunkhash`. Learn more about [caching](/guides/caching/).
181+
T> 注意,长效的 bundle 缓存是通过“基于内容的 hash 策略”来实现的(content-based hashing)。查阅更多关于[缓存](/guides/caching/)
183182

184183
***
185184

0 commit comments

Comments
 (0)