Skip to content

Commit b64b0a8

Browse files
tats-ufisker
andauthored
Use ESM entrypoint for require(ESM) (#16958)
Co-authored-by: fisker Cheung <[email protected]>
1 parent 97d8115 commit b64b0a8

3 files changed

Lines changed: 10 additions & 1 deletion

File tree

changelog_unreleased/misc/16958.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#### Use ESM entrypoint for `require(ESM)` (#16958 by @tats-u)
2+
3+
Node.js 22.12 or later [can (experimentally) load ESM modules with `require` function](https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require) without runtime flags. This change enables `require` to load Prettier without the CommonJS entrypoint with almost only the ability to import the ESM entrypoint.
4+
5+
The feature to load ES modules with `require` is not completely stable but can be used without ExperimentalWarning as of Node 22.13.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"exports": {
1616
".": {
1717
"types": "./src/index.d.ts",
18+
"module-sync": "./src/index.js",
1819
"require": "./src/index.cjs",
1920
"default": "./src/index.js"
2021
},

scripts/build/build-package-json.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ async function buildPackageJson({ file, files }) {
4242
exports: {
4343
".": {
4444
types: "./index.d.ts",
45+
"module-sync": "./index.mjs",
4546
require: "./index.cjs",
4647
browser: {
4748
import: "./standalone.mjs",
@@ -55,12 +56,14 @@ async function buildPackageJson({ file, files }) {
5556
.filter((file) => file.output.format === "umd")
5657
.map((file) => {
5758
const basename = path.basename(file.output.file, ".js");
59+
const mjsPath = `./${file.output.file.replace(/\.js$/u, ".mjs")}`;
5860
return [
5961
file.isPlugin ? `./plugins/${basename}` : `./${basename}`,
6062
{
6163
types: `./${file.output.file.replace(/\.js$/u, ".d.ts")}`,
64+
"module-sync": mjsPath,
6265
require: `./${file.output.file}`,
63-
default: `./${file.output.file.replace(/\.js$/u, ".mjs")}`,
66+
default: mjsPath,
6467
},
6568
];
6669
}),

0 commit comments

Comments
 (0)