Skip to content

doc: auto-generate ESM/CJS

Rodrigo Muino Tomonari requested to merge github/fork/RedYetiDev/auto-esm-cjs into main

This PR introduces the ability to automatically generate ESM vs. CJS snippets in the documentation.

So far, I've updated only the Buffer documentation to use this new feature. If this approach is approved, I plan to extend it to the rest of the documentation. I believe this feature will be a valuable enhancement, particularly when the @nodejs/web-infra team updates the documentation completely. In the meantime, this serves as a 'polyfill' to provide the feature now.

This feature allows for one code snippet to generate both ESM and CJS, rather than a mjs snippet and a cjs snippet.

How to use this feature?

Old:

const { Buffer } = require('node:buffer');
import { Buffer } from 'node:buffer';

New:

//@import '{ Buffer }' 'node:buffer'

Syntax:

//@import 'what-to-import' 'where-from' !star

If you want the snippet to generate the ESM as import * as module from 'module';, append !star to the @import comment.

Merge request reports

Loading