Skip to content
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

Add More Configuration File Options #9713

Merged
merged 2 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 16 additions & 5 deletions packages/next/lib/find-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,23 @@ export async function findConfig<T>(
}

// If we didn't find the configuration in `package.json`, we should look for
// known filenames. The /rc$/ version of this file does not support YAML
// like some configuration loaders.
const filePath = await findUp(`.${key}rc.json`, {
cwd: directory,
})
// known filenames.
const filePath = await findUp(
[
`.${key}rc.json`,
`${key}.config.json`,
`.${key}rc.js`,
`${key}.config.js`,
],
{
cwd: directory,
}
)
if (filePath) {
if (filePath.endsWith('.js')) {
return require(filePath)
}

// We load JSON contents with JSON5 to allow users to comment in their
// configuration file. This pattern was popularized by TypeScript.
const fileContents = fs.readFileSync(filePath, 'utf8')
Expand Down
18 changes: 18 additions & 0 deletions test/unit/find-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ describe('find config', () => {
expect(config).toEqual({ foo: 'bar' })
})

it('should resolve rc.js', async () => {
const config = await findConfig(join(fixtureDir, 'config-js'), 'test')
expect(config).toEqual({ foo: 'bar' })
})

it('should resolve .config.json', async () => {
const config = await findConfig(
join(fixtureDir, 'config-long-json'),
'test'
)
expect(config).toEqual({ foo: 'bar' })
})

it('should resolve .config.js', async () => {
const config = await findConfig(join(fixtureDir, 'config-long-js'), 'test')
expect(config).toEqual({ foo: 'bar' })
})

it('should resolve package.json', async () => {
const config = await findConfig(
join(fixtureDir, 'config-package-json'),
Expand Down
1 change: 1 addition & 0 deletions test/unit/fixtures/config-js/.testrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { foo: 'bar' }
1 change: 1 addition & 0 deletions test/unit/fixtures/config-long-js/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { foo: 'bar' }
1 change: 1 addition & 0 deletions test/unit/fixtures/config-long-json/test.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "foo": "bar" }