Skip to content

Commit 007c823

Browse files
piehkodiakhq[bot]
andauthored
fix: don't fail entire build when gatsby-config fails to be imported (#562)
* add failing test * fix: don't fail entire build when gatsby-config fails to be imported --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 5874f67 commit 007c823

File tree

12 files changed

+350
-15
lines changed

12 files changed

+350
-15
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"test": "run-s format install:demo build:demo test:unit test:e2e",
1313
"test:unit": "jest",
1414
"test:e2e": "run-s test:e2e:*",
15+
"test:e2e:v3": "cd plugin/test && ./v3-e2e-test.sh",
1516
"test:e2e:v4": "cd plugin/test && ./v4-e2e-test.sh",
1617
"test:e2e:v5": "cd plugin/test && ./v5-e2e-test.sh",
1718
"format": "run-s format:check-fix:*",

plugin/src/helpers/config.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,15 @@ export async function spliceConfig({
6060
return fs.writeFile(fileName, out)
6161
}
6262

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

6666
if (!existsSync(gatsbyConfigFile)) {
6767
return {}
6868
}
6969

70-
try {
71-
// eslint-disable-next-line n/global-require, import/no-dynamic-require, @typescript-eslint/no-var-requires
72-
return require(gatsbyConfigFile) as GatsbyConfig
73-
} catch (error) {
74-
utils.build.failBuild('Could not load gatsby-config.js', { error })
75-
}
70+
// eslint-disable-next-line n/global-require, import/no-dynamic-require, @typescript-eslint/no-var-requires
71+
return require(gatsbyConfigFile) as GatsbyConfig
7672
}
7773

7874
function hasPlugin(plugins: PluginRef[], pluginName: string): boolean {
@@ -85,15 +81,12 @@ function hasPlugin(plugins: PluginRef[], pluginName: string): boolean {
8581
)
8682
}
8783

