Skip to content

Commit c0d31e6

Browse files
committed
翻译 guides/code-splitting-libraries.md
1 parent 60bf5b9 commit c0d31e6

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

content/guides/code-splitting-libraries.md

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
---
2-
title: 代码拆分 - Libraries
2+
title: 代码拆分 - 库(Libraries)
33
sort: 4
44
contributors:
55
- pksjce
66
- chrisVillanueva
77
- johnstew
88
- rafde
99
---
10+
一个典型的应用程序会使用第三方框架、工具库。这些库一般使用特定的版本并且其代码不经常改变。然而,应用本身的代码会频繁改变。
1011

11-
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.
12+
把第三方代码和应用本身的代码一起打包是非常低效的。因为浏览器会根据缓存头来缓存资源文件,如果文件没有被改变,文件将会被缓存从而不用去再次请求 cdn。为了利用这一特性,我们希望不管应用本身的代码如何改变,vendor 文件的 hash 始终恒定不变。
1213

13-
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.
14+
只有当我们把 vendor 和应用代码的 bundle 分离时,才能实现这一点。
1415

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

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

2120
`npm install --save moment`
2221

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

2524
__index.js__
2625
```javascript
@@ -29,7 +28,7 @@ console.log(moment().format());
2928

3029
```
3130

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

3433
__webpack.config.js__
3534

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

50-
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`.
49+
在项目中运行 `webpack`,如果你查看生成的包,会发现 `moment` `index.js` 都被打包进了 `bundle.js`
5150

52-
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.
51+
这对于该应用来说是很不理想的。如果 `index.js` 中的代码改变了,那么整个 bundle 都会重新构建。浏览器就需要加载新的 bundle,即使其中大部分代码都没改变。
5352

54-
## Multiple Entries
53+
## 多入口
5554

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

5857
__webpack.config.js__
5958

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

77-
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!
76+
再次运行 `webpack`,可以发现生成了两个 bundle。然而如果查看他们的代码,会发现 `moment` 的代码在两个文件中都出现了!
7877

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

8180
## `CommonsChunkPlugin`
8281

83-
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.
82+
这是一个非常复杂的插件。它从根本上允许我们从不同的 bundle 中提取所有的公共模块,并且将他们加入公共 bundle 中。如果公共 bundle 不存在,那么它将会创建一个出来。
8483

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

8786
__webpack.config.js__
8887

@@ -102,17 +101,17 @@ module.exports = function(env) {
102101
},
103102
plugins: [
104103
new webpack.optimize.CommonsChunkPlugin({
105-
name: 'vendor' // Specify the common bundle's name.
104+
name: 'vendor' // 指定公共 bundle 的名字。
106105
})
107106
]
108107
}
109108
}
110109
```
111-
Now run `webpack` on your application. Bundle inspection shows that `moment` code is present only in the vendor bundle.
110+
现在运行 `webpack`。查看结果会发现 `moment` 代码只会出现在 vendor bundle 中。
112111

113-
## Implicit Common Vendor Chunk
112+
## 隐式公共 vendor chunk
114113

115-
You can configure a `CommonsChunkPlugin` instance to only accept vendor libraries.
114+
你可以将 `CommonsChunkPlugin` 配置为只接受 vendor 库。
116115

117116
__webpack.config.js__
118117

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

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

147-
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.
148-
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.
147+
这意味着我们任然无法从浏览器缓存机制中受益,因为 vendor 的 hash 在每次构建中都会改变,浏览器也必须重新加载文件。
149148

150-
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.
149+
这里的问题在于,每次构建时,webpack 生成了一些 webpack runtime 代码,用来帮助 webpack 完成其工作。当只有一个 bundle 的时候,runtime 代码驻留在其中。但是当生成多个 bundle 的时候,运行时代码被提取到了公共模块中,在这里就是 `vendor` 文件。
151150

152-
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.
151+
为了防止这种情况,我们需要将运行时代码提取到一个单独的 manifest 文件中。尽管我们又创建了另一个 bundle,其开销也被我们在 `vendor` 文件的长期缓存中获得的好处所抵消。
153152

154153
__webpack.config.js__
155154

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

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

181180
***
182181

0 commit comments

Comments
 (0)