Use ESM entrypoint for require(ESM) (#16958)

Co-authored-by: fisker Cheung <lionkay@gmail.com>
This commit is contained in:
Tatsunori Uchinoandfisker Cheung authored and GitHub committed 2025-01-21 12:17:24 +08:00
1 parent 97d8115d18
commit b64b0a88ba
3 files changed
+10 -1

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
#### Use ESM entrypoint for `require(ESM)` (#16958 by @tats-u)
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.
The feature to load ES modules with `require` is not completely stable but can be used without ExperimentalWarning as of Node 22.13.
+1
View File
@@ -15,6 +15,7 @@
"exports": {
".": {
"types": "./src/index.d.ts",
"module-sync": "./src/index.js",
"require": "./src/index.cjs",
"default": "./src/index.js"
},
+4 -1
View File
@@ -42,6 +42,7 @@ async function buildPackageJson({ file, files }) {
exports: {
".": {
types: "./index.d.ts",
"module-sync": "./index.mjs",
require: "./index.cjs",
browser: {
import: "./standalone.mjs",
@@ -55,12 +56,14 @@ async function buildPackageJson({ file, files }) {
.filter((file) => file.output.format === "umd")
.map((file) => {
const basename = path.basename(file.output.file, ".js");
const mjsPath = `./${file.output.file.replace(/\.js$/u, ".mjs")}`;
return [
file.isPlugin ? `./plugins/${basename}` : `./${basename}`,
{
types: `./${file.output.file.replace(/\.js$/u, ".d.ts")}`,
"module-sync": mjsPath,
require: `./${file.output.file}`,
default: `./${file.output.file.replace(/\.js$/u, ".mjs")}`,
default: mjsPath,
},
];
}),