Commit 369ac488 authored by Andrew Gerard's avatar Andrew Gerard Committed by Tim Neutkens

Assign deep defaults for custom config (#6359)

* assign deep defaults for custom config

* allow for new object type defaults

* fix linting
parent 6eacea43
......@@ -25,6 +25,20 @@ const defaultConfig = {
}
}
function assignDefaults (userConfig) {
Object.keys(userConfig).forEach(key => {
const maybeObject = userConfig[key]
if ((!!maybeObject) && (maybeObject.constructor === Object)) {
userConfig[key] = {
...(defaultConfig[key] || {}),
...userConfig[key]
}
}
})
return { ...defaultConfig, ...userConfig }
}
function normalizeConfig (phase, config) {
if (typeof config === 'function') {
return config(phase, { defaultConfig })
......@@ -35,7 +49,7 @@ function normalizeConfig (phase, config) {
export default function loadConfig (phase, dir, customConfig) {
if (customConfig) {
return { ...defaultConfig, configOrigin: 'server', ...customConfig }
return assignDefaults({ configOrigin: 'server', ...customConfig })
}
const path = findUp.sync(CONFIG_FILE, {
cwd: dir
......@@ -48,19 +62,7 @@ export default function loadConfig (phase, dir, customConfig) {
if (userConfig.target && !targets.includes(userConfig.target)) {
throw new Error(`Specified target is invalid. Provided: "${userConfig.target}" should be one of ${targets.join(', ')}`)
}
if (userConfig.experimental) {
userConfig.experimental = {
...defaultConfig.experimental,
...userConfig.experimental
}
}
if (userConfig.onDemandEntries) {
userConfig.onDemandEntries = {
...defaultConfig.onDemandEntries,
...userConfig.onDemandEntries
}
}
return { ...defaultConfig, configOrigin: CONFIG_FILE, ...userConfig }
return assignDefaults({ configOrigin: CONFIG_FILE, ...userConfig })
}
return defaultConfig
......
......@@ -23,6 +23,12 @@ describe('config', () => {
expect(config.defaultConfig).toBeDefined()
})
it('Should assign object defaults deeply to user config', () => {
const config = loadConfig(PHASE_DEVELOPMENT_SERVER, pathToConfigFn)
expect(config.distDir).toEqual('.next')
expect(config.onDemandEntries.maxInactiveAge).toBeDefined()
})
it('Should pass the customConfig correctly', () => {
const config = loadConfig(PHASE_DEVELOPMENT_SERVER, null, { customConfig: true })
expect(config.customConfig).toBe(true)
......@@ -33,6 +39,23 @@ describe('config', () => {
expect(config.webpack).toBe(null)
})
it('Should assign object defaults deeply to customConfig', () => {
const config = loadConfig(PHASE_DEVELOPMENT_SERVER, null, { customConfig: true, onDemandEntries: { custom: true } })
expect(config.customConfig).toBe(true)
expect(config.onDemandEntries.maxInactiveAge).toBeDefined()
})
it('Should allow setting objects which do not have defaults', () => {
const config = loadConfig(PHASE_DEVELOPMENT_SERVER, null, { bogusSetting: { custom: true } })
expect(config.bogusSetting).toBeDefined()
expect(config.bogusSetting.custom).toBe(true)
})
it('Should override defaults for arrays from user arrays', () => {
const config = loadConfig(PHASE_DEVELOPMENT_SERVER, null, { pageExtensions: ['.bogus'] })
expect(config.pageExtensions).toEqual(['.bogus'])
})
it('Should throw when an invalid target is provided', () => {
try {
loadConfig(PHASE_DEVELOPMENT_SERVER, join(__dirname, '_resolvedata', 'invalid-target'))
......
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