Skip to content

Commit

Permalink
Add More Configuration File Options
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer committed Dec 11, 2019
1 parent a40a32d commit f4daa09
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
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" }

0 comments on commit f4daa09

Please sign in to comment.