Skip to content

Commit

Permalink
fix: don't fail entire build when gatsby-config fails to be imported
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Feb 10, 2023
1 parent 8248205 commit d5ad5b2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 35 deletions.
76 changes: 41 additions & 35 deletions plugin/src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,15 @@ export async function spliceConfig({
return fs.writeFile(fileName, out)
}

function loadGatsbyConfig({ gatsbyRoot, utils }): GatsbyConfig | never {
function loadGatsbyConfig({ gatsbyRoot }): GatsbyConfig {
const gatsbyConfigFile = resolve(gatsbyRoot, 'gatsby-config.js')

if (!existsSync(gatsbyConfigFile)) {
return {}
}

try {
// eslint-disable-next-line n/global-require, import/no-dynamic-require, @typescript-eslint/no-var-requires
return require(gatsbyConfigFile) as GatsbyConfig
} catch (error) {
utils.build.failBuild('Could not load gatsby-config.js', { error })
}
// 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 {
Expand All @@ -88,38 +84,48 @@ function hasPlugin(plugins: PluginRef[], pluginName: string): boolean {
export async function checkConfig({ utils, netlifyConfig }): Promise<void> {
const gatsbyRoot = getGatsbyRoot(netlifyConfig.build.publish)

// warn if gatsby-plugin-netlify is missing
const gatsbyConfig = loadGatsbyConfig({
utils,
gatsbyRoot,
})

if (hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify')) {
if (
!(await checkPackageVersion(
gatsbyRoot,
'gatsby-plugin-netlify',
'>=4.2.0',
))
) {
console.error(
'The plugin `gatsby-plugin-netlify` does not support DSG, please update to >=4.2.0',
)
}
} else {
let gatsbyConfig: GatsbyConfig | undefined
try {
gatsbyConfig = loadGatsbyConfig({
gatsbyRoot,
})
} catch (e) {
console.error(
'Please install `gatsby-plugin-netlify` and enable it in your gatsby-config.js. https://www.gatsbyjs.com/plugins/gatsby-plugin-netlify/',
'Could not load gatsby-config.js: ' +
e.message +
"\n\nUnable to validate if 'gatsby-plugin-netlify' is setup correctly.",
)
}

if (hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify-cache')) {
console.error(
"The plugin 'gatsby-plugin-netlify-cache' is not compatible with the Gatsby build plugin",
)
console.error(
'Please uninstall gatsby-plugin-netlify-cache and remove it from your gatsby-config.js',
)
utils.build.failBuild('Incompatible Gatsby plugin installed')
if (gatsbyConfig) {
// warn if gatsby-plugin-netlify is missing
if (hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify')) {
if (
!(await checkPackageVersion(
gatsbyRoot,
'gatsby-plugin-netlify',
'>=4.2.0',
))
) {
console.error(
'The plugin `gatsby-plugin-netlify` does not support DSG, please update to >=4.2.0',
)
}
} else {
console.error(
'Please install `gatsby-plugin-netlify` and enable it in your gatsby-config.js. https://www.gatsbyjs.com/plugins/gatsby-plugin-netlify/',
)
}

if (hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify-cache')) {
console.error(
"The plugin 'gatsby-plugin-netlify-cache' is not compatible with the Gatsby build plugin",
)
console.error(
'Please uninstall gatsby-plugin-netlify-cache and remove it from your gatsby-config.js',
)
utils.build.failBuild('Incompatible Gatsby plugin installed')
}
}

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ describe('A site with "--require esm" and gatsby-config.js authored in ESM', ()
it('successfully builds and warns that is unable to validate gatsby-config', async () => {
const { success, logs } = await buildSite()
expect(success).toBeTruthy()
expect(logs.stderr)
.toMatch(`Could not load gatsby-config.js: Cannot use import statement outside a module
Unable to validate if 'gatsby-plugin-netlify' is setup correctly.`)
})
})

0 comments on commit d5ad5b2

Please sign in to comment.