Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support shared config that forbids require() #15233

Merged
merged 5 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions changelog_unreleased/api/15233.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#### Support shared config that forbids `require()` (#15233 by @fisker)

If an external shared config package is used, and the package `exports` don't have `require` or `default` export.

In Prettier stable Prettier fails when attempt to `require()` the package, and throws an error.

```text
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in <packageName>/package.json
```
10 changes: 6 additions & 4 deletions src/config/load-external-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import requireFromFile from "../utils/require-from-file.js";
import importFromFile from "../utils/import-from-file.js";

const requireErrorCodesShouldBeIgnored = new Set([
"MODULE_NOT_FOUND",
"ERR_REQUIRE_ESM",
"ERR_PACKAGE_PATH_NOT_EXPORTED",
]);
async function loadExternalConfig(config, filepath) {
/*
Try `require()` first, this is how it works in Prettier v2.
Expand All @@ -12,10 +17,7 @@ async function loadExternalConfig(config, filepath) {
try {
return requireFromFile(config, filepath);
} catch (/** @type {any} */ error) {
if (
error?.code !== "MODULE_NOT_FOUND" &&
error?.code !== "ERR_REQUIRE_ESM"
) {
if (!requireErrorCodesShouldBeIgnored.has(error?.code)) {
throw error;
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/__tests__/config-resolution.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,15 @@ test(".json5 config file(invalid)", async () => {
const error = /JSON5: invalid end of input at 2:1/;
await expect(prettier.resolveConfig(file)).rejects.toThrow(error);
});

test("support external module with `module` only `exports`", async () => {
const directory = path.join(
__dirname,
"../cli/config/external-config/esm-package-forbids-require",
);
const file = path.join(directory, "index.js");

await expect(prettier.resolveConfig(file)).resolves.toMatchObject({
printWidth: 79,
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("should have no semi");

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"prettier": "prettier-config-forbids-require"
}