Skip to content

Commit b697f74

Browse files
update master & update loaders & update plugins (#418)
* update /content/loaders & /content/plugins * update /content/loaders & /content/plugins * update /content/loaders & /content/plugins * update contributors * update /content/loaders & /content/plugins * fix LinkDropdown * 修复 npm 命令错误导致编译不成功的问题 * update /content/loaders & /content/plugins * update /content/loaders & /content/plugins * update /content/loaders & /content/plugins * docs(plugins): fix typo in module-concatenation-plugin.md (#1683) * docs(concepts): simplify the introduction (#1673) Make the `index` page more beginner friendly with less technical lingo and complex details. Resolves #1416 * docs(plugins): add “scope hoisting” intro in module-concatenation-plugin (#1684) This adds a link between “concatenation behavior” and “scope hoisting”. Without this, a new person might be confused what exactly “scope hoisting” is (because it appears without any visible connection to the previous content). * docs(api): fix some method signatures in loaders.md (#1685) In actual fact the parameters passed to `emitWarning` / `emitError` must be an instance of Error. * update /content/loaders & /content/plugins * docs(guides): consistent quoute use in typescript.md (#1687) * docs(api/guides): document new --concatenate-modules flag (#1686) Document the new `--concatenate-modules` flag in both the CLI documentation and production guide. Add section on the `ModuleConcatenationPlugin` in the production guide (as we include this plugin under `-p`, it also makes sense to mention it in this guide. * docs(guides): fix issues with examples in shimming.md (#1680) Rename plugin identifier and require webpack when it is used in the configuration examples. * docs(guides): add middleware tip to the hmr guide Resolves #1682 * Revert "A new --concatenate-modules flag" (#1692) * update master * update /content/loaders & /content/plugins * docs(concepts): fix grammar in output.md (#1694) * docs(contribute): update writing-a-loader (#1691) Use normal function instead of arrow function to fix scope in loader example. * docs(plugins): add external example in SourceMapDevToolPlugin (#1676) Demonstrate how one might use the plugin to host source maps externally. * docs(config): update dev-server open option (#1693) State the ability to open in specific browser. * fix bugs * update /content/loaders & /content/plugins * docs(api): improve formatting and grammar in loaders.md * docs(api): clarify fourth parameter of `this.callback` in loaders.md Add some lead in descriptions to the `Examples` section and clarify that meta data can be passed along via the fourth parameter to `this.callback` and accepted as the third parameter to a loader function. Resolves #1607 * docs(api): populate missing link in loaders.md * docs(plugins): correct example in html-webpack-plugin (#1698) * docs(guides): update an example in production.md (#1696) Switch to shortened form when using the `DefinePlugin` to define the `process.env.NODE_ENV` value. This avoids a bug which is mentioned in the plugin's documentation: https://webpack.js.org/plugins/define-plugin/#feature-flags * fix(markdown): fix overflowing inline code (#1701) Change the css to fix the text inside code tag which was overflowing the parent div. * docs(concepts): update concepts wording (#1702) Add "static" to "module bundler". Some feedback here was given to me on twitter to make sure we are clear with what these terms mean. * update /content/loaders & /content/plugins * docs(config): fix dead link to webpack-dev-server example (#1704) * docs(concepts): use fragment links in usage instructions (#1705) This is just a quality of life adjustment that updates the list of ways to use loaders with fragment links to the relevant section in the docs. In their current state, the section feels like a dead end, abruptly cutting off with three bullet points and no examples. While one can read on and figure it out, there is a break in focus that is quite distracting. * docs(guides): add windows usage tip in getting-started (#1671) * doc(guides): fix grammatical error in build-performance (#1709) Change "less" to "fewer". * docs(guides): correct two small typos * docs(api): remove inadvertent double verb (#1714) * docs(contribute): fix grammar in writing-a-plugin (#1715) * docs(config): add semicolon for consistency (#1716) The final code block was missing a semicolon from the end of the first line; added it in to match the other require statements. * docs(contributing): add note about debian OS (#1721) Related issue: #1718 * docs(guides): add output example to shimming doc (#1720) * docs(plugins): use `.includes` over `.indexOf` (#1719) Really minor but I think `.includes` is more readable to the unfamiliar with javascript. * docs(guides): use `npx` in getting-started (#1708) Keep the mention the webpack binary's path but use the `npx` utility now that it ships with Node. Fix some punctuation and grammar. Clarify why npm scripts are still useful even over `npx`. * update /src/content/loaders & /src/content/plugins
1 parent 60d7009 commit b697f74

25 files changed

+394
-208
lines changed

.github/CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ See the `package.json` for the full list of `scripts`.
3030
3131
> Note that a __Python version between v2.5.0 and 3.0.0__ is required for the [proselint][12] dependency.
3232
33+
> On Debian and Ubuntu operating systems you may have to use `node >= 7.0.0` to avoid build errors with `node-sass`. Please note that we don't officially support building on these systems.
34+
3335

3436
## Contributor License Agreement
3537

src/content/api/loaders.md

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,56 @@ contributors:
1515

1616
## 示例
1717

18+
The following sections provide some basic examples of the different types of loaders. Note that the `map` and `meta` parameters are optional, see [`this.callback`](/api/loaders#this-callback) below.
19+
1820
### 同步 loader
1921

20-
**sync-loader.js**
22+
Either `return` or `this.callback` can be used to return the transformed `content` synchronously:
2123

22-
```javascript
23-
module.exports = function(content) {
24+
__sync-loader.js__
25+
26+
``` js
27+
module.exports = function(content, map, meta) {
2428
return someSyncOperation(content);
2529
};
2630
```
2731

28-
**sync-loader-with-multiple-results.js**
32+
The `this.callback` method is more flexible as it allows multiple arguments to be passed as opposed to just the `content`.
2933

30-
```javascript
31-
module.exports = function(content) {
32-
this.callback(null, someSyncOperation(content), sourceMaps, ast);
33-
return; // 当调用callback()时总是返回undefined
34+
__sync-loader-with-multiple-results.js__
35+
36+
``` js
37+
module.exports = function(content, map, meta) {
38+
this.callback(null, someSyncOperation(content), sourceMaps, meta);
39+
return; // 当调用callback()时总是返回 undefined
3440
};
3541
```
3642

37-
3843
### 异步 loader
3944

40-
**async-loader.js**
45+
For asynchronous loaders, [`this.async`](/api/loaders#this-async) is used to retrieve the `callback` function:
4146

42-
```javascript
43-
module.exports = function(content) {
44-
var callback = this.async();
45-
someAsyncOperation(content, function(err, result) {
46-
if(err) return callback(err);
47-
callback(null, result);
48-
});
47+
__async-loader.js__
48+
49+
``` js
50+
module.exports = function(content, map, meta) {
51+
var callback = this.async();
52+
someAsyncOperation(content, function(err, result) {
53+
if (err) return callback(err);
54+
callback(null, result, map, meta);
55+
});
4956
};
5057
```
5158

52-
**async-loader-with-multiple-results.js**
59+
__async-loader-with-multiple-results.js__
5360

54-
```javascript
55-
module.exports = function(content) {
56-
var callback = this.async();
57-
someAsyncOperation(content, function(err, result, sourceMaps, ast) {
58-
if(err) return callback(err);
59-
callback(null, result, sourceMaps, ast);
60-
});
61+
``` js
62+
module.exports = function(content, map, meta) {
63+
var callback = this.async();
64+
someAsyncOperation(content, function(err, result, sourceMaps, meta) {
65+
if (err) return callback(err);
66+
callback(null, result, sourceMaps, meta);
67+
});
6168
};
6269
```
6370

@@ -68,9 +75,9 @@ T> loader 最初被设计为可以在同步 loader pipeline(如 Node.js ,使
6875

6976
默认情况下,资源文件会被转化为 UTF-8 字符串,然后传给 loader。通过设置 `raw`,loader 可以接收原始的 `Buffer`。每一个 loader 都可以用 `String` 或者 `Buffer` 的形式传递它的处理结果。Complier 将会把它们在 loader 之间相互转换。
7077

71-
**raw-loader.js**
78+
__raw-loader.js__
7279

73-
```javascript
80+
``` js
7481
module.exports = function(content) {
7582
assert(content instanceof Buffer);
7683
return someSyncOperation(content);
@@ -87,10 +94,11 @@ loader **总是**从右到左地被调用,但是在一些情况下,loader
8794

8895
如果中间某个 loader 的 `pitch` 方法返回了一个值,那么剩下的 loader 都会被跳过,转而从当前 loader 开始向左调用 loader。`data`可以在 pitch 和普通的 loader 调用间传递。
8996

90-
```javascript
97+
``` js
9198
module.exports = function(content) {
9299
return someSyncOperation(content, this.data.value);
93100
};
101+
94102
module.exports.pitch = function(remainingRequest, precedingRequest, data) {
95103
if(someCondition()) {
96104
// 直接返回
@@ -101,13 +109,13 @@ module.exports.pitch = function(remainingRequest, precedingRequest, data) {
101109
```
102110

103111

104-
## The loader context
112+
## loader 上下文
105113

106114
loader context 表示在 loader 内使用 `this` 可以访问的一些方法或属性
107115

108116
假设我们在 `/abc/file.js` 中这样请求加载别的模块:
109117

110-
```javascript
118+
``` js
111119
require("./loader1?xyz!loader2!./resource?rrr");
112120
```
113121

@@ -136,26 +144,28 @@ require("./loader1?xyz!loader2!./resource?rrr");
136144
1. 如果这个 loader 配置了 [`options`](/configuration/module/#useentry) 对象的话,`this.query` 就指向这个 option 对象。
137145
2. 如果 loader 中没有 `options`,而是以 query 字符串作为参数调用时,`this.query` 就是一个以 `?` 开头的字符串。
138146

139-
W> `options` 已取代 `query`,所以此属性废弃。使用`loader-utils`中的 [`getOptions` 方法](https://github.com/webpack/loader-utils#getoptions)来提取给定 loader 的 option。
147+
W> `options` 已取代 `query`,所以此属性废弃。使用 `loader-utils` 中的 [`getOptions` 方法](https://github.com/webpack/loader-utils#getoptions)来提取给定 loader 的 option。
140148

141149

142150
### `this.callback`
143151

144152
一个可以同步或者异步调用的可以返回多个结果的函数。预期的参数是:
145153

146-
```typescript
154+
``` js
147155
this.callback(
148-
err: Error | null,
149-
content: string | Buffer,
150-
sourceMap?: SourceMap,
151-
abstractSyntaxTree?: AST
156+
err: Error | null,
157+
content: string | Buffer,
158+
sourceMap?: SourceMap,
159+
meta?: any
152160
);
153161
```
154162

155163
1. 第一个参数必须是 `Error` 或者 `null`
156164
2. 第二个参数是一个 `string` 或者 [`Buffer`](https://nodejs.org/api/buffer.html)
157165
3. 可选的:第三个参数必须是一个可以被[这个模块](https://github.com/mozilla/source-map)解析的 source map。
158-
4. 可选的:`AST` 可以是给定语言的抽象语法树,比如 [`ESTree`](https://github.com/estree/estree)。这个值会被 webpack 自身忽略掉,但是如果你想在多个 loader 之间共用 AST 的时候对于加速构建非常有用。
166+
4. 可选的:The fourth option, ignored by webpack, can be anything (e.g. some meta data).
167+
168+
T> It can be useful to pass an abstract syntax tree (AST), like [`ESTree`](https://github.com/estree/estree), as the fourth argument (`meta`) to speed up the build time if you want to share common ASTs between loaders.
159169

160170
如果这个函数被调用的话,你应该返回 undefined 从而避免含糊的 loader 结果。
161171

@@ -174,7 +184,7 @@ this.callback(
174184

175185
设置是否可缓存标志的函数:
176186

177-
```typescript
187+
``` typescript
178188
cacheable(flag = true: boolean)
179189
```
180190

@@ -187,13 +197,13 @@ cacheable(flag = true: boolean)
187197

188198
所有 loader 组成的数组。它在 pitch 阶段的时候是可以写入的。
189199

190-
```typescript
200+
``` js
191201
loaders = [{request: string, path: string, query: string, module: function}]
192202
```
193203

194204
在我们的示例中:
195205

196-
```javascript
206+
``` js
197207
[
198208
{
199209
request: "/abc/loader1.js?xyz",
@@ -260,7 +270,7 @@ T> loader 最初被设计为可以同时当 Babel transform 用。如果你编
260270

261271
### `this.emitWarning`
262272

263-
```typescript
273+
``` typescript
264274
emitWarning(warning: Error)
265275
```
266276

@@ -269,7 +279,7 @@ emitWarning(warning: Error)
269279

270280
### `this.emitError`
271281

272-
```typescript
282+
``` typescript
273283
emitError(error: Error)
274284
```
275285

@@ -278,7 +288,7 @@ emitError(error: Error)
278288

279289
### `this.loadModule`
280290

281-
```typescript
291+
``` typescript
282292
loadModule(request: string, callback: function(err, source, sourceMap, module))
283293
```
284294

@@ -287,7 +297,7 @@ loadModule(request: string, callback: function(err, source, sourceMap, module))
287297

288298
### `this.resolve`
289299

290-
```typescript
300+
``` typescript
291301
resolve(context: string, request: string, callback: function(err, result: string))
292302
```
293303

@@ -296,7 +306,7 @@ resolve(context: string, request: string, callback: function(err, result: string
296306

297307
### `this.addDependency`
298308

299-
```typescript
309+
``` typescript
300310
addDependency(file: string)
301311
dependency(file: string) // 简写
302312
```
@@ -306,7 +316,7 @@ dependency(file: string) // 简写
306316

307317
### `this.addContextDependency`
308318

309-
```typescript
319+
``` typescript
310320
addContextDependency(directory: string)
311321
```
312322

@@ -315,7 +325,7 @@ addContextDependency(directory: string)
315325

316326
### `this.clearDependencies`
317327

318-
```typescript
328+
``` typescript
319329
clearDependencies()
320330
```
321331

@@ -324,7 +334,7 @@ clearDependencies()
324334

325335
### `this.emitFile`
326336

327-
```typescript
337+
``` typescript
328338
emitFile(name: string, content: Buffer|string, sourceMap: {...})
329339
```
330340
@@ -343,7 +353,7 @@ W> 强烈建议不要使用这些属性,因为我们打算移除它们。它
343353
344354
### `this.exec`
345355
346-
```typescript
356+
``` typescript
347357
exec(code: string, filename: string)
348358
```
349359
@@ -352,7 +362,7 @@ exec(code: string, filename: string)
352362
353363
### `this.resolveSync`
354364
355-
```typescript
365+
``` typescript
356366
resolveSync(context: string, request: string) -> string
357367
```
358368

src/content/concepts/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ contributors:
1010
- TheDutchCoder
1111
---
1212

13-
本质上,*webpack* 是一个现代 JavaScript 应用程序的_模块打包器(module bundler)_。当 webpack 处理应用程序时,它会递归地构建一个_依赖关系图(dependency graph)_,其中包含应用程序需要的每个模块,然后将所有这些模块打包成一个或多个 _bundle_
13+
本质上,*webpack* 是一个现代 JavaScript 应用程序的_静态模块打包器(module bundler)_。当 webpack 处理应用程序时,它会递归地构建一个_依赖关系图(dependency graph)_,其中包含应用程序需要的每个模块,然后将所有这些模块打包成一个或多个 _bundle_
1414

1515
它是[高度可配置的](/configuration),但是,在开始前你需要先理解四个**核心概念**
1616

src/content/concepts/loaders.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ module.exports = {
4343

4444
在你的应用程序中,有三种使用 loader 的方式:
4545

46-
* 配置(推荐):在 __webpack.config.js__ 文件中指定 loader。
47-
* 内联:在每个 `import` 语句中显式指定 loader。
48-
* CLI:在 shell 命令中指定它们。
46+
* [配置](#configuration)(推荐):在 __webpack.config.js__ 文件中指定 loader。
47+
* [内联](#inline):在每个 `import` 语句中显式指定 loader。
48+
* [CLI](#cli):在 shell 命令中指定它们。
4949

5050

5151
### 配置[Configuration]

src/content/concepts/output.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ contributors:
99

1010
配置 `output` 选项可以控制 webpack 如何向硬盘写入编译文件。注意,即使可以存在多个`入口`起点,但只指定一个`输出`配置。
1111

12+
1213
## 用法(Usage)
1314

1415
在 webpack 中配置 `output` 属性的最低要求是,将它的值设置为一个对象,包括以下两点:
@@ -36,7 +37,6 @@ module.exports = config;
3637

3738
如果配置创建了多个单独的 "chunk"(例如,使用多个入口起点或使用像 CommonsChunkPlugin 这样的插件),则应该使用[占位符(substitutions)](/configuration/output#output-filename)来确保每个文件具有唯一的名称。
3839

39-
4040
``` javascript
4141
{
4242
entry: {

src/content/configuration/dev-server.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Content not from webpack is served from /path/to/dist/
4040

4141
这将给出一些背景知识,就能知道服务器的访问位置,并且知道服务已启动。
4242

43-
如果你通过 Node.js API 来使用 dev-server, `devServer` 中的选项将被忽略。将选项作为第二个参数传入: `new WebpackDevServer(compiler, {...})`。关于如何通过 Node.js API 使用 webpack-dev-server 的示例,请[查看此处](https://github.com/webpack/webpack-dev-server/blob/master/examples/node-api-simple/server.js)
43+
如果你通过 Node.js API 来使用 dev-server, `devServer` 中的选项将被忽略。将选项作为第二个参数传入: `new WebpackDevServer(compiler, {...})`。关于如何通过 Node.js API 使用 webpack-dev-server 的示例,请[查看此处](https://github.com/webpack/webpack-dev-server/tree/master/examples/api/simple)
4444

4545
W> Be aware that when [exporting multiple configurations](/configuration/configuration-types/#exporting-multiple-configurations) only the `devServer` options for the first configuration will be taken into account and used for all the configurations in the array.
4646

@@ -465,6 +465,12 @@ Usage via the CLI
465465
webpack-dev-server --open
466466
```
467467

468+
If no browser is provided (as shown above), your default browser will be used. To specify a different browser, just pass its name:
469+
470+
```bash
471+
webpack-dev-server --open 'Google Chrome'
472+
```
473+
468474

469475
## `devServer.openPage`
470476

src/content/configuration/plugins.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ plugins: [
2929
一个复杂示例,使用多个插件,可能看起来就像这样:
3030

3131
```js
32-
var webpack = require('webpack')
32+
var webpack = require('webpack');
3333
// 导入非 webpack 默认自带插件
3434
var ExtractTextPlugin = require('extract-text-webpack-plugin');
3535
var DashboardPlugin = require('webpack-dashboard/plugin');

src/content/contribute/writing-a-loader.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ __src/loader.js__
234234
``` js
235235
import { getOptions } from 'loader-utils';
236236

237-
export default source => {
237+
export default function loader(source) {
238238
const options = getOptions(this);
239239

240240
source = source.replace(/\[name\]/g, options.name);

src/content/contribute/writing-a-plugin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ This is the simplest format for a plugin. Many useful events like `"compile"`, `
178178

179179
`applyPluginsWaterfall(name: string, init: any, args: any...)`
180180

181-
Here each of the plugins are called one after the other with the args from the return value of the previous plugin. The plugin must take into consider the order of its execution.
181+
Here each of the plugins are called one after the other with the args from the return value of the previous plugin. The plugin must take the order of its execution into account.
182182
It must accept arguments from the previous plugin that was executed. The value for the first plugin is `init`. This pattern is used in the Tapable instances which are related to the `webpack` templates like `ModuleTemplate`, `ChunkTemplate` etc.
183183

184184
- __asynchronous__ When all the plugins are applied asynchronously using

0 commit comments

Comments
 (0)