Skip to content

Commit

Permalink
Merge pull request #124 from netlify/fix/get-next-config
Browse files Browse the repository at this point in the history
fix: fix errors while loading `next.config.js`
  • Loading branch information
ehmicky authored Mar 11, 2021
2 parents 2ee1c38 + e55f2a9 commit 106a177
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 23 deletions.
4 changes: 2 additions & 2 deletions helpers/doesNotNeedPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const isStaticExportProject = require('./isStaticExportProject')
const doesSiteUseNextOnNetlify = require('./doesSiteUseNextOnNetlify')
const hasCorrectNextConfig = require('./hasCorrectNextConfig')

const doesNotNeedPlugin = async ({ netlifyConfig, packageJson, utils }) => {
const doesNotNeedPlugin = async ({ netlifyConfig, packageJson, failBuild }) => {
const { build } = netlifyConfig
const { name, scripts = {} } = packageJson
const nextConfigPath = await findUp('next.config.js')

return (
isStaticExportProject({ build, scripts }) ||
doesSiteUseNextOnNetlify({ packageJson }) ||
!(await hasCorrectNextConfig({ nextConfigPath, failBuild: utils.build.failBuild }))
!(await hasCorrectNextConfig({ nextConfigPath, failBuild }))
)
}

Expand Down
6 changes: 5 additions & 1 deletion helpers/getNextConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { resolve } = require('path')
let nextConfig

// Load next.config.js
const getNextConfig = async function (failBuild) {
const getNextConfig = async function (failBuild = defaultFailBuild) {
// Memoizes `nextConfig`
if (nextConfig !== undefined) {
return nextConfig
Expand All @@ -23,4 +23,8 @@ const getNextConfig = async function (failBuild) {
return nextConfig
}

const defaultFailBuild = function (message, { error }) {
throw new Error(`${message}\n${error.stack}`)
}

module.exports = getNextConfig
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
return failBuild('Could not find a package.json for this project')
}

if (await doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
return
}

Expand All @@ -46,7 +46,9 @@ module.exports = {
constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC },
utils,
}) {
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
const { failBuild } = utils.build

if (await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/helpers/getI18n.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Get the i1i8n details specified in next.config.js, if any
const getNextConfig = require('./getNextConfig')
const getNextConfig = require('../../../helpers/getNextConfig')

const getI18n = async () => {
const nextConfig = await getNextConfig()
Expand Down
15 changes: 0 additions & 15 deletions src/lib/helpers/getNextConfig.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib/helpers/getNextDistDir.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Get the NextJS distDir specified in next.config.js
const { join } = require('path')
const getNextConfig = require('./getNextConfig')
const getNextConfig = require('../../../helpers/getNextConfig')

const getNextDistDir = async () => {
const nextConfig = await getNextConfig()
Expand Down
2 changes: 1 addition & 1 deletion src/lib/helpers/getPrerenderManifest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { join } = require('path')
const { readJSONSync } = require('fs-extra')
const getNextConfig = require('./getNextConfig')
const getNextConfig = require('../../../helpers/getNextConfig')
const getNextDistDir = require('./getNextDistDir')
const getDataRouteForRoute = require('./getDataRouteForRoute')
const asyncForEach = require('./asyncForEach')
Expand Down

0 comments on commit 106a177

Please sign in to comment.