Skip to content

docs: modernize 'Writing a Loader' guide to ESM syntax#7979

Merged
alexander-akait merged 5 commits into
webpack:mainfrom
hagemaruwu:fix/asset-management-esm
Mar 12, 2026
Merged

docs: modernize 'Writing a Loader' guide to ESM syntax#7979
alexander-akait merged 5 commits into
webpack:mainfrom
hagemaruwu:fix/asset-management-esm

Conversation

@hagemaruwu
Copy link
Copy Markdown
Contributor

@hagemaruwu hagemaruwu commented Mar 10, 2026

Summary

The "Writing a Loader" guide was mixing CommonJS (require/module.exports)
and ESM (import/export) syntax across different code examples, which is
confusing for readers. This PR makes all examples consistent by using ESM
throughout. It also fixes a missing __dirname definition in the
test/compiler.js example that would cause a runtime error in an ESM context.

part of #7772

What kind of change does this PR introduce?

docs

Did you add tests for your changes?

No — this is a documentation-only change. No functional code was modified.

Does this PR introduce a breaking change?

No.

If relevant, what needs to be documented once your changes are merged or what have you already documented?

No additional documentation needed. This PR is itself a documentation fix.

Use of AI

I used an AI assistant to help identify inconsistencies
in the file and understand the difference between CommonJS and ESM module
syntax. All code changes were applied manually by me. I reviewed
the webpack AI policy before submitting.

@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 10, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
webpack-js-org Ready Ready Preview, Comment Mar 12, 2026 0:55am

Request Review

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Mar 10, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

import { Volume, createFsFromVolume } from "memfs";
import webpack from "webpack";

const __filename = fileURLToPath(import.meta.url);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an obsolete way to get the filename and dirname nowadays in Node.js -- you can check the new way on Node.js docs or even ask AI for it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the TL;DR is import.meta.dirname and import.meta.filename

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review! I've updated both examples to use
'import.meta.dirnameinstead of thefileURLToPath` approach.

### Common Code

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:
Avoid generating common code in every module the loader processes. Instead, create a runtime file in the loader and `import` it as a shared module:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should write here that commonjs syntax supported too, but we recommended to use ECMA modules

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @alexander-akait! I have updated the prose to explicitly mention that require is still supported while recommending import.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also restored the missing
memfs import that was accidentally removed.

@alexander-akait alexander-akait merged commit 79ec3eb into webpack:main Mar 12, 2026
8 of 9 checks passed
pranjalisr pushed a commit to pranjalisr/webpack.js.org that referenced this pull request Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants