Skip to content

Commit a5b80cc

Browse files
hagemaruwupranjalisr
authored andcommitted
docs: modernize 'Writing a Loader' guide to ESM syntax (webpack#7979)
1 parent 75c6432 commit a5b80cc

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ contributors:
1010
- chenxsan
1111
- dev-itsheng
1212
- evenstensberg
13+
- hagemaruwu
1314
---
1415

1516
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
2324
**webpack.config.js**
2425

2526
```js
26-
const path = require("node:path");
27+
import path from "node:path";
2728

28-
module.exports = {
29+
export default {
2930
// ...
3031
module: {
3132
rules: [
@@ -50,9 +51,11 @@ To test multiple, you can utilize the `resolveLoader.modules` configuration to u
5051
**webpack.config.js**
5152

5253
```js
53-
const path = require("node:path");
54+
import path from "node:path";
5455

55-
module.exports = {
56+
const __dirname = import.meta.dirname;
57+
58+
export default {
5659
// ...
5760
resolveLoader: {
5861
modules: ["node_modules", path.resolve(__dirname, "loaders")],
@@ -85,7 +88,7 @@ In the following example, the `foo-loader` would be passed the raw resource and
8588
**webpack.config.js**
8689

8790
```js
88-
module.exports = {
91+
export default {
8992
// ...
9093
module: {
9194
rules: [
@@ -227,18 +230,20 @@ T> If the language only accepts relative urls (e.g. `url(file)` always refers to
227230

228231
### Common Code
229232

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.
231236

232237
**src/loader-runtime.js**
233238

234239
```js
235-
const { someOtherModule } = require("./some-other-module");
240+
import { someOtherModule } from "./some-other-module.js";
236241

237-
module.exports = function runtime(params) {
242+
export default function runtime(params) {
238243
const x = params.y * 2;
239244

240245
return someOtherModule(params, x);
241-
};
246+
}
242247
```
243248

244249
**src/loader.js**
@@ -344,6 +349,8 @@ import path from "node:path";
344349
import { Volume, createFsFromVolume } from "memfs";
345350
import webpack from "webpack";
346351

352+
const __dirname = import.meta.dirname;
353+
347354
export default (fixture, options = {}) => {
348355
const compiler = webpack({
349356
context: __dirname,

0 commit comments

Comments
 (0)