Skip to content

Commit

Permalink
Use default next.config.js if not found (#30155)
Browse files Browse the repository at this point in the history
Some warnings/errors will mention `next.config.js` even if it doesn't exist so we'll make sure to assign a default value of `next.config.js`.

- Follow up to #30152
  • Loading branch information
styfle committed Oct 22, 2021
1 parent 6f3e947 commit 0226754
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
8 changes: 3 additions & 5 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,11 +765,9 @@ export default async function build(
} = await staticCheckSpan.traceAsyncFn(async () => {
process.env.NEXT_PHASE = PHASE_PRODUCTION_BUILD

const configFileName = config.configFileName
const runtimeEnvConfig = {
publicRuntimeConfig: config.publicRuntimeConfig,
serverRuntimeConfig: config.serverRuntimeConfig,
}
const { configFileName, publicRuntimeConfig, serverRuntimeConfig } =
config
const runtimeEnvConfig = { publicRuntimeConfig, serverRuntimeConfig }

const nonStaticErrorPageSpan = staticCheckSpan.traceChild(
'check-static-error-page'
Expand Down
14 changes: 9 additions & 5 deletions packages/next/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const experimentalWarning = execOnce(() => {
})

function assignDefaults(userConfig: { [key: string]: any }) {
const configFileName = userConfig.configFileName || 'next.config.js'
const configFileName = userConfig.configFileName
if (typeof userConfig.exportTrailingSlash !== 'undefined') {
console.warn(
chalk.yellow.bold('Warning: ') +
Expand Down Expand Up @@ -528,9 +528,12 @@ export default async function loadConfig(
await loadEnvConfig(dir, phase === PHASE_DEVELOPMENT_SERVER, Log)
await loadWebpackHook()

let configFileName = 'next.config.js'

if (customConfig) {
return assignDefaults({
configOrigin: 'server',
configFileName,
...customConfig,
}) as NextConfigComplete
}
Expand All @@ -539,7 +542,7 @@ export default async function loadConfig(

// If config file was found
if (path?.length) {
const configName = basename(path)
configFileName = basename(path)
let userConfigModule: any

try {
Expand All @@ -549,7 +552,7 @@ export default async function loadConfig(
userConfigModule = await import(pathToFileURL(path).href)
} catch (err) {
Log.error(
`Failed to load ${configName}, see more info here https://nextjs.org/docs/messages/next-config-error`
`Failed to load ${configFileName}, see more info here https://nextjs.org/docs/messages/next-config-error`
)
throw err
}
Expand All @@ -560,7 +563,7 @@ export default async function loadConfig(

if (Object.keys(userConfig).length === 0) {
Log.warn(
`Detected ${configName}, no exported configuration found. https://nextjs.org/docs/messages/empty-configuration`
`Detected ${configFileName}, no exported configuration found. https://nextjs.org/docs/messages/empty-configuration`
)
}

Expand Down Expand Up @@ -588,7 +591,7 @@ export default async function loadConfig(
return assignDefaults({
configOrigin: relative(dir, path),
configFile: path,
configFileName: configName,
configFileName,
...userConfig,
}) as NextConfigComplete
} else {
Expand All @@ -612,6 +615,7 @@ export default async function loadConfig(
}

const completeConfig = defaultConfig as NextConfigComplete
completeConfig.configFileName = configFileName
setHttpAgentOptions(completeConfig.httpAgentOptions)
return completeConfig
}
Expand Down

0 comments on commit 0226754

Please sign in to comment.