Skip to content

Commit d6724e2

Browse files
Merge pull request webpack#104 from lcxfs1991/cn
bundle-loader and code-splitting-require
2 parents 832760a + fb1c619 commit d6724e2

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

content/guides/code-splitting-require.md

100644100755
Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@ contributors:
66
- rahulcs
77
---
88

9-
In this section, we will discuss how webpack splits code using `require.ensure()`.
9+
在这一节,我们会讨论 webpack 如何使用 `require.ensure()` 进行代码拆分。
1010

1111
## `require.ensure()`
1212

13-
webpack statically parses for `require.ensure()` in the code while building and adds the modules here into a separate chunk. This new chunk is loaded on demand by webpack through jsonp.
13+
webpack 在编译时,会静态地解析代码中的 `require.ensure()`,同时将模块添加到一个分开的 chunk 当中。这个新的 chunk 会被 webpack 通过 `jsonp` 来按需加载。
1414

15-
The syntax is as follows
15+
语法如下:
1616

1717
```javascript
1818
require.ensure(dependencies: String[], callback: function(require), chunkName: String)
1919
```
2020

21-
#### dependencies
22-
This is an array of strings where we can declare all the modules that need to be made available before all the code in the callback function can be executed.
21+
#### 依赖 dependencies
22+
这是一个字符串数组,通过这个参数,在所有的回调函数的代码被执行前,我们可以将所有需要用到的模块进行声明。
2323

24-
#### callback
25-
This is the callback function that webpack will execute once the dependencies are loaded. An implementation of the require object is sent as a parameter to this function. This is so that, we can further `require()` the dependencies and any other modules for execution.
24+
#### 回调 callback
25+
当所有的依赖都加载完成后,webpack会执行这个回调函数。`require` 对象的一个实现会作为一个参数传递给这个回调函数。因此,我们可以进一步 `require()` 依赖和其它模块提供下一步的执行。
2626

27-
#### chunkName
28-
The chunkName is the name given to the chunk created by this particular `require.ensure()`. By giving the same name at different split points of `require.ensure()`, we can make sure all the dependencies are collectively put in the same bundle.
27+
#### chunk名称 chunkName
28+
chunkName 是提供给这个特定的 `require.ensure()` 的 chunk 的名称。通过提供 `require.ensure()` 不同执行点相同的名称,我们可以保证所有的依赖都会一起放进相同的 文件束(bundle)。
2929

30-
Let us consider the following project
30+
让我们来看以下的项目
3131

3232
```bash
3333
\\ file structure
@@ -69,36 +69,37 @@ module.exports = function(env) {
6969
}
7070
}
7171
```
72-
On running webpack on this project, we find that webpack has created two new bundles, `bundle.js` and `0.bundle.js`.
72+
通过执行这个项目的 webpack 构建,我们发现 webpack 创建了2个新的文件束, `bundle.js` `0.bundle.js`
7373

74-
`entry.js` and `a.js` are bundled in `bundle.js`.
74+
`entry.js` `a.js` 被打包进 `bundle.js`.
7575

76-
`b.js` is bundled in `0.bundle.js`.
76+
`b.js` 被打包进 `0.bundle.js`.
7777

78-
?> `require.ensure` relies on `Promises` internally. If you use `require.ensure` with older browsers, remember to shim `Promise.` [es6-promise polyfill](https://github.com/stefanpenner/es6-promise).
78+
?> `require.ensure` 内部依赖于 `Promises`。 如果你在旧的浏览器中使用 `require.ensure` 请记得 去 shim `Promise.` [es6-promise polyfill](https://github.com/stefanpenner/es6-promise).
7979

80-
## Gotchas for `require.ensure()`
80+
## `require.ensure()` 的坑点
8181

82-
### Empty Array as Parameter
82+
### 空数组作为参数
8383

8484
```javascript
8585
require.ensure([], function(require){
8686
require('./a.js');
8787
});
8888
```
8989

90-
The above code ensures that a split point is created and `a.js` is bundled separately by webpack.
90+
以上代码保证了拆分点被创建,而且 `a.js` webpack 分开打包。
9191

92-
### Dependencies as Parameter
92+
### 依赖作为参数
9393

9494
```javascript
9595
require.ensure(['./a.js'], function(require) {
9696
require('./b.js');
9797
});
9898
```
9999

100-
In the above code, `a.js` and `b.js` are bundled together and split from the main bundle. But only the contents of `b.js` are executed. The contents of `a.js` are only made available and not executed.
101-
To execute `a.js`, we will have to require it in a sync manner like `require('./a.js')` for the JavaScript to get executed.
100+
上面代码, `a.js``b.js` 都被打包到一起,而且从主文件束中拆分出来。但只有 `b.js` 的内容被执行。`a.js` 的内容仅仅是可被使用,但并没有被输出。
101+
102+
想去执行 `a.js`,我们需要异步地引用它,如 `require('./a.js')`,让它的 JavaScritp 被执行。
102103

103104
***
104105

generated/loaders/bundle-loader.md

100644100755
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,37 @@ title: bundle-loader
33
source: https://raw.githubusercontent.com/webpack/bundle-loader/master/README.md
44
edit: https://github.com/webpack/bundle-loader/edit/master/README.md
55
---
6-
# bundle loader for webpack
6+
# webpack 的文件束加载器
77

88
## Usage
99

10-
[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)
10+
[文档: 使用加载器](http://webpack.github.io/docs/using-loaders.html)
1111

1212
``` javascript
13-
// The chunk is requested, when you require the bundle
13+
// 当你引用 文件束 (bundle) 的时候,chunk 会被浏览器加载。
1414
var waitForChunk = require("bundle-loader!./file.js");
1515

16-
// To wait until the chunk is available (and get the exports)
17-
// you need to async wait for it.
16+
// 为了等待 chunk 的加载完成 (而且为了拿到 exports 输出)
17+
// 你需要异步去等待它
1818
waitForChunk(function(file) {
19-
// use file like is was required with
19+
// 这里可以使用file,就像是用下面的代码require进来一样
2020
// var file = require("./file.js");
2121
});
22-
// wraps the require in a require.ensure block
22+
// require 包裹在 require.ensure 的代码块中
2323
```
2424

25-
The file is requested when you require the bundle loader. If you want it to request it lazy, use:
25+
当你引用文件束的时候,chunk 会被浏览器加载。如果你想它懒加载,请用:
2626

2727
``` javascript
2828
var load = require("bundle-loader?lazy!./file.js");
2929

30-
// The chunk is not requested until you call the load function
30+
// 文件束不会被加载,除非你调用了 call 函数
3131
load(function(file) {
3232

3333
});
3434
```
3535

36-
You may set name for bundle (`name` query parameter). See [documentation](https://github.com/webpack/loader-utils#interpolatename).
36+
你可能会给文件束设名称(`name` 查询参数)。请查看[documentation](https://github.com/webpack/loader-utils#interpolatename).
3737

3838
``` javascript
3939
require("bundle-loader?lazy&name=my-chunk!./file.js");

0 commit comments

Comments
 (0)