Skip to content

Commit 715b2ba

Browse files
authored
docs(loaders): update async loader API documentation (#7541)
1 parent c1ac637 commit 715b2ba

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

Diff for: src/content/api/loaders.mdx

+16-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ contributors:
1414
- snitin315
1515
- chenxsan
1616
- jamesgeorge007
17+
- alexeyr
1718
---
1819

1920
A loader is 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.
2021

2122
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.
2223

23-
A single result can be returned in [sync mode](#synchronous-loaders). For multiple results the `this.callback()` must be called. In [async mode](#asynchronous-loaders) `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.
24+
A single result can be returned in [sync mode](#synchronous-loaders). For multiple results the `this.callback()` must be called and the loader must return `undefined`.
25+
26+
In [async mode](#asynchronous-loaders) you can return a single result from an `async function`. Alternatively, you may call `this.async()` to indicate that the [loader runner](https://github.com/webpack/loader-runner) should wait for an asynchronous result. It returns `this.callback()`. In this case the loader must return `undefined` and call that callback. This is the only option for multiple results.
2427

2528
```js
2629
/**
@@ -63,10 +66,21 @@ module.exports = function (content, map, meta) {
6366

6467
### Asynchronous Loaders
6568

66-
For asynchronous loaders, [`this.async`](#thisasync) is used to retrieve the `callback` function:
69+
For asynchronous loaders, you can return the transformed `content` from an `async function`:
6770

6871
**async-loader.js**
6972

73+
```javascript
74+
module.exports = async function (content, map, meta) {
75+
var result = await someAsyncOperation(content);
76+
return result;
77+
};
78+
```
79+
80+
Or you can use [`this.async`](#thisasync) to retrieve the `callback` function:
81+
82+
**async-loader-with-callback.js**
83+
7084
```javascript
7185
module.exports = function (content, map, meta) {
7286
var callback = this.async();

0 commit comments

Comments
 (0)