From 178cc71838abd4ceb9dce668dfb555feb2a8da94 Mon Sep 17 00:00:00 2001
From: Jamie Haywood <jamie.haywood@gmail.com>
Date: Fri, 1 Dec 2023 13:07:10 +0000
Subject: [PATCH] feat(config): add `.cjs` file extension config support
 (#26075)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
---
 docs/development/configuration.md                       | 3 +++
 lib/workers/global/config/parse/__fixtures__/config.cjs | 3 +++
 lib/workers/global/config/parse/file.spec.ts            | 1 +
 lib/workers/global/config/parse/file.ts                 | 1 +
 4 files changed, 8 insertions(+)
 create mode 100644 lib/workers/global/config/parse/__fixtures__/config.cjs

diff --git a/docs/development/configuration.md b/docs/development/configuration.md
index 54b007df01..a238d41296 100644
--- a/docs/development/configuration.md
+++ b/docs/development/configuration.md
@@ -24,6 +24,9 @@ If you need an alternate location or name, set it in the environment variable `R
 For example `RENOVATE_CONFIG_FILE=myconfig.js` or `RENOVATE_CONFIG_FILE=myconfig.json` and not `RENOVATE_CONFIG_FILE=myconfig`.
 If none is provided, or the file type is invalid, Renovate will fail.
 
+If you are in an ESM repo (`"type": "module"` in `package.json`) then you must use a `.cjs` extension and set `RENOVATE_CONFIG_FILE`.
+For example `RENOVATE_CONFIG_FILE=myconfig.cjs`.
+
 Using a configuration file gives you very granular configuration options.
 For instance, you can override most settings at the global (file), repository, or package level.
 e.g. apply one set of labels for `backend/package.json` and a different set of labels for `frontend/package.json` in the same repository.
diff --git a/lib/workers/global/config/parse/__fixtures__/config.cjs b/lib/workers/global/config/parse/__fixtures__/config.cjs
new file mode 100644
index 0000000000..52a85479b3
--- /dev/null
+++ b/lib/workers/global/config/parse/__fixtures__/config.cjs
@@ -0,0 +1,3 @@
+module.exports = {
+  token: 'abcdefg',
+};
diff --git a/lib/workers/global/config/parse/file.spec.ts b/lib/workers/global/config/parse/file.spec.ts
index 96ad95cbea..921a708f04 100644
--- a/lib/workers/global/config/parse/file.spec.ts
+++ b/lib/workers/global/config/parse/file.spec.ts
@@ -24,6 +24,7 @@ describe('workers/global/config/parse/file', () => {
   describe('.getConfig()', () => {
     it.each([
       ['custom js config file', 'config.js'],
+      ['custom js config file', 'config.cjs'],
       ['custom js config file exporting a Promise', 'config-promise.js'],
       ['custom js config file exporting a function', 'config-function.js'],
       // The next two are different syntactic ways of expressing the same thing
diff --git a/lib/workers/global/config/parse/file.ts b/lib/workers/global/config/parse/file.ts
index f66cee56d1..693ff64672 100644
--- a/lib/workers/global/config/parse/file.ts
+++ b/lib/workers/global/config/parse/file.ts
@@ -25,6 +25,7 @@ export async function getParsedContent(file: string): Promise<RenovateConfig> {
         await readSystemFile(file, 'utf8'),
         file,
       ) as RenovateConfig;
+    case '.cjs':
     case '.js': {
       const tmpConfig = await import(file);
       let config = tmpConfig.default
-- 
GitLab