From 44949eb5dc45197cfca3e1890809e675a4a38597 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 13 Dec 2023 16:16:15 +0800 Subject: [PATCH] Support absolute path in config file (#15666) --- changelog_unreleased/api/15666.md | 15 +++++++++++++++ src/main/plugins/load-plugin.js | 4 ++++ tests/integration/__tests__/plugin-resolution.js | 11 +++++++++++ .../plugins/absolute-path/prettier.config.mjs | 7 +++++++ 4 files changed, 37 insertions(+) create mode 100644 changelog_unreleased/api/15666.md create mode 100644 tests/integration/cli/config/plugins/absolute-path/prettier.config.mjs diff --git a/changelog_unreleased/api/15666.md b/changelog_unreleased/api/15666.md new file mode 100644 index 000000000..2ad2b069b --- /dev/null +++ b/changelog_unreleased/api/15666.md @@ -0,0 +1,15 @@ +#### Support absolute path as plugin in config file (#15666 by @fisker) + +```js +// prettier.config.cjs +module.exports = { + plugins: [ + // posix style + "/path/to/plugin.js", + // Windows style + "D:\\\\path\\to\\plugin.js", + // Use `require.resolve` + require.resolve("my-awesome-prettier-plugin"), + ], +}; +``` diff --git a/src/main/plugins/load-plugin.js b/src/main/plugins/load-plugin.js index 057fb0d3e..891cdb946 100644 --- a/src/main/plugins/load-plugin.js +++ b/src/main/plugins/load-plugin.js @@ -16,6 +16,10 @@ const loadPluginFromDirectory = mem( ); const importPlugin = mem(async (name) => { + if (path.isAbsolute(name)) { + return import(pathToFileURL(name).href); + } + try { // try local files return await import(pathToFileURL(path.resolve(name)).href); diff --git a/tests/integration/__tests__/plugin-resolution.js b/tests/integration/__tests__/plugin-resolution.js index d33dad4cc..bfbf163c3 100644 --- a/tests/integration/__tests__/plugin-resolution.js +++ b/tests/integration/__tests__/plugin-resolution.js @@ -62,3 +62,14 @@ describe("loads --plugin by bespoke plugin name (assuming it is installed in cwd write: [], }); }); + +describe.only("support absolute path in config file", () => { + runCli("cli/config/plugins/absolute-path", ["--parser=uppercase-rocks"], { + input: "prettier should be uppercase", + }).test({ + stdout: "PRETTIER SHOULD BE UPPERCASE", + stderr: "", + status: 0, + write: [], + }); +}); diff --git a/tests/integration/cli/config/plugins/absolute-path/prettier.config.mjs b/tests/integration/cli/config/plugins/absolute-path/prettier.config.mjs new file mode 100644 index 000000000..da9d1a4df --- /dev/null +++ b/tests/integration/cli/config/plugins/absolute-path/prettier.config.mjs @@ -0,0 +1,7 @@ +import path from 'node:path' + +export default { + plugins: [ + path.resolve('../../../../../config/prettier-plugins/prettier-plugin-uppercase-rocks/index.js') + ] +}