diff --git a/changelog_unreleased/misc/17156.md b/changelog_unreleased/misc/17156.md new file mode 100644 index 000000000..3fa33a673 --- /dev/null +++ b/changelog_unreleased/misc/17156.md @@ -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. diff --git a/package.json b/package.json index 180572024..489498548 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,6 @@ "exports": { ".": { "types": "./src/index.d.ts", - "module-sync": "./src/index.js", "require": "./src/index.cjs", "default": "./src/index.js" }, diff --git a/scripts/build/build-package-json.js b/scripts/build/build-package-json.js index ea185c32e..bc8e1376c 100644 --- a/scripts/build/build-package-json.js +++ b/scripts/build/build-package-json.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")}`, }, ]; }), diff --git a/website/blog/2025-02-09-3.5.0.md b/website/blog/2025-02-09-3.5.0.md index 47c3b8e21..2b06723b9 100644 --- a/website/blog/2025-02-09-3.5.0.md +++ b/website/blog/2025-02-09-3.5.0.md @@ -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.