88-
export async function checkConfig({ utils, netlifyConfig }): Promise<void> {
89-
const gatsbyRoot = getGatsbyRoot(netlifyConfig.build.publish)
90-
84+
async function checkGatsbyPluginsCompatibility({
85+
utils,
86+
gatsbyRoot,
87+
gatsbyConfig,
88+
}): Promise<void> | never {
9189
// warn if gatsby-plugin-netlify is missing
92-
const gatsbyConfig = loadGatsbyConfig({
93-
utils,
94-
gatsbyRoot,
95-
})
96-
9790
if (hasPlugin(gatsbyConfig.plugins, 'gatsby-plugin-netlify')) {
9891
if (
9992
!(await checkPackageVersion(
@@ -121,6 +114,29 @@ export async function checkConfig({ utils, netlifyConfig }): Promise<void> {
121114
)
122115
utils.build.failBuild('Incompatible Gatsby plugin installed')
123116
}
117+
}
118+
119+
export async function checkConfig({ utils, netlifyConfig }): Promise<void> {
120+
const gatsbyRoot = getGatsbyRoot(netlifyConfig.build.publish)
121+
122+
let gatsbyConfig: GatsbyConfig | undefined
123+
try {
124+
gatsbyConfig = loadGatsbyConfig({
125+
gatsbyRoot,
126+
})
127+
} catch (error) {
128+
console.error(
129+
`Could not load gatsby-config.js: ${error.message}\n\nUnable to validate if 'gatsby-plugin-netlify' is setup correctly.`,
130+
)
131+
}
132+
133+
if (gatsbyConfig) {
134+
await checkGatsbyPluginsCompatibility({
135+
utils,
136+
gatsbyConfig,
137+
gatsbyRoot,
138+
})
139+
}
124140

125141
if (
126142
netlifyConfig.plugins.some(
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules/
2+
.cache/
3+
public
4+
.netlify/cache
5+
.netlify/functions
6+
package-lock.json
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
The BSD Zero Clause License (0BSD)
2+
3+
Copyright (c) 2020 Gatsby Inc.
4+
5+
Permission to use, copy, modify, and/or distribute this software for any
6+
purpose with or without fee is hereby granted.
7+
8+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14+
PERFORMANCE OF THIS SOFTWARE.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// eslint-disable-next-line node/no-unpublished-require
2+
const { buildSite } = require('../../../../helpers')
3+
4+
jest.setTimeout(120_000)
5+
describe('A site with "--require esm" and gatsby-config.js authored in ESM', () => {
6+
it('successfully builds and warns that is unable to validate gatsby-config', async () => {
7+
const { success, logs } = await buildSite()
8+
expect(success).toBeTruthy()
9+
expect(logs.stderr)
10+
.toMatch(`Could not load gatsby-config.js: Cannot use import statement outside a module
11+
12+
Unable to validate if 'gatsby-plugin-netlify' is setup correctly.`)
13+
})
14+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import ActualConfig from './actual-gatsby-config.js'
2+
3+
module.exports = ActualConfig
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[build]
2+
command = "npm run build"
3+
publish = "public/"
4+
5+
[[plugins]]
6+
package = "../../../../src/index.ts"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "esm-gatsby-config",
3+
"version": "1.0.0",
4+
"private": true,
5+
"description": "My Gatsby site",
6+
"author": "Michal Piechowiak",
7+
"keywords": [
8+
"gatsby"
9+
],
10+
"scripts": {
11+
"develop": "cross-env NODE_OPTIONS=\"-r esm\" gatsby develop",
12+
"start": "cross-env NODE_OPTIONS=\"-r esm\" gatsby develop",
13+
"build": "cross-env NODE_OPTIONS=\"-r esm\" gatsby build",
14+
"serve": "cross-env NODE_OPTIONS=\"-r esm\" gatsby serve",
15+
"clean": "gatsby clean",
16+
"jest": "jest",
17+
"test": "npm run build && jest"
18+
},
19+
"license": "0BSD",
20+
"dependencies": {
21+
"cross-env": "^7.0.3",
22+
"esm": "^3.2.25",
23+
"gatsby": "^3.15.0",
24+
"react": "^17.0.1",
25+
"react-dom": "^17.0.1"
26+
},
27+
"devDependencies": {
28+
"jest": "^27.0.4"
29+
}
30+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Link } from 'gatsby'
2+
import * as React from 'react'
3+
4+
// styles
5+
const pageStyles = {
6+
color: '#232129',
7+
padding: '96px',
8+
fontFamily: '-apple-system, Roboto, sans-serif, serif',
9+
}
10+
const headingStyles = {
11+
marginTop: 0,
12+
marginBottom: 64,
13+
maxWidth: 320,
14+
}
15+
16+
const paragraphStyles = {
17+
marginBottom: 48,
18+
}
19+
const codeStyles = {
20+
color: '#8A6534',
21+
padding: 4,
22+
backgroundColor: '#FFF4DB',
23+
fontSize: '1.25rem',
24+
borderRadius: 4,
25+
}
26+
27+
// markup
28+
const NotFoundPage = () => (
29+
<main style={pageStyles}>
30+
<title>Not found</title>
31+
<h1 style={headingStyles}>Page not found</h1>
32+
<p style={paragraphStyles}>
33+
Sorry{' '}
34+
<span role="img" aria-label="Pensive emoji">
35+
😔
36+
</span>{' '}
37+
we couldn’t find what you were looking for.
38+
<br />
39+
{process.env.NODE_ENV === 'development' ? (
40+
<>
41+
<br />
42+
Try creating a page in <code style={codeStyles}>src/pages/</code>.
43+
<br />
44+
</>
45+
) : null}
46+
<br />
47+
<Link to="/">Go home</Link>.
48+
</p>
49+
</main>
50+
)
51+
52+
export default NotFoundPage

0 commit comments

Comments
 (0)