Revert "Use ESM entrypoint for require(ESM)" (#17156)
Co-authored-by: Tatsunori Uchino <tats.u@live.jp>
This commit is contained in:
1 parent
c98acabc34
commit
bf5aab8f2d
4 files changed
+12
-5
No files matched your search
@@ -0,0 +1,3 @@
|
||||
#### Remove `module-sync` condition (#17156 by @fisker)
|
||||
|
||||
In Prettier 3.5.0, [we added `module-sync` condition to `package.json`](https://prettier.io/blog/2025/02/09/3.5.0#use-esm-entrypoint-for-requireesm-16958-by-tats-u), so that `require("prettier")` can use ESM version, but turns out it doesn't work if CommonJS and ESM plugins both imports builtin plugins. To solve this problem, we decide simply remove the `module-sync` condition, so `require("prettier")` will still use the CommonJS version, we'll revisit until `require(ESM)` feature is more stable.
|
||||
@@ -15,7 +15,6 @@
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./src/index.d.ts",
|
||||
"module-sync": "./src/index.js",
|
||||
"require": "./src/index.cjs",
|
||||
"default": "./src/index.js"
|
||||
},
|
||||
|
||||
@@ -42,7 +42,6 @@ async function buildPackageJson({ file, files }) {
|
||||
exports: {
|
||||
".": {
|
||||
types: "./index.d.ts",
|
||||
"module-sync": "./index.mjs",
|
||||
require: "./index.cjs",
|
||||
browser: {
|
||||
import: "./standalone.mjs",
|
||||
@@ -56,14 +55,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,
|
||||
// `module-sync` condition can prevent CJS plugins from working: https://github.com/prettier/prettier/issues/17139
|
||||
// Perform a test before re-adding it.
|
||||
require: `./${file.output.file}`,
|
||||
default: mjsPath,
|
||||
default: `./${file.output.file.replace(/\.js$/u, ".mjs")}`,
|
||||
},
|
||||
];
|
||||
}),
|
||||
|
||||
@@ -398,6 +398,12 @@ If you can't upgrade Prettier for some reason, you can still use JSONC syntax in
|
||||
|
||||
#### Use ESM entrypoint for `require(ESM)` ([#16958](https://github.com/prettier/prettier/pull/16958) by [@tats-u](https://github.com/tats-u))
|
||||
|
||||
:::info
|
||||
|
||||
This change has been reverted in v3.5.2, check [#17139](https://github.com/prettier/prettier/pull/17139) for details.
|
||||
|
||||
:::
|
||||
|
||||
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.
|
||||
Reference in new issue
Block a user