Skip to content

Commit 935fd0c

Browse files
authored
docs(contribute): add data sharing info among chained loaders (#7214)
1 parent 45c21ce commit 935fd0c

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,22 @@ export default function (source) {
171171
}
172172
```
173173

174+
### Data Sharing
175+
176+
In webpack, loaders can be chained together and share data with subsequent loaders in the chain. To achieve this, you can pass data along with the content (source code) using the `this.callback` method in raw loaders. In the default exported function of a raw loader, you can pass data using the fourth argument of `this.callback`.
177+
178+
```javascript
179+
export default function (source) {
180+
const options = getOptions(this);
181+
// Pass data using the fourth argument of this.callback
182+
this.callback(null, `export default ${JSON.stringify(source)}`, null, {
183+
some: data,
184+
});
185+
}
186+
```
187+
188+
In the example above, `some` property in the fourth argument of `this.callback` is used to pass data to the next chained loader.
189+
174190
### Loader Dependencies
175191

176192
If a loader uses external resources (i.e. by reading from filesystem), they **must** indicate it. This information is used to invalidate cacheable loaders and recompile in watch mode. Here's a brief example of how to accomplish this using the `addDependency` method:
@@ -245,7 +261,12 @@ Don't insert absolute paths into the module code as they break hashing when the
245261

246262
```js
247263
// `loaderContext` is same as `this` inside loader function
248-
JSON.stringify(loaderContext.utils.contextify(loaderContext.context || loaderContext.rootContext, request));
264+
JSON.stringify(
265+
loaderContext.utils.contextify(
266+
loaderContext.context || loaderContext.rootContext,
267+
request
268+
)
269+
);
249270
```
250271

251272
### Peer Dependencies

0 commit comments

Comments
 (0)