feat(core): Added support for conditional plugins
This commit is contained in:
parent
3aeefc3748
commit
72c2a6c9e7
2 changed files with 26 additions and 2 deletions
|
@ -1,10 +1,17 @@
|
||||||
import Pattern from './pattern'
|
import Pattern from './pattern'
|
||||||
|
|
||||||
export default function Design(config, plugins = false) {
|
export default function Design(config, plugins = false, conditionalPlugins = false) {
|
||||||
const pattern = function (settings) {
|
const pattern = function (settings) {
|
||||||
Pattern.call(this, config)
|
Pattern.call(this, config)
|
||||||
|
// Load plugins
|
||||||
if (Array.isArray(plugins)) for (let plugin of plugins) this.use(plugin)
|
if (Array.isArray(plugins)) for (let plugin of plugins) this.use(plugin)
|
||||||
else if (plugins) this.use(plugins)
|
else if (plugins) this.use(plugins)
|
||||||
|
// Load conditional plugins
|
||||||
|
if (Array.isArray(conditionalPlugins))
|
||||||
|
for (let plugin of conditionalPlugins) this.useIf(plugin, settings)
|
||||||
|
else if (conditionalPlugins.plugin && conditionalPlugins.condition)
|
||||||
|
this.useIf(conditionalPlugins, settings)
|
||||||
|
|
||||||
this.apply(settings)
|
this.apply(settings)
|
||||||
|
|
||||||
return this
|
return this
|
||||||
|
|
|
@ -397,6 +397,23 @@ Pattern.prototype.use = function (plugin, data) {
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Pattern.prototype.useIf = function (plugin, settings) {
|
||||||
|
if (plugin.condition(settings)) {
|
||||||
|
if (this.debug)
|
||||||
|
this.raise.debug(
|
||||||
|
`Condition met: Loaded plugin \`${plugin.plugin.name}:${plugin.plugin.version}\``
|
||||||
|
)
|
||||||
|
this.loadPluginHooks(plugin.plugin, plugin.data)
|
||||||
|
} else {
|
||||||
|
if (this.debug)
|
||||||
|
this.raise.debug(
|
||||||
|
`Condition not met: Skipped loading plugin \`${plugin.plugin.name}:${plugin.plugin.version}\``
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
Pattern.prototype.loadPluginHooks = function (plugin, data) {
|
Pattern.prototype.loadPluginHooks = function (plugin, data) {
|
||||||
for (let hook of Object.keys(this.hooks)) {
|
for (let hook of Object.keys(this.hooks)) {
|
||||||
if (typeof plugin.hooks[hook] === 'function') {
|
if (typeof plugin.hooks[hook] === 'function') {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue