You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/api/node.md
+64-64Lines changed: 64 additions & 64 deletions
Original file line number
Diff line number
Diff line change
@@ -6,85 +6,85 @@ contributors:
6
6
- rynclark
7
7
---
8
8
9
-
webpack provides a Node.js API which can be used directly in Node.js runtime.
9
+
webpack 提供了 Node.js API,可以在 Node.js 运行时下直接使用。
10
10
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()`调用中生效。
12
12
13
-
## Installation
13
+
## 安装(Installation)
14
14
15
-
To start using webpack Node.js API, first install webpack if you haven’t yet:
15
+
开始使用 webpack 的 Node.js API 之前,首先你需要安装 webpack:
16
16
17
17
```
18
18
npm install webpack --save-dev
19
19
```
20
20
21
-
Then require the webpack module in your Node.js script:
21
+
然后在 Node.js 脚本中 `require` webpack 模块:
22
22
23
23
```js
24
24
constwebpack=require("webpack");
25
25
26
-
//Or if you prefer ES2015:
26
+
//或者如果你喜欢 ES2015:
27
27
importwebpackfrom"webpack";
28
28
```
29
29
30
30
## `webpack()`
31
31
32
-
The imported `webpack`function is fed a webpack [Configuration Object](/configuration/) and runs the webpack compiler if a callback function is provided:
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.
**Note**that you can provide the `webpack`function with an array of configurations:
49
+
**注意**你可以传入一个配置选项数组到 `webpack`函数内:
50
50
51
51
```js-with-links
52
52
webpack([
53
-
{ /* Configuration Object */ },
54
-
{ /* Configuration Object */ },
55
-
{ /* Configuration Object */ }
53
+
{ /* 配置对象 */ },
54
+
{ /* 配置对象 */ },
55
+
{ /* 配置对象 */ }
56
56
], (err, [stats](#stats-object)) => {
57
57
// ...
58
58
});
59
59
```
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).
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:
Calling the `run` method on the `Compiler` instance is much like the quick run method mentioned above:
71
+
调用 `Compiler` 实例的 `run` 方法跟上文提到的快速执行方法很相似:
72
72
73
73
```js-with-links
74
74
const webpack = require("webpack");
75
75
76
76
const compiler = webpack({
77
-
// [Configuration Object](/configuration/)
77
+
// [配置对象](/configuration/)
78
78
});
79
79
80
80
compiler.run((err, [stats](#stats-object)) => {
81
81
// ...
82
82
});
83
83
```
84
84
85
-
## Watching
85
+
## 监听(Watching)
86
86
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`.
T> It’s not allowed to watch or run again before the existing watcher has been closed or invalidated.
123
+
T> 不允许在当前监听器已经关闭或失效前再次监听或执行。
124
124
125
-
### Invalidate `Watching`
125
+
### 作废 `Watching`(Invalidate `Watching`)
126
126
127
-
Manually invalidate the current compiling round, but don’t stop watching.
127
+
手动将当前的编译阶段废弃,但不停止监听。
128
128
129
129
```js
130
130
watching.invalidate(() => {
131
131
console.warn("Invalidated.");
132
132
});
133
133
```
134
134
135
-
## Stats Object
135
+
## Stats 对象(Stats Object)
136
136
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:
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:
0 commit comments