Skip to content

Commit 3d21384

Browse files
fail build if plugin cant load next.config.js (#99)
* fail build if plugin cant load next.config.js * pass config loading error to failBuild message Co-authored-by: Erez Rokah <[email protected]> Co-authored-by: Erez Rokah <[email protected]>
1 parent 05a573a commit 3d21384

File tree

6 files changed

+41
-8
lines changed

6 files changed

+41
-8
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ test/sample/netlify/functions
77
test/sample/my-publish-dir
88
test/sample/.next
99
test/sample/.netlify
10-
next.config.js
10+
/next.config.js
1111

1212
# Logs
1313
logs

helpers/doesNotNeedPlugin.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ const isStaticExportProject = require('./isStaticExportProject')
55
const doesSiteUseNextOnNetlify = require('./doesSiteUseNextOnNetlify')
66
const hasCorrectNextConfig = require('./hasCorrectNextConfig')
77

8-
const doesNotNeedPlugin = async ({ netlifyConfig, packageJson }) => {
8+
const doesNotNeedPlugin = async ({ netlifyConfig, packageJson, utils }) => {
99
const { build } = netlifyConfig
1010
const { name, scripts = {} } = packageJson
1111
const nextConfigPath = await findUp('next.config.js')
1212

1313
return (
1414
isStaticExportProject({ build, scripts }) ||
1515
doesSiteUseNextOnNetlify({ packageJson }) ||
16-
!hasCorrectNextConfig(nextConfigPath)
16+
!hasCorrectNextConfig({ nextConfigPath, failBuild: utils.build.failBuild })
1717
)
1818
}
1919

helpers/hasCorrectNextConfig.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require('path')
22

33
// Checks if site has the correct next.cofig.js
4-
const hasCorrectNextConfig = (nextConfigPath) => {
4+
const hasCorrectNextConfig = ({ nextConfigPath, failBuild }) => {
55
// In the plugin's case, no config is valid because we'll make it ourselves
66
if (nextConfigPath === undefined) return true
77

@@ -12,7 +12,12 @@ const hasCorrectNextConfig = (nextConfigPath) => {
1212

1313
// If the next config exists, log warning if target isnt in acceptableTargets
1414
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
15-
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.'))
15+
let nextConfig
16+
try {
17+
nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.'))
18+
} catch (error) {
19+
return failBuild('Error loading your next.config.js.', { error })
20+
}
1621
const isValidTarget = acceptableTargets.includes(nextConfig.target)
1722
if (!isValidTarget) {
1823
console.log(

index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = {
2424
return failBuild('Could not find a package.json for this project')
2525
}
2626

27-
if (await doesNotNeedPlugin({ netlifyConfig, packageJson })) {
27+
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
2828
return
2929
}
3030

@@ -39,8 +39,13 @@ module.exports = {
3939
await pWriteFile('next.config.js', nextConfig)
4040
}
4141
},
42-
async onBuild({ netlifyConfig, packageJson, constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC } }) {
43-
if (await doesNotNeedPlugin({ netlifyConfig, packageJson })) {
42+
async onBuild({
43+
netlifyConfig,
44+
packageJson,
45+
constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC },
46+
utils,
47+
}) {
48+
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
4449
return
4550
}
4651

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
target: "serverless",
3+
i18n: {
4+
locales: ["en", "fr"
5+
}
6+
};

test/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ describe('preBuild()', () => {
141141
}),
142142
).rejects.toThrow(`Could not find a package.json for this project`)
143143
})
144+
145+
test('fail build if the app cant load the next.config.js', async () => {
146+
await useFixture('broken_next_config')
147+
148+
await expect(
149+
plugin.onPreBuild({
150+
netlifyConfig,
151+
packageJson: DUMMY_PACKAGE_JSON,
152+
utils,
153+
constants: { FUNCTIONS_SRC: 'out_functions' },
154+
}),
155+
).rejects.toThrow(`Error loading your next.config.js.`)
156+
})
144157
})
145158

146159
describe('onBuild()', () => {
@@ -155,6 +168,7 @@ describe('onBuild()', () => {
155168
netlifyConfig,
156169
packageJson,
157170
constants: {},
171+
utils,
158172
})
159173

160174
expect(await pathExists(`${PUBLISH_DIR}/index.html`)).toBeFalsy()
@@ -170,6 +184,7 @@ describe('onBuild()', () => {
170184
packageJson: DUMMY_PACKAGE_JSON,
171185
utils,
172186
constants: { FUNCTIONS_SRC: 'out_functions' },
187+
utils,
173188
})
174189

175190
expect(await pathExists(`${PUBLISH_DIR}/index.html`)).toBeFalsy()
@@ -187,6 +202,7 @@ describe('onBuild()', () => {
187202
PUBLISH_DIR,
188203
FUNCTIONS_SRC: 'functions',
189204
},
205+
utils,
190206
})
191207

192208
expect(await pathExists(`${PUBLISH_DIR}/_redirects`)).toBeTruthy()
@@ -206,6 +222,7 @@ describe('onBuild()', () => {
206222
FUNCTIONS_SRC,
207223
PUBLISH_DIR: '.',
208224
},
225+
utils,
209226
})
210227

211228
expect(await pathExists(`${resolvedFunctions}/next_api_test/next_api_test.js`)).toBeTruthy()

0 commit comments

Comments
 (0)