Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Feb 20, 2022
1 parent 924a21b commit 025d486
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 30 deletions.
9 changes: 6 additions & 3 deletions src/config/plugin/lib/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import { normalizeShape } from './shape.js'
export const addPlugin = async function (
pluginConfig,
{
pluginType: { builtins, pluginProp, shape, item },
sharedPropNames,
sharedConfig,
builtins,
pluginProp,
shape,
item,
context,
cwd,
sharedConfig,
sharedPropNames,
},
) {
const { [pluginProp]: id, moduleId, parents, ...pluginConfigA } = pluginConfig
Expand Down
10 changes: 5 additions & 5 deletions src/config/plugin/lib/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import { getListItemsDefinitions } from './item.js'
import { safeNormalizeConfig } from './normalize.js'

// Normalize the main property, i.e. the list of `pluginsConfigs`
export const normalizeList = async function ({
pluginType: {
export const normalizeList = async function (
{
list,
default: defaultValue,
name,
multiple,
builtins,
pluginProp,
modulePrefix,
context,
cwd,
},
pluginConfigs = defaultValue,
context,
cwd,
}) {
) {
const isArray = Array.isArray(pluginConfigs)
const pluginConfigsA = await safeNormalizeConfig(
pluginConfigs,
Expand Down
25 changes: 5 additions & 20 deletions src/config/plugin/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,16 @@ import { normalizePluginType } from './type.js'
// - Plugins without configuration
// - Single plugin per type, as opposed to multiple
// - Single configuration per plugin
export const addPlugins = async function (
pluginConfigs,
pluginType,
{ sharedConfig = {}, context, cwd } = {},
) {
export const addPlugins = async function (pluginConfigs, pluginType) {
const pluginTypeA = normalizePluginType(pluginType)
const { sharedConfig: sharedConfigA, sharedPropNames } = getSharedConfig(
sharedConfig,
pluginTypeA,
)

const pluginConfigsA = await normalizeList({
pluginConfigs,
pluginType: pluginTypeA,
context,
cwd,
})
const { sharedConfig, sharedPropNames } = getSharedConfig(pluginTypeA)
const pluginConfigsA = await normalizeList(pluginTypeA, pluginConfigs)
const plugins = await Promise.all(
pluginConfigsA.map((pluginConfig) =>
addPlugin(pluginConfig, {
pluginType: pluginTypeA,
...pluginTypeA,
sharedPropNames,
sharedConfig: sharedConfigA,
context,
cwd,
sharedConfig,
}),
),
)
Expand Down
2 changes: 1 addition & 1 deletion src/config/plugin/lib/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { set } from '../../normalize/lib/prop_path/set.js'

// Retrieve top-level properties that are shared with all plugins of a specific
// type. Those are merged with plugin-specific properties.
export const getSharedConfig = function (sharedConfig, { item }) {
export const getSharedConfig = function ({ item, sharedConfig }) {
const sharedPropNames = getSharedConfigPropNames(item)
const sharedConfigProps = sharedPropNames.flatMap((sharedPropName) =>
Object.entries(list(sharedConfig, sharedPropName)),
Expand Down
6 changes: 6 additions & 0 deletions src/config/plugin/lib/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export const normalizePluginType = function ({
shape = [],
list = {},
item = [],
sharedConfig = {},
context,
cwd,
}) {
return {
name,
Expand All @@ -22,5 +25,8 @@ export const normalizePluginType = function ({
shape,
list,
item,
sharedConfig,
context,
cwd,
}
}
3 changes: 2 additions & 1 deletion src/config/plugin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ const addConfigPlugins = async function ({
cwd,
}) {
try {
return await addPlugins(pluginConfigs, pluginType, {
return await addPlugins(pluginConfigs, {
...pluginType,
sharedConfig: config,
context,
cwd,
Expand Down

0 comments on commit 025d486

Please sign in to comment.