Skip to content

Commit

Permalink
Merge pull request #114 from netlify/chore/refactor-get-next-config
Browse files Browse the repository at this point in the history
chore: separate `getNextConfig()`
  • Loading branch information
ehmicky authored Mar 11, 2021
2 parents a4d644e + c4da751 commit 430569c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
26 changes: 26 additions & 0 deletions helpers/getNextConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { resolve } = require('path')

let nextConfig

// Load next.config.js
const getNextConfig = async function (failBuild) {
// Memoizes `nextConfig`
if (nextConfig !== undefined) {
return nextConfig
}

// We cannot load `next` at the top-level because we validate whether the
// site is using `next` inside `onPreBuild`.
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
const loadConfig = require('next/dist/next-server/server/config').default

try {
nextConfig = await loadConfig(PHASE_PRODUCTION_BUILD, resolve('.'))
} catch (error) {
return failBuild('Error loading your next.config.js.', { error })
}

return nextConfig
}

module.exports = getNextConfig
19 changes: 5 additions & 14 deletions helpers/hasCorrectNextConfig.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
const path = require('path')
const getNextConfig = require('./getNextConfig')

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

// We cannot load `next` at the top-level because we validate whether the
// site is using `next` inside `onPreBuild`.
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
const { default: loadConfig } = require('next/dist/next-server/server/config')
const { target } = await getNextConfig(failBuild)

// If the next config exists, log warning if target isnt in acceptableTargets
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
let nextConfig
try {
nextConfig = await loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.'))
} catch (error) {
return failBuild('Error loading your next.config.js.', { error })
}
const isValidTarget = acceptableTargets.includes(nextConfig.target)
const isValidTarget = acceptableTargets.includes(target)
if (!isValidTarget) {
console.log(
`Your next.config.js must set the "target" property to one of: ${acceptableTargets.join(', ')}. Update the
`Your next.config.js must set the "target" property to one of: ${acceptableTargets.join(', ')}. Update the
target property to allow this plugin to run.`,
)
}
Expand Down

0 comments on commit 430569c

Please sign in to comment.