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
+16-9Lines changed: 16 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@ contributors:
10
10
- chenxsan
11
11
- dev-itsheng
12
12
- evenstensberg
13
+
- hagemaruwu
13
14
---
14
15
15
16
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.
@@ -23,9 +24,9 @@ To test a single loader, you can use `path` to `resolve` a local file within a r
23
24
**webpack.config.js**
24
25
25
26
```js
26
-
constpath=require("node:path");
27
+
importpathfrom"node:path";
27
28
28
-
module.exports= {
29
+
exportdefault {
29
30
// ...
30
31
module: {
31
32
rules: [
@@ -50,9 +51,11 @@ To test multiple, you can utilize the `resolveLoader.modules` configuration to u
@@ -85,7 +88,7 @@ In the following example, the `foo-loader` would be passed the raw resource and
85
88
**webpack.config.js**
86
89
87
90
```js
88
-
module.exports= {
91
+
exportdefault {
89
92
// ...
90
93
module: {
91
94
rules: [
@@ -227,18 +230,20 @@ T> If the language only accepts relative urls (e.g. `url(file)` always refers to
227
230
228
231
### Common Code
229
232
230
-
Avoid generating common code in every module the loader processes. Instead, create a runtime file in the loader and generate a `require` to that shared module:
233
+
- Avoid generating common code in every module the loader processes. Instead, create a runtime file in the loader and `import` (or `require`) it as a shared module:
234
+
235
+
W> While CommonJS syntax (`require`) is completely supported, we highly recommend using ECMAScript Modules (`import`) for new loaders.
0 commit comments