Skip to content

Commit 7d650ca

Browse files
author
那里好脏不可以
authored
docs(contribute): update to [email protected] (#5633)
* docs(contribute): update to [email protected] * docs(contribute): fix some detail
1 parent b84af38 commit 7d650ca

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ contributors:
88
- anikethsaha
99
- jamesgeorge007
1010
- chenxsan
11+
- dev-itsheng
1112
---
1213

1314
A loader is a node module that exports a function. This function is called when a resource should be transformed by this loader. The given function will have access to the [Loader API](/api/loaders/) using the `this` context provided to it.
@@ -137,12 +138,12 @@ Make sure the loader does not retain state between module transformations. Each
137138

138139
### Loader Utilities
139140

140-
Take advantage of the [`loader-utils`](https://github.com/webpack/loader-utils) package. It provides a variety of useful tools but one of the most common is retrieving the options passed to the loader. Along with `loader-utils`, the [`schema-utils`](https://github.com/webpack-contrib/schema-utils) package should be used for consistent JSON Schema based validation of loader options. Here's a brief example that utilizes both:
141+
Take advantage of the [`loader-utils`](https://github.com/webpack/loader-utils) package which provides a variety of useful tools. Along with `loader-utils`, the [`schema-utils`](https://github.com/webpack-contrib/schema-utils) package should be used for consistent JSON Schema based validation of loader options. Here's a brief example that utilizes both:
141142

142143
**loader.js**
143144

144145
```js
145-
import { getOptions } from 'loader-utils';
146+
import { urlToRequest } from 'loader-utils';
146147
import { validate } from 'schema-utils';
147148

148149
const schema = {
@@ -155,12 +156,14 @@ const schema = {
155156
};
156157

157158
export default function (source) {
158-
const options = getOptions(this);
159+
const options = this.getOptions();
159160

160161
validate(schema, options, {
161162
name: 'Example Loader',
162163
baseDataPath: 'options',
163164
});
165+
166+
console.log('The request path', urlToRequest(this.resourcePath));
164167

165168
// Apply some transformations to the source...
166169

@@ -284,10 +287,8 @@ Our loader will process `.txt` files and replace any instance of `[name]` with t
284287
**src/loader.js**
285288

286289
```js
287-
import { getOptions } from 'loader-utils';
288-
289290
export default function loader(source) {
290-
const options = getOptions(this);
291+
const options = this.getOptions();
291292

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

0 commit comments

Comments
 (0)