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: src/content/contribute/writing-a-loader.mdx
+47Lines changed: 47 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,7 @@ contributors:
11
11
- dev-itsheng
12
12
- evenstensberg
13
13
- hagemaruwu
14
+
- raj-sapalya
14
15
---
15
16
16
17
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.
@@ -101,6 +102,52 @@ export default {
101
102
};
102
103
```
103
104
105
+
## Pitching loaders
106
+
107
+
Loaders normally run **right to left**. A loader can also export a
108
+
`pitch` function, which runs **left to right** before the normal phase
109
+
begins. This allows a loader to pass data to its own normal phase or
// Pitch phase — runs left to right before normal loaders
122
+
data.value="/* processed by my-loader */";
123
+
}
124
+
```
125
+
126
+
T> `data` is shared only between the `pitch` and normal functions of
127
+
the same loader. It is not shared across different loaders in
128
+
the chain.
129
+
130
+
### Short-circuiting the loader chain
131
+
132
+
If a `pitch` function returns a value, webpack skips the remaining
133
+
loaders to the right and reverses immediately. This can be useful when
134
+
you want to generate module code early and bypass the normal phase.
135
+
136
+
```js
137
+
exportfunctionpitch(remainingRequest) {
138
+
return`
139
+
import style from ${JSON.stringify(`!!${remainingRequest}`)};
140
+
const el = document.createElement("style");
141
+
el.textContent = style;
142
+
document.head.appendChild(el);
143
+
`;
144
+
}
145
+
```
146
+
147
+
W> When a `pitch` function returns a value, the default export of that
148
+
loader does not run. Only use this pattern when you intend to replace
149
+
the normal processing pipeline.
150
+
104
151
## Guidelines
105
152
106
153
The following guidelines should be followed when writing a loader. They are ordered in terms of importance and some only apply in certain scenarios, read the detailed sections that follow for more information.
0 commit comments