@webiny/plugins
Advanced tools
Comparing version 4.11.0 to 4.12.0-beta.0
{ | ||
"name": "@webiny/plugins", | ||
"version": "4.11.0", | ||
"version": "4.12.0-beta.0", | ||
"main": "index.js", | ||
@@ -32,3 +32,3 @@ "repository": { | ||
}, | ||
"gitHead": "0c48579d37956130d7672d13747250edb22021b3" | ||
"gitHead": "335cbb074d745f73795e4a5d507f9687aafe6f82" | ||
} |
import { Plugin } from "./types"; | ||
export declare class PluginsContainer { | ||
plugins: Record<string, Plugin>; | ||
constructor(plugins?: any); | ||
constructor(...args: any[]); | ||
byName<T extends Plugin = Plugin>(name: string): T; | ||
byType<T extends Plugin>(type?: string): T[]; | ||
register(...plugins: any): void; | ||
register(...args: any): void; | ||
unregister(name: string): void; | ||
} |
@@ -14,3 +14,15 @@ "use strict"; | ||
const assign = (plugins, target) => { | ||
const isOptionsObject = item => item && !Array.isArray(item) && !item.type && !item.name; | ||
const normalizeArgs = args => { | ||
let options = {}; // Check if last item in the plugins array is actually an options object. | ||
if (isOptionsObject(args[args.length - 1])) { | ||
[options] = args.splice(-1, 1); | ||
} | ||
return [args, options]; | ||
}; | ||
const assign = (plugins, options, target) => { | ||
for (let i = 0; i < plugins.length; i++) { | ||
@@ -20,3 +32,3 @@ const plugin = plugins[i]; | ||
if (Array.isArray(plugin)) { | ||
assign(plugin, target); | ||
assign(plugin, options, target); | ||
continue; | ||
@@ -29,6 +41,9 @@ } | ||
plugin.name = name = (0, _uniqid.default)(plugin.type + "-"); | ||
} // If skip existing was set to true, and a plugin with the same name was already registered, skip registration. | ||
if (!options.skipExisting || !target[name]) { | ||
target[name] = plugin; | ||
plugin.init && plugin.init(); | ||
} | ||
target[name] = plugin; | ||
plugin.init && plugin.init(); | ||
} | ||
@@ -38,5 +53,5 @@ }; | ||
class PluginsContainer { | ||
constructor(plugins = []) { | ||
constructor(...args) { | ||
(0, _defineProperty2.default)(this, "plugins", {}); | ||
assign(plugins, this.plugins); | ||
this.register(...args); | ||
} | ||
@@ -56,4 +71,5 @@ | ||
register(...plugins) { | ||
assign(plugins, this.plugins); | ||
register(...args) { | ||
const [plugins, options] = normalizeArgs(args); | ||
assign(plugins, options, this.plugins); | ||
} | ||
@@ -60,0 +76,0 @@ |
@@ -8,1 +8,2 @@ export { PluginsContainer } from "./PluginsContainer"; | ||
}; | ||
export declare type PluginCollection = (Plugin | PluginCollection)[]; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13962
122
2