Skip to content

Commit 6990d55

Browse files
Merge pull request webpack#124 from dickeylth/cn
add api/node translation done
2 parents d9fa9da + cc19e7b commit 6990d55

File tree

1 file changed

+64
-64
lines changed

1 file changed

+64
-64
lines changed

content/api/node.md

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,85 +6,85 @@ contributors:
66
- rynclark
77
---
88

9-
webpack provides a Node.js API which can be used directly in Node.js runtime.
9+
webpack 提供了 Node.js API,可以在 Node.js 运行时下直接使用。
1010

11-
The Node.js API is useful in scenarios in which you need to customize the build or development process since all the reporting and error handling must be done manually and webpack only does the compiling part. For this reason the [`stats`](/configuration/stats) configuration options will not have any effect in the `webpack()` call.
11+
当你需要自定义构建或开发流程时,Node.js API 非常有用,因为此时所有的报告和错误处理都必须自行实现,webpack 仅仅负责编译的部分。所以 [`stats`](/configuration/stats) 配置选项不会在 `webpack()` 调用中生效。
1212

13-
## Installation
13+
## 安装(Installation)
1414

15-
To start using webpack Node.js API, first install webpack if you haven’t yet:
15+
开始使用 webpack Node.js API 之前,首先你需要安装 webpack
1616

1717
```
1818
npm install webpack --save-dev
1919
```
2020

21-
Then require the webpack module in your Node.js script:
21+
然后在 Node.js 脚本中 `require` webpack 模块:
2222

2323
``` js
2424
const webpack = require("webpack");
2525

26-
// Or if you prefer ES2015:
26+
// 或者如果你喜欢 ES2015:
2727
import webpack from "webpack";
2828
```
2929

3030
## `webpack()`
3131

32-
The imported `webpack` function is fed a webpack [Configuration Object](/configuration/) and runs the webpack compiler if a callback function is provided:
32+
导入的 `webpack` 函数需要传入一个 webpack [配置对象](/configuration/),当同时传入回调函数时就会执行 webpack compiler
3333

3434
``` js-with-links
3535
const webpack = require("webpack");
3636
3737
webpack({
38-
// [Configuration Object](/configuration/)
38+
// [配置对象](/configuration/)
3939
}, (err, [stats](#stats-object)) => {
4040
if (err || stats.hasErrors()) {
41-
// [Handle errors here](#error-handling)
41+
// [在这里处理错误](#-error-handling-)
4242
}
43-
// Done processing
43+
// 处理完成
4444
});
4545
```
4646

