Skip to content

refactor: locate next.config.js by root publish directory, instead of root of repository #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions helpers/defaultFailBuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.defaultFailBuild = function (message, { error }) {
throw new Error(`${message}\n${error.stack}`)
}
11 changes: 5 additions & 6 deletions helpers/getNextConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@

const { resolve } = require('path')

const { defaultFailBuild } = require('./defaultFailBuild')
const moize = require('moize')

// Load next.config.js
const getNextConfig = async function (failBuild = defaultFailBuild) {
const getNextConfig = async function (params = {}) {
const { failBuild = defaultFailBuild, publishPath = '.' } = params

// 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 {
return await loadConfig(PHASE_PRODUCTION_BUILD, resolve('.'))
return await loadConfig(PHASE_PRODUCTION_BUILD, resolve(publishPath))
} catch (error) {
return failBuild('Error loading your next.config.js.', { error })
}
}

const moizedGetNextConfig = moize(getNextConfig, { maxSize: 1e3, isPromise: true })

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

module.exports = moizedGetNextConfig
2 changes: 1 addition & 1 deletion helpers/hasCorrectNextConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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

const { target } = await getNextConfig(failBuild)
const { target } = await getNextConfig({ failBuild })

// If the next config exists, log warning if target isnt in acceptableTargets
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
Expand Down
6 changes: 4 additions & 2 deletions src/lib/helpers/getNextDistDir.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Get the NextJS distDir specified in next.config.js
const { join } = require('path')

const getNextConfig = require('../../../helpers/getNextConfig')
const { defaultFailBuild } = require('../../../helpers/defaultFailBuild')

const getNextDistDir = async () => {
const nextConfig = await getNextConfig()
const getNextDistDir = async (publishPath) => {
const nextConfig = await getNextConfig({ failBuild: defaultFailBuild, publishPath })

return join('.', nextConfig.distDir)
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/steps/copyNextAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const getNextDistDir = require('../helpers/getNextDistDir')
// Copy the NextJS' static assets from NextJS distDir to Netlify publish folder.
// These need to be available for NextJS to work.
const copyNextAssets = async (publishPath) => {
const nextDistDir = await getNextDistDir()
const nextDistDir = await getNextDistDir(publishPath)
const staticAssetsPath = join(nextDistDir, 'static')
if (!existsSync(staticAssetsPath)) {
throw new Error(
Expand Down