Skip to content

docs(cn): translate src/content/api/loaders.md #951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 3, 2021

Conversation

jacob-lcs
Copy link
Member

describe your changes...

  • Read and sign the CLA. PRs that haven't signed it won't be accepted.
  • Make sure your PR complies with the writer's guide.
  • Review the diff carefully as sometimes this can reveal issues.
  • Do not abandon your Pull Request: Stale Pull Requests.
  • Remove these instructions from your PR as they are for your eyes only.

@jacob-lcs
Copy link
Member Author

#755 长时间未进行提交,故基于其翻译进行修改
@QC-L

@jacob-lcs jacob-lcs requested a review from QC-L January 31, 2021 12:41
@QC-L
Copy link
Member

QC-L commented Feb 1, 2021

收到~我马上看下

@@ -16,20 +16,20 @@ contributors:
- jamesgeorge007
---

A loader is just a JavaScript module that exports a function. The [loader runner](https://github.com/webpack/loader-runner) calls this function and passes the result of the previous loader or the resource file into it. The `this` context of the function is filled-in by webpack and the [loader runner](https://github.com/webpack/loader-runner) with some useful methods that allow the loader (among other things) to change its invocation style to async, or get query parameters.
loader 是一个导出函数的 JavaScript 模块。[loader runner](https://github.com/webpack/loader-runner) 会调用这个函数,然后把上一个 loader 产生的结果或者资源文件传入进去。函数的 `this` 上下文将由 webpack 填充,并且 [loader runner](https://github.com/webpack/loader-runner) 有一些有用的方法,可以使 loader 改变为异步调用方式,或者获取 query 参数。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
loader 是一个导出函数的 JavaScript 模块。[loader runner](https://github.com/webpack/loader-runner) 会调用这个函数,然后把上一个 loader 产生的结果或者资源文件传入进去。函数的 `this` 上下文将由 webpack 填充,并且 [loader runner](https://github.com/webpack/loader-runner) 有一些有用的方法,可以使 loader 改变为异步调用方式,或者获取 query 参数。
loader 本质上是导出为函数的 JavaScript 模块。[loader runner](https://github.com/webpack/loader-runner) 会调用此函数,然后将上一个 loader 产生的结果或者资源文件传入进去。函数中的 `this` 作为上下文会被 webpack 填充,并且 [loader runner](https://github.com/webpack/loader-runner) 中包含一些实用的方法,比如可以使 loader 调用方式变为异步,或者获取 query 参数。


The first loader is passed one argument: the content of the resource file. The compiler expects a result from the last loader. The result should be a `String` or a `Buffer` (which is converted to a string), representing the JavaScript source code of the module. An optional SourceMap result (as a JSON object) may also be passed.
第一个 loader 只有一个传入参数:资源文件的内容。compiler 预期得到最后一个 loader 产生的处理结果。这个处理结果应该是 `String` 或者 `Buffer`(能够被转换为 string),代表了模块的 JavaScript 源码。另外,还可以传递一个可选的 SourceMap 结果(格式为 JSON 对象)。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
第一个 loader 只有一个传入参数:资源文件的内容。compiler 预期得到最后一个 loader 产生的处理结果。这个处理结果应该是 `String` 或者 `Buffer`(能够被转换为 string),代表了模块的 JavaScript 源码。另外,还可以传递一个可选的 SourceMap 结果(格式为 JSON 对象)。
起始 loader 只有一个入参:资源文件的内容。compiler 预期得到最后一个 loader 产生的处理结果。这个处理结果应该为 `String` 或者 `Buffer`(能够被转换为 string)类型,代表了模块的 JavaScript 源码。另外,还可以传递一个可选的 SourceMap 结果(格式为 JSON 对象)。


A single result can be returned in __sync mode__. For multiple results the `this.callback()` must be called. In __async mode__ `this.async()` must be called to indicate that the [loader runner](https://github.com/webpack/loader-runner) should wait for an asynchronous result. It returns `this.callback()`. Then the loader must return `undefined` and call that callback.
如果是单个处理结果,可以在__同步模式__中直接返回。如果有多个处理结果,则必须调用 `this.callback()`。在__异步模式__中,必须调用 `this.async()` 来指示 [loader runner](https://github.com/webpack/loader-runner) 等待异步结果,它会返回 `this.callback()` 回调函数。随后 loader 必须返回 `undefined` 并且调用该回调函数。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
如果是单个处理结果,可以在__同步模式__中直接返回。如果有多个处理结果,则必须调用 `this.callback()`__异步模式__,必须调用 `this.async()` 来指示 [loader runner](https://github.com/webpack/loader-runner) 等待异步结果,它会返回 `this.callback()` 回调函数。随后 loader 必须返回 `undefined` 并且调用该回调函数。
如果是单个处理结果,可以在 __同步模式__ 中直接返回。如果有多个处理结果,则必须调用 `this.callback()`__异步模式__,必须调用 `this.async()` 来告知 [loader runner](https://github.com/webpack/loader-runner) 等待异步结果,它会返回 `this.callback()` 回调函数。随后 loader 必须返回 `undefined` 并且调用该回调函数。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要加个空格,不然 __ 无法解析

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块我过两天看看改下编译(英文间有空格,中文没有,就会有问题)

@@ -78,33 +78,33 @@ module.exports = function(content, map, meta) {
};
```

T> Loaders were originally designed to work in synchronous loader pipelines, like Node.js (using [enhanced-require](https://github.com/webpack/enhanced-require)), _and_ asynchronous pipelines, like in webpack. However, since expensive synchronous computations are a bad idea in a single-threaded environment like Node.js, we advise making your loader asynchronous if possible. Synchronous loaders are ok if the amount of computation is trivial.
T> loader 最初被设计为可以在同步 loader pipelines(如 Node.js ,使用 [enhanced-require](https://github.com/webpack/enhanced-require)),_以及_在异步 pipelines(如 webpack )中运行。然而在 Node.js 这样的单线程环境下进行耗时长的同步计算不是个好主意,我们建议尽可能地使你的 loader 异步化。但如果计算量很小,同步 loader 也是可以的。
Copy link
Member

@QC-L QC-L Feb 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
T> loader 最初被设计为可以在同步 loader pipelines(如 Node.js ,使用 [enhanced-require](https://github.com/webpack/enhanced-require)),_以及_在异步 pipelines(如 webpack )中运行。然而在 Node.js 这样的单线程环境下进行耗时长的同步计算不是个好主意,我们建议尽可能地使你的 loader 异步化。但如果计算量很小,同步 loader 也是可以的。
T> loader 最初被设计为可以在同步 loader pipelines(如 Node.js ,使用 [enhanced-require](https://github.com/webpack/enhanced-require)),_以及_ 在异步 pipelines(如 webpack)中运行。然而,由于同步计算过于耗时,在 Node.js 这样的单线程环境下进行此操作并不是好的方案,我们建议尽可能地使你的 loader 异步化。但如果计算量很小,同步 loader 也是可以的。

// return value can be a `Buffer` too
// This is also allowed if loader is not "raw"
// 返回值也可以是一个 `Buffer`
// 即使不是 raw loader 也没问题
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 即使不是 raw loader 也没问题
// 即使不是 "raw",loader 也没问题

};
module.exports.raw = true;
```


### Pitching Loader {#pitching-loader}

Loaders are __always__ called from right to left. There are some instances where the loader only cares about the __metadata__ behind a request and can ignore the results of the previous loader. The `pitch` method on loaders is called from __left to right__ before the loaders are actually executed (from right to left).
loader __总是__从右到左被调用。有些情况下,loader 只关心 request 后面的__元数据(metadata)__,并且忽略前一个 loader 的结果。在实际(从右到左)执行 loader 之前,会先__从左到右__调用 loader 上的 `pitch` 方法。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
loader __总是__从右到左被调用。有些情况下,loader 只关心 request 后面的__元数据(metadata)__,并且忽略前一个 loader 的结果。在实际(从右到左)执行 loader 之前,会先__从左到右__调用 loader 上的 `pitch` 方法。
loader __总是__ 从右到左被调用。有些情况下,loader 只关心 request 后面的 __元数据(metadata)__,并且忽略前一个 loader 的结果。在实际(从右到左)执行 loader 之前,会先 __从左到右__ 调用 loader 上的 `pitch` 方法。


T> Loaders may be added inline in requests and disabled via inline prefixes, which will impact the order in which they are "pitched" and executed. See [`Rule.enforce`](/configuration/module/#ruleenforce) for more details.
T> loader 可以通过 request 添加或者禁用内联前缀,这将影响到 pitch 和 执行的顺序。更多详情查看 [`Rule.enforce`](/configuration/module/#ruleenforce)
Copy link
Member

@QC-L QC-L Feb 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
T> loader 可以通过 request 添加或者禁用内联前缀,这将影响到 pitch 和 执行的顺序。更多详情查看 [`Rule.enforce`](/configuration/module/#ruleenforce)
T> loader 可以通过 request 添加或者禁用内联前缀,这将影响到 pitch 和执行的顺序。更多详情请查阅 [`Rule.enforce`](/configuration/module/#ruleenforce)

@@ -136,9 +136,9 @@ These steps would occur:
|- a-loader normal execution
```

So why might a loader take advantage of the "pitching" phase?
那么,为什么 loader 可以利用 "跳跃(pitching)" 阶段呢?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
那么,为什么 loader 可以利用 "跳跃(pitching)" 阶段呢?
那么,为什么 loader 可以利用 "pitching" 阶段呢?


First, the `data` passed to the `pitch` method is exposed in the execution phase as well under `this.data` and could be useful for capturing and sharing information from earlier in the cycle.
首先,传递给 `pitch` 方法的 `data`,在执行阶段也会暴露在 `this.data` 之下,并且可以用于在循环时,捕获和共享前面的信息。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
首先,传递给 `pitch` 方法的 `data`,在执行阶段也会暴露在 `this.data` 之下,并且可以用于在循环时,捕获和共享前面的信息
首先,传递给 `pitch` 方法的 `data`,在执行阶段也会暴露在 `this.data` 之下,并且可以用于在循环时,捕获并共享前面的信息


In the example: `'/abc/loader1.js?xyz!/abc/node_modules/loader2/index.js!/abc/resource.js?rrr'`
在我们的例子中:`"/abc/loader1.js?xyz!/abc/node_modules/loader2/index.js!/abc/resource.js?rrr"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
在我们的例子中:`"/abc/loader1.js?xyz!/abc/node_modules/loader2/index.js!/abc/resource.js?rrr"`
在我们的例子中:`'/abc/loader1.js?xyz!/abc/node_modules/loader2/index.js!/abc/resource.js?rrr'`

@@ -564,7 +564,7 @@ module.exports = function(source) {
};
```

The module will get bundled like this:
这个模块将获取像下面的 bundle
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
这个模块将获取像下面的 bundle
这个模块将获取像下面的 bundle


T> Since webpack 5, `this.getOptions` is available in loader context. It substitutes `getOptions` method from [loader-utils](https://github.com/webpack/loader-utils#getoptions).
T> webpack 5 开始, `this.getOptions` 可以获取到 loader 上下文对象。 它用来替代来自 [loader-utils](https://github.com/webpack/loader-utils#getoptions) 中的 `getOptions` 方法。
Copy link
Member

@QC-L QC-L Feb 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
T> 从 webpack 5 开始, `this.getOptions` 可以获取到 loader 上下文对象。 它用来替代来自 [loader-utils](https://github.com/webpack/loader-utils#getoptions) 中的 `getOptions` 方法。
T> 从 webpack 5 开始,`this.getOptions` 可以获取到 loader 上下文对象。它用来替代来自 [loader-utils](https://github.com/webpack/loader-utils#getoptions) 中的 `getOptions` 方法。



### `this.callback` {#thiscallback}

A function that can be called synchronously or asynchronously in order to return multiple results. The expected arguments are:
一个可以同步或者异步调用的可以返回多个结果的函数。预期的参数是:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@@ -362,15 +362,15 @@ Here is a Warning!
@ ./src/index.js 1:0-25
```

T> Note that the warnings will not be displayed if `stats.warnings` is set to `false`, or some other omit setting is used to `stats` such as `none` or `errors-only`. See the [stats presets configuration](/configuration/stats/#stats-presets).
T> 请注意,如果 `stats.warnings` 设置为 `false`,警告信息将不会显示。 或者其他一些省略设置被用做 `status`, 例如 `none` 或者 `errors-only`
Copy link
Member

@QC-L QC-L Feb 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
T> 请注意,如果 `stats.warnings` 设置为 `false`,警告信息将不会显示。 或者其他一些省略设置被用做 `status`, 例如 `none` 或者 `errors-only`
T> 请注意,如果 `stats.warnings` 设置为 `false`,警告信息将不会显示。或者其他一些省略设置被用做 `status`例如 `none` 或者 `errors-only`

@@ -379,7 +379,7 @@ Here is an Error!
@ ./src/index.js 1:0-25
```

T> Unlike throwing an Error directly, it will NOT interrupt the compilation process of the current module.
T> 与抛出错误中断运行不同, 它不会中断当前模块的编译过程。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
T> 与抛出错误中断运行不同, 它不会中断当前模块的编译过程。
T> 与抛出错误中断运行不同,它不会中断当前模块的编译过程。

@@ -388,9 +388,9 @@ T> Unlike throwing an Error directly, it will NOT interrupt the compilation proc
loadModule(request: string, callback: function(err, source, sourceMap, module))
```

Resolves the given request to a module, applies all configured loaders and calls back with the generated source, the sourceMap and the module instance (usually an instance of [`NormalModule`](https://github.com/webpack/webpack/blob/master/lib/NormalModule.js)). Use this function if you need to know the source code of another module to generate the result.
解析给定的 request 到一个模块,应用所有配置的 loader ,并且在回调函数中传入生成的 sourcesourceMap 和 模块实例(通常是 [`NormalModule`](https://github.com/webpack/webpack/blob/master/lib/NormalModule.js) 的一个实例)。如果你需要获取其他模块的源代码来生成结果的话,你可以使用这个函数。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
解析给定的 request 到一个模块,应用所有配置的 loader ,并且在回调函数中传入生成的 source 、sourceMap 和 模块实例(通常是 [`NormalModule`](https://github.com/webpack/webpack/blob/master/lib/NormalModule.js) 的一个实例)。如果你需要获取其他模块的源代码来生成结果的话,你可以使用这个函数。
解析给定的 request 到模块,应用所有配置的 loader,并且在回调函数中传入生成的 source、sourceMap 和模块实例(通常是 [`NormalModule`](https://github.com/webpack/webpack/blob/master/lib/NormalModule.js) 的一个实例)。如果你需要获取其他模块的源代码来生成结果的话,你可以使用这个函数。

@@ -399,13 +399,13 @@ Resolves the given request to a module, applies all configured loaders and calls
resolve(context: string, request: string, callback: function(err, result: string))
```

Resolve a request like a require expression.
像 require 表达式一样解析一个 request
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
像 require 表达式一样解析一个 request
像 require 表达式一样解析一个 request。

- `request` is the request to be resolved. Usually either relative requests like `./relative` or module requests like `module/path` are used, but absolute paths like `/some/path` are also possible as requests.
- `callback` is a normal Node.js-style callback function giving the resolved path.
- `context` 必须是一个目录的绝对路径。此目录用作解析的起始位置。
- `request` 是要被解析的 request。 通常情况下,像 `./relative` 的相对请求或者像 `module/path` 的模块请求会被使用,但是像 `/some/path` 也有可能被当做 request。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `request` 是要被解析的 request。 通常情况下,像 `./relative` 的相对请求或者像 `module/path` 的模块请求会被使用,但是像 `/some/path` 也有可能被当做 request。
- `request` 是要被解析的 request。通常情况下,像 `./relative` 的相对请求或者像 `module/path` 的模块请求会被使用,但是像 `/some/path` 也有可能被当做 request。


Any options under webpack [`resolve` options](/configuration/resolve/#resolve) are possible. They are merged with the configured `resolve` options. Note that `"..."` can be used in arrays to extend the value from `resolve` options, e.g. `{ extensions: [".sass", "..."] }`.
webpack [`resolve` options](/configuration/resolve/#resolve) 下的任意配置项都是可能的。他们会被合并进 `resolve` 配置项中。请注意,`"..."` 可以在数组中使用,用于拓展 `resolve` 配置项的值。例如:`{ extensions: [".sass", "..."] }`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
在 webpack [`resolve` options](/configuration/resolve/#resolve) 下的任意配置项都是可能的。他们会被合并进 `resolve` 配置项中。请注意,`"..."` 可以在数组中使用,用于拓展 `resolve` 配置项的值。例如:`{ extensions: [".sass", "..."] }`
在 webpack [`resolve` 选项](/configuration/resolve/#resolve) 下的任意配置项都是可能的。他们会被合并进 `resolve` 配置项中。请注意,`"..."` 可以在数组中使用,用于拓展 `resolve` 配置项的值。例如:`{ extensions: [".sass", "..."] }`

- the request string: `(./src/loader.js!./src/lib.js)`
- the loader path: `(from ./src/loader.js)`
- the caller path: `@ ./src/index.js 1:0-25`
- 模块路径: `ERROR in ./src/lib.js`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 模块路径: `ERROR in ./src/lib.js`
- 模块路径`ERROR in ./src/lib.js`



### Inline matchResource {#inline-matchresource}

A new inline request syntax was introduced in webpack v4. Prefixing `<match-resource>!=!` to a request will set the `matchResource` for this request.
在webpack v4中引入了一种新的内联请求语法。前缀为 `<match-resource>!=!` 将为此请求设置 `matchResource`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
在webpack v4中引入了一种新的内联请求语法。前缀为 `<match-resource>!=!` 将为此请求设置 `matchResource`
在 webpack v4 中引入了一种新的内联请求语法。前缀为 `<match-resource>!=!` 将为此请求设置 `matchResource`

@@ -624,7 +624,7 @@ __file.js__
console.log('yep');
```

A loader could transform the file into the following file and use the `matchResource` to apply the user-specified CSS processing rules:
loader 可以将文件转换为以下文件,并使用 `matchResource` 应用用户指定的CSS处理规则:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
loader 可以将文件转换为以下文件,并使用 `matchResource` 应用用户指定的CSS处理规则
loader 可以将文件转换为以下文件,并使用 `matchResource` 应用用户指定的 CSS 处理规则

Copy link
Member

@QC-L QC-L left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改一下吧,问题有点多

@jacob-lcs jacob-lcs requested a review from QC-L February 1, 2021 04:24
@QC-L QC-L merged commit fcc6264 into docschina:cn Feb 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants