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

Slightly changed invokeConfigSetters code #79

Merged
merged 1 commit into from
Jan 13, 2017
Merged
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
22 changes: 13 additions & 9 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function createConfig (webpack, configSetters) {
const context = { fileType, webpack }

invokePreHooks(configSetters, context)
const config = invokeConfigSetters(configSetters, context, {}, config)
const config = invokeConfigSetters(configSetters, context)
const postProcessedConfig = invokePostHooks(configSetters, context, config)

return postProcessedConfig
Expand Down Expand Up @@ -65,7 +65,15 @@ function group (configSetters) {
const pre = getHooks(configSetters, 'pre')
const post = getHooks(configSetters, 'post')

const groupBlock = (context, config) => invokeConfigSetters(configSetters, context, config)
const groupBlock = (context, config) => {
// `baseConfig` must be {}, so `invokeConfigSetters()` returns a config
// diff, not the whole merged config
const baseConfig = {}
// `getCompleteConfig` will make sure the whole config is passed as 2nd param when
// the config setters are invoked, even though the baseConfig is `{}`, not `config`
const getCompleteConfig = (incompleteConfig) => merge.smart(config, incompleteConfig)
return invokeConfigSetters(configSetters, context, baseConfig, getCompleteConfig)
}

return Object.assign(groupBlock, { pre, post })
}
Expand All @@ -84,17 +92,13 @@ function getHooks (configSetters, type) {
return filterDuplicates(flattenedHooks)
}

function invokeConfigSetters (configSetters, context, baseConfig = {}, initialConfig = {}) {
const getCompleteConfig = Object.keys(baseConfig).length > 0
? (mergedConfig) => merge.smart(baseConfig, mergedConfig)
: (mergedConfig) => mergedConfig

function invokeConfigSetters (configSetters, context, baseConfig = {}, getCompleteConfig = (config) => config) {
return configSetters.reduce(
(mergedConfig, setter) => {
const configPartial = setter(context, getCompleteConfig(mergedConfig))
return merge.smart(mergedConfig, configPartial)
},
initialConfig
baseConfig
)
}

Expand All @@ -105,7 +109,7 @@ function invokePreHooks (configSetters, context) {

function invokePostHooks (configSetters, context, config) {
const postHooks = getHooks(configSetters, 'post')
return invokeConfigSetters(postHooks, context, config, config)
return invokeConfigSetters(postHooks, context, config)
}

function filterDuplicates (array) {
Expand Down