47-
T> The `err` object **will not** include compilation errors and those must be handled separately using `stats.hasErrors()` which will be covered in detail in [Error Handling](#error-handling) section of this guide. The `err` object will only contain webpack-related issues, such as misconfiguration, etc.
47+
T> 编译错误****`err` 对象内,而是需要使用 `stats.hasErrors()` 单独处理,你可以在指南的 [错误处理](#-error-handling-) 部分查阅到更多细节。`err` 对象只会包含 webpack 相关的问题,比如配置错误等。
4848

49-
**Note** that you can provide the `webpack` function with an array of configurations:
49+
**注意** 你可以传入一个配置选项数组到 `webpack` 函数内:
5050

5151
``` js-with-links
5252
webpack([
53-
{ /* Configuration Object */ },
54-
{ /* Configuration Object */ },
55-
{ /* Configuration Object */ }
53+
{ /* 配置对象 */ },
54+
{ /* 配置对象 */ },
55+
{ /* 配置对象 */ }
5656
], (err, [stats](#stats-object)) => {
5757
// ...
5858
});
5959
```
60-
T> webpack will **not** run the multiple configurations in parallel. Each configuration is only processed after the previous one has finished processing. To have webpack process them in parallel, you can use a third-party solution like [parallel-webpack](https://www.npmjs.com/package/parallel-webpack).
60+
T> webpack ****会并行执行多个配置。每个配置只会在前一个处理结束后才会开始处理。如果你需要 webpack 并行执行它们,你可以使用像 [parallel-webpack](https://www.npmjs.com/package/parallel-webpack) 这样的第三方解决方案。
6161

62-
## Compiler Instance
62+
## Compiler 实例(Compiler Instance)
6363

64-
If you don’t pass the `webpack` runner function a callback, it will return a webpack `Compiler` instance. This instance can be used to manually trigger the webpack runner or have it build and watch for changes. Much like the [CLI](/api/cli/) Api. The `Compiler` instance provides the following methods:
64+
如果你不传入回调函数到 `webpack` 执行函数中,就会得到一个 webpack `Compiler` 实例。你可以通过它手动触发 webpack 执行器,或者是让它执行构建并监听变更。和 [CLI](/api/cli/) API 很类似。`Compiler` 实例提供了以下方法:
6565

6666
* `.run(callback)`
6767
* `.watch(watchOptions, handler)`
6868

69-
## Run
69+
## 执行(Run)
7070

71-
Calling the `run` method on the `Compiler` instance is much like the quick run method mentioned above:
71+
调用 `Compiler` 实例的 `run` 方法跟上文提到的快速执行方法很相似:
7272

7373
``` js-with-links
7474
const webpack = require("webpack");
7575
7676
const compiler = webpack({
77-
// [Configuration Object](/configuration/)
77+
// [配置对象](/configuration/)
7878
});
7979
8080
compiler.run((err, [stats](#stats-object)) => {
8181
// ...
8282
});
8383
```
8484

85-
## Watching
85+
## 监听(Watching)
8686

87-
Calling the `watch` method, triggers the webpack runner, but then watches for changes (much like CLI: `webpack --watch`), as soon as webpack detects a change, runs again. Returns an instance of `Watching`.
87+
调用 `watch` 方法会触发 webpack 执行器,但之后会监听变更(很像 CLI 命令: `webpack --watch`),一旦 webpack 检测到文件变更,就会重新执行编译。该方法返回一个 `Watching` 实例。
8888

8989
``` js-with-links
9090
watch(watchOptions, callback)
@@ -94,7 +94,7 @@ watch(watchOptions, callback)
9494
const webpack = require("webpack");
9595
9696
const compiler = webpack({
97-
// [Configuration Object](/configuration/)
97+
// [配置对象](/configuration/)
9898
});
9999
100100
const watching = compiler.watch({
@@ -103,62 +103,62 @@ const watching = compiler.watch({
103103
poll: undefined
104104
</details>
105105
}, (err, [stats](#stats-object)) => {
106-
// Print watch/build result here...
106+
// 在这里打印 watch/build 结果...
107107
console.log(stats);
108108
});
109109
```
110110

111-
`Watching` options are [covered in detail here](/configuration/watch/#watchoptions).
111+
`Watching` 配置选项的[细节可以在这里查阅](/configuration/watch/#watchoptions)
112112

113-
### Close `Watching`
113+
### 关闭 `Watching`(Close `Watching`)
114114

115-
The `watch` method returns a `Watching` instance that exposes `.close(callback)` method. Calling this method will end watching:
115+
`watch` 方法返回一个 `Watching` 实例,它会暴露一个 `.close(callback)` 方法。调用该方法将会结束监听:
116116

117117
``` js
118118
watching.close(() => {
119119
console.log("Watching Ended.");
120120
});
121121
```
122122

123-
T> It’s not allowed to watch or run again before the existing watcher has been closed or invalidated.
123+
T> 不允许在当前监听器已经关闭或失效前再次监听或执行。
124124

125-
### Invalidate `Watching`
125+
### 作废 `Watching`(Invalidate `Watching`)
126126

127-
Manually invalidate the current compiling round, but don’t stop watching.
127+
手动将当前的编译阶段废弃,但不停止监听。
128128

129129
``` js
130130
watching.invalidate(() => {
131131
console.warn("Invalidated.");
132132
});
133133
```
134134

135-
## Stats Object
135+
## Stats 对象(Stats Object)
136136

137-
The `stats` object that is passed as a second argument of the [`webpack()`](#webpack-) callback, is a good source of information about the code compilation process. It includes:
137+
`stats` 对象会被作为 [`webpack()`](#webpack-) 回调函数的第二个参数传入,可以通过它获取到代码编译过程中的有用信息,包括:
138138

139-
- Errors and Warnings (if any)
140-
- Timings
141-
- Module and Chunk information
142-
- etc.
139+
- 错误和警告(如有)
140+
- 计时
141+
- 模块和 chunk 信息
142+
- 其他信息
143143

144-
The [webpack CLI](/api/cli) uses this information to display a nicely formatted output in your console.
144+
[webpack CLI](/api/cli) 正是基于这些信息在控制台展示友好的格式输出。
145145

146-
This object exposes these methods:
146+
该对象暴露了以下方法:
147147

148148
### `stats.hasErrors()`
149149

150-
Can be used to check if there were errors while compiling. Returns `true` or `false`.
150+
可以用来检查编译期是否有错误,返回 `true` `false`
151151

152152
### `stats.hasWarnings()`
153153

154-
Can be used to check if there were warnings while compiling. Returns `true` or `false`.
154+
可以用来检查编译期是否有警告,返回 `true` `false`
155155

156156
### `stats.toJson(options)`
157157

158-
Returns compilation information as a JSON object. `options` can be either a string (a preset) or an object for more granular control:
158+
JSON 对象形式返回编译信息。`options` 可以是一个字符串(预设值)或是颗粒化控制的对象:
159159

160160
``` js-with-links
161-
stats.toJson("minimal"); // [more options: "verbose", etc](/configuration/stats).
161+
stats.toJson("minimal"); // [更多选项如: "verbose"](/configuration/stats).
162162
```
163163
``` js
164164
stats.toJson({
@@ -167,59 +167,59 @@ stats.toJson({
167167
});
168168
```
169169

170-
All available options and presets are described in [Stats documentation](/configuration/stats)
170+
所有可用的配置选项和预设值记录在 [Stats 文档](/configuration/stats)
171171

172-
> Here’s [an example of this function’s output](https://github.com/webpack/analyse/blob/master/app/pages/upload/example.json)
172+
> 这里有 [该函数输出的示例](https://github.com/webpack/analyse/blob/master/app/pages/upload/example.json)
173173
174174
### `stats.toString(options)`
175175

176-
Returns a formatted string of the compilation information (similar to [CLI](/api/cli) output).
176+
以格式化的字符串形式返回描述编译信息(类似 [CLI](/api/cli) 的输出)。
177177

178-
Options are the same as [`stats.toJson(options)`](/api/node#stats-tojson-options-) with one addition:
178+
配置对象与 [`stats.toJson(options)`](/api/node#stats-tojson-options-) 一致,除了额外增加的一个选项:
179179

180180
``` js
181181
stats.toString({
182182
// ...
183-
// Add console colors
183+
// 增加控制台颜色开关
184184
colors: true
185185
});
186186
```
187187

188-
Here’s an example of `stats.toString()` usage:
188+
下面是 `stats.toString()` 用法的示例:
189189

190190
``` js-with-links
191191
const webpack = require("webpack");
192192
193193
webpack({
194-
// [Configuration Object](/configuration/)
194+
// [配置对象](/configuration/)
195195
}, (err, stats) => {
196196
if (err) {
197197
console.error(err);
198198
return;
199199
}
200200
201201
console.log(stats.toString({
202-
chunks: false, // Makes the build much quieter
203-
colors: true // Shows colors in the console
202+
chunks: false, // 使构建过程更静默无输出
203+
colors: true // 在控制台展示颜色
204204
}));
205205
});
206206
```
207207

208-
## Error Handling
208+
## 错误处理(Error Handling)
209209

210-
For a good error handling, you need to account for these three types of errors:
210+
完备的错误处理中需要考虑以下三种类型的错误:
211211

212-
- Fatal webpack errors (wrong configuration, etc)
213-
- Compilation errors (missing modules, syntax errors, etc)
214-
- Compilation warnings
212+
- 致命的 wepback 错误(配置出错等)
213+
- 编译错误(缺失的模块,语法错误等)
214+
- 编译警告
215215

216-
Here’s an example that does all that:
216+
下面是一个覆盖这些场景的示例:
217217

218218
``` js-with-links
219219
const webpack = require("webpack");
220220
221221
webpack({
222-
// [Configuration Object](/configuration/)
222+
// [配置对象](/configuration/)
223223
}, (err, stats) => {
224224
if (err) {
225225
console.error(err.stack || err);
@@ -239,13 +239,13 @@ webpack({
239239
console.warn(info.warnings)
240240
}
241241
242-
// Log result...
242+
// 记录结果...
243243
});
244244
```
245245

246-
## Compiling to Memory
246+
## 编译到内存中(Compiling to Memory)
247247

248-
webpack writes the output to the specified files on disk. If you want webpack to output them to a different kind of file system (memory, webDAV, etc), you can set the `outputFileSystem` option on the compiler:
248+
webpack 默认将输出写入到磁盘上指定的文件中。如果你希望 webpack 将它们写入到其他类型的文件系统中(比如内存、webDAV 等),你可以在 compiler 上设置 `outputFileSystem` 选项:
249249

250250
``` js
251251
const MemoryFS = require("memory-fs");
@@ -256,12 +256,12 @@ const compiler = webpack({ /* options*/ });
256256

257257
compiler.outputFileSystem = fs;
258258
compiler.run((err, stats) => {
259-
// Read the output later:
259+
// 之后读取输出:
260260
const content = fs.readFileSync("...");
261261
});
262262
```
263263

264-
T> The output file system you provide needs to be compatible with Node’s own [`fs`](https://nodejs.org/api/fs.html) module interface.
264+
T> 你指定的输出文件系统需要兼容 Node 自身的 [`fs`](https://nodejs.org/api/fs.html) 模块接口。
265265

266266
***
267267

0 commit comments

Comments
 (0)