Commit 67079096 authored by sagar talla's avatar sagar talla Committed by Joe Haddad

Error when user tries to use uncompiled `next.config.ts` (#9223)

* Throw Error for next.config.ts

Ref: https://github.com/zeit/next-plugins/issues/555

* Check for .js else .ts

* Update Error Message
Co-Authored-By: 's avatarAlexander Kachkaev <alexander@kachkaev.ru>

* Add test case when both next.config.js and next.config.ts exist.

* Remove extra constant CONFIG_FILE_TS and Add check for ts/tsx/json

* Rename test config parameter and add tsx and json files

* Change messege

* change value

* Update next.config.json

* Update next.config.json

* Change message  and add jsx filter

* Adjust

* Simplify test

* adjust
parent 6b54e9ed
import chalk from 'chalk'
import findUp from 'find-up'
import os from 'os'
import { basename, extname } from 'path'
import { CONFIG_FILE } from '../lib/constants'
import { execOnce } from '../lib/utils'
......@@ -162,6 +163,24 @@ export default function loadConfig(
}
return assignDefaults({ configOrigin: CONFIG_FILE, ...userConfig })
} else {
const configBaseName = basename(CONFIG_FILE, extname(CONFIG_FILE))
const nonJsPath = findUp.sync(
[
`${configBaseName}.jsx`,
`${configBaseName}.ts`,
`${configBaseName}.tsx`,
`${configBaseName}.json`,
],
{ cwd: dir }
)
if (nonJsPath && nonJsPath.length) {
throw new Error(
`Configuring Next.js via '${basename(
nonJsPath
)}' is not supported. Please replace the file with 'next.config.js'.`
)
}
}
return defaultConfig
......
module.exports = {
__test__ext: 'js'
}
module.exports = {
__test__ext: 'jsx'
}
module.exports = {
__test__ext: 'ts',
}
module.exports = {
__test__ext: 'tsx',
}
......@@ -85,4 +85,27 @@ describe('config', () => {
)
expect(config.target).toBe('serverless')
})
it('Should throw an error when next.config.js is not present', () => {
try {
loadConfig(
PHASE_DEVELOPMENT_SERVER,
join(__dirname, '_resolvedata', 'typescript-config')
)
// makes sure we don't just pass if the loadConfig passes while it should fail
throw new Error('failed')
} catch (err) {
expect(err.message).toMatch(
/Configuring Next.js via .+ is not supported. Please replace the file with 'next.config.js'./
)
}
})
it('Should not throw an error when two versions of next.config.js are present', () => {
const config = loadConfig(
PHASE_DEVELOPMENT_SERVER,
join(__dirname, '_resolvedata', 'js-ts-config')
)
expect(config.__test__ext).toBe('js')
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment