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/development/how-to-write-a-loader.md
+56-50Lines changed: 56 additions & 50 deletions
Original file line number
Diff line number
Diff line change
@@ -3,93 +3,99 @@ title: How to write a loader?
3
3
sort: 3
4
4
---
5
5
6
-
A loader is a node module exporting a `function`.
6
+
loader是导出`function`的节点模块。
7
7
8
-
This function is called when a resource should be transformed by this loader.
8
+
当资源应该由此loader转换时,调用此函数。
9
9
10
-
In the simple case, when only a single loader is applied to the resource, the loader is called with one parameter: the content of the resource file as string.
The loader can access the [loader API](/api/loaders/) on the `this` context in the function.
12
+
这个loader能够在这个函数的上下文中`this`中可以访问 [[loader API | loaders]]。
13
13
14
-
A sync loader that only wants to give a one value can simply `return` it. In every other case the loader can give back any number of values with the `this.callback(err, values...)` function. Errors are passed to the `this.callback` function or thrown in a sync loader.
The loader is expected to give back one or two values. The first value is a resulting JavaScript code as string or buffer. The second optional value is a SourceMap as JavaScript object.
In the complex case, when multiple loaders are chained, only the last loader gets the resource file and only the first loader is expected to give back one or two values (JavaScript and SourceMap). Values that any other loader give back are passed to the previous loader.
(Ordered by priority, first one should get the highest priority)
42
+
(按照优先级排序,第一个具有最高的优先级)
39
43
40
-
*Loaders should do only a single task
41
-
*Loaders can be chained. Create loaders for every step, instead of a loader that does everything at once.
44
+
*Loaders应该只做一个任务
45
+
*Loaders能够被链接。为每一步创建loaders,而不是让一个loader马上做所有事情。
42
46
43
-
This also means they should not convert to JavaScript if not necessary.
47
+
这也意味着不必须的话它们不应该转换成JavaScript。
44
48
45
-
Example: Render HTML from a template file by applying the query parameters
49
+
例子:通过应用查询参数来将模板文件渲染成HTML。
46
50
47
-
I could write a loader that compiles the template from source, execute it and return a module that exports a string containing the HTML code. This is bad.
Most loaders are cacheable, so they should flag itself as cacheable.
75
+
大多数loaders是可以缓存的,因此它们应该把自身标志成可缓存的。
70
76
71
-
Just call `cacheable` in the loader.
77
+
只要在load中调用`cacheable`。
72
78
73
79
```javascript
74
-
//Cacheable identity loader
80
+
//利用cacheable定义loader
75
81
module.exports=function(source) {
76
82
this.cacheable();
77
83
return source;
78
84
};
79
85
```
80
86
81
-
### Do not keep state between runs and modules
87
+
### 不要在运行和模块间保存状态
82
88
83
-
A loader should be independent of other modules compiled (expect of these issued by the loader).
89
+
loader应该和其它编译后的模块相互独立。(期望这些能够被loader处理)
84
90
85
-
A loader should be independent of previous compilations of the same module.
91
+
loader应该和相同模块的之前汇编相互独立。
86
92
87
-
### Mark dependencies
93
+
### 标志依赖
88
94
89
-
If a loader uses external resources (i. e. by reading from filesystem), they **must** tell about that. This information is used to invalidate cacheable loaders and recompile in watch mode.
In many languages there is some schema to specify dependencies. i. e. in css there is `@import` and `url(...)`. These dependencies should be resolved by the module system.
*Use the `this.resolve` function to resolve the path
118
+
*将它们转化成 `require`s。
119
+
*使用`this.resolve`函数来解析路径。
114
120
115
-
Example 1 css-loader: The css-loader transform dependencies to `require`s, by replacing `@import`s with a require to the other stylesheet (processed with the css-loader too) and `url(...)` with a `require` to the referenced file.
Example 2 less-loader: The less-loader cannot transform `@import`s to `require`s, because all less files need to be compiled in one pass to track variables and mixins. Therefore the less-loader extends the less compiler with a custom path resolving logic. This custom logic uses `this.resolve` to resolve the file with the configuration of the module system (aliasing, custom module directories, etc.).
Don't generate much code that is common in every module processed by that loader. Create a (runtime) file in the loader and generate a `require` to that common code.
Don't put absolute paths in to the module code. They break hashing when the root for the project is moved. There is a method [`stringifyRequest` in loader-utils](https://github.com/webpack/loader-utils#stringifyrequest) which converts an absolute path to an relative one.
### Use a library as `peerDependencies`when they wrap it
150
+
### 使用`peerDependencies`来包装library
145
151
146
-
using a peerDependency allows the application developer to specify the exact version in `package.json` if desired. The dependency should be relatively open to allow updating the library without needing to publish a new loader version.
there are situations where your loader requires programmable objects with functions which cannot stringified as `query`-string. The less-loader, for example, provides the possibility to specify [LESS-plugins](https://github.com/webpack/less-loader#less-plugins). In these cases, a loader is allowed to extend webpack's `options`-object to retrieve that specific option. In order to avoid name collisions, however, it is important that the option is namespaced under the loader's camelCased npm-name.
The loader should also allow to specify the config-key (e.g. `lessLoader`) via `query`. See [discussion](https://github.com/webpack/less-loader/pull/40) and [example implementation](https://github.com/webpack/less-loader/blob/39f742b4624fceae6d9cf266e9554d07a32a9c14/index.js#L49-51).
178
+
loader还应该允许通过`query`来指定config-key(比如`lessLoader`)。 See [讨论](https://github.com/webpack/less-loader/pull/40) and [案例实现](https://github.com/webpack/less-loader/blob/39f742b4624fceae6d9cf266e9554d07a32a9c14/index.js#L49-51).
0 commit comments