From 64aa1140cdf146e77b1872fb7dc68dcc726336d8 Mon Sep 17 00:00:00 2001 From: Graydon Pleasants Date: Wed, 5 Jul 2023 14:28:01 -0400 Subject: [PATCH 1/2] modified loadGatsbyConfig to support TS --- plugin/src/helpers/config.ts | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/plugin/src/helpers/config.ts b/plugin/src/helpers/config.ts index fc2e863d..0382374c 100644 --- a/plugin/src/helpers/config.ts +++ b/plugin/src/helpers/config.ts @@ -60,15 +60,30 @@ export async function spliceConfig({ return fs.writeFile(fileName, out) } -function loadGatsbyConfig({ gatsbyRoot }): GatsbyConfig { - const gatsbyConfigFile = resolve(gatsbyRoot, 'gatsby-config.js') +function loadGatsbyConfig({ gatsbyRoot }) { + const gatsbyConfigFileJS = resolve(gatsbyRoot, 'gatsby-config.js'); + const gatsbyConfigFileTS = resolve(gatsbyRoot, 'gatsby-config.ts'); - if (!existsSync(gatsbyConfigFile)) { - return {} + let gatsbyConfigFile = ''; + + if (existsSync(gatsbyConfigFileJS)) { + gatsbyConfigFile = gatsbyConfigFileJS; + } else if (existsSync(gatsbyConfigFileTS)) { + gatsbyConfigFile = gatsbyConfigFileTS; + } else { + return {}; + } + + const resolvedGatsbyConfigFile = resolveModule.sync(gatsbyConfigFile, { + extensions: ['.js', '.ts'], + }); + + if (!resolvedGatsbyConfigFile) { + return {}; } // eslint-disable-next-line n/global-require, import/no-dynamic-require, @typescript-eslint/no-var-requires - return require(gatsbyConfigFile) as GatsbyConfig + return require(resolvedGatsbyConfigFile); } function hasPlugin(plugins: PluginRef[], pluginName: string): boolean { From d630b4d8c1cae39c69193d28a2d4529f57781eca Mon Sep 17 00:00:00 2001 From: Graydon Pleasants Date: Wed, 5 Jul 2023 14:38:45 -0400 Subject: [PATCH 2/2] Update config.ts --- plugin/src/helpers/config.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/plugin/src/helpers/config.ts b/plugin/src/helpers/config.ts index 0382374c..db9d65fd 100644 --- a/plugin/src/helpers/config.ts +++ b/plugin/src/helpers/config.ts @@ -74,16 +74,8 @@ function loadGatsbyConfig({ gatsbyRoot }) { return {}; } - const resolvedGatsbyConfigFile = resolveModule.sync(gatsbyConfigFile, { - extensions: ['.js', '.ts'], - }); - - if (!resolvedGatsbyConfigFile) { - return {}; - } - - // eslint-disable-next-line n/global-require, import/no-dynamic-require, @typescript-eslint/no-var-requires - return require(resolvedGatsbyConfigFile); + // eslint-disable-next-line n/global-require, import/no-dynamic-require, @typescript-eslint/no-var-requires + return require(gatsbyConfigFile) as GatsbyConfig; } function hasPlugin(plugins: PluginRef[], pluginName: string): boolean {