@webiny/plugins
Advanced tools
@@ -1,2 +0,3 @@ | ||
| import { PluginCollection, PluginsContainer } from "./types"; | ||
| import type { PluginCollection } from "./types.js"; | ||
| import { PluginsContainer } from "./types.js"; | ||
| export declare class AsyncPluginsContainer { | ||
@@ -3,0 +4,0 @@ private readonly plugins; |
@@ -1,14 +0,8 @@ | ||
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports.AsyncPluginsContainer = void 0; | ||
| var _types = require("./types"); | ||
| import { PluginsContainer } from "./types.js"; | ||
| const isPluginLoader = value => { | ||
| return typeof value === "function"; | ||
| }; | ||
| class AsyncPluginsContainer { | ||
| export class AsyncPluginsContainer { | ||
| constructor(plugins) { | ||
| this.plugins = plugins instanceof _types.PluginsContainer ? plugins.all() : plugins; | ||
| this.plugins = plugins instanceof PluginsContainer ? plugins.all() : plugins; | ||
| } | ||
@@ -20,3 +14,3 @@ async init() { | ||
| const plugins = await this.traverseAndLoadPlugins(this.plugins); | ||
| this.pluginsContainer = new _types.PluginsContainer(plugins); | ||
| this.pluginsContainer = new PluginsContainer(plugins); | ||
| return this.pluginsContainer; | ||
@@ -39,4 +33,3 @@ } | ||
| } | ||
| exports.AsyncPluginsContainer = AsyncPluginsContainer; | ||
| //# sourceMappingURL=AsyncPluginsContainer.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"names":["_types","require","isPluginLoader","value","AsyncPluginsContainer","constructor","plugins","PluginsContainer","all","init","pluginsContainer","traverseAndLoadPlugins","Array","isArray","reduce","acc","item","then","lazyPlugins","Promise","resolve","exports"],"sources":["AsyncPluginsContainer.ts"],"sourcesContent":["import { Plugin, PluginCollection, PluginFactory, PluginsContainer } from \"~/types\";\n\nconst isPluginLoader = (value: unknown): value is PluginFactory => {\n return typeof value === \"function\";\n};\n\nexport class AsyncPluginsContainer {\n private readonly plugins: PluginCollection;\n private pluginsContainer: PluginsContainer | undefined;\n\n constructor(plugins: PluginCollection | PluginsContainer) {\n this.plugins = plugins instanceof PluginsContainer ? plugins.all() : plugins;\n }\n\n async init() {\n if (this.pluginsContainer) {\n return this.pluginsContainer;\n }\n\n const plugins = await this.traverseAndLoadPlugins(this.plugins);\n this.pluginsContainer = new PluginsContainer(plugins);\n\n return this.pluginsContainer;\n }\n\n private async traverseAndLoadPlugins(plugins: Plugin | PluginCollection): Promise<Plugin[]> {\n if (!Array.isArray(plugins)) {\n return [plugins];\n }\n\n return plugins.reduce<Promise<Plugin[]>>((acc, item) => {\n return acc.then(async plugins => {\n if (isPluginLoader(item)) {\n const lazyPlugins = await item();\n return [...plugins, ...(await this.traverseAndLoadPlugins(lazyPlugins))];\n }\n\n return [...plugins, ...(await this.traverseAndLoadPlugins(item))];\n });\n }, Promise.resolve([]));\n }\n}\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEA,MAAMC,cAAc,GAAIC,KAAc,IAA6B;EAC/D,OAAO,OAAOA,KAAK,KAAK,UAAU;AACtC,CAAC;AAEM,MAAMC,qBAAqB,CAAC;EAI/BC,WAAWA,CAACC,OAA4C,EAAE;IACtD,IAAI,CAACA,OAAO,GAAGA,OAAO,YAAYC,uBAAgB,GAAGD,OAAO,CAACE,GAAG,CAAC,CAAC,GAAGF,OAAO;EAChF;EAEA,MAAMG,IAAIA,CAAA,EAAG;IACT,IAAI,IAAI,CAACC,gBAAgB,EAAE;MACvB,OAAO,IAAI,CAACA,gBAAgB;IAChC;IAEA,MAAMJ,OAAO,GAAG,MAAM,IAAI,CAACK,sBAAsB,CAAC,IAAI,CAACL,OAAO,CAAC;IAC/D,IAAI,CAACI,gBAAgB,GAAG,IAAIH,uBAAgB,CAACD,OAAO,CAAC;IAErD,OAAO,IAAI,CAACI,gBAAgB;EAChC;EAEA,MAAcC,sBAAsBA,CAACL,OAAkC,EAAqB;IACxF,IAAI,CAACM,KAAK,CAACC,OAAO,CAACP,OAAO,CAAC,EAAE;MACzB,OAAO,CAACA,OAAO,CAAC;IACpB;IAEA,OAAOA,OAAO,CAACQ,MAAM,CAAoB,CAACC,GAAG,EAAEC,IAAI,KAAK;MACpD,OAAOD,GAAG,CAACE,IAAI,CAAC,MAAMX,OAAO,IAAI;QAC7B,IAAIJ,cAAc,CAACc,IAAI,CAAC,EAAE;UACtB,MAAME,WAAW,GAAG,MAAMF,IAAI,CAAC,CAAC;UAChC,OAAO,CAAC,GAAGV,OAAO,EAAE,IAAI,MAAM,IAAI,CAACK,sBAAsB,CAACO,WAAW,CAAC,CAAC,CAAC;QAC5E;QAEA,OAAO,CAAC,GAAGZ,OAAO,EAAE,IAAI,MAAM,IAAI,CAACK,sBAAsB,CAACK,IAAI,CAAC,CAAC,CAAC;MACrE,CAAC,CAAC;IACN,CAAC,EAAEG,OAAO,CAACC,OAAO,CAAC,EAAE,CAAC,CAAC;EAC3B;AACJ;AAACC,OAAA,CAAAjB,qBAAA,GAAAA,qBAAA","ignoreList":[]} | ||
| {"version":3,"names":["PluginsContainer","isPluginLoader","value","AsyncPluginsContainer","constructor","plugins","all","init","pluginsContainer","traverseAndLoadPlugins","Array","isArray","reduce","acc","item","then","lazyPlugins","Promise","resolve"],"sources":["AsyncPluginsContainer.ts"],"sourcesContent":["import type { Plugin, PluginCollection, PluginFactory } from \"~/types.js\";\nimport { PluginsContainer } from \"~/types.js\";\n\nconst isPluginLoader = (value: unknown): value is PluginFactory => {\n return typeof value === \"function\";\n};\n\nexport class AsyncPluginsContainer {\n private readonly plugins: PluginCollection;\n private pluginsContainer: PluginsContainer | undefined;\n\n constructor(plugins: PluginCollection | PluginsContainer) {\n this.plugins = plugins instanceof PluginsContainer ? plugins.all() : plugins;\n }\n\n async init() {\n if (this.pluginsContainer) {\n return this.pluginsContainer;\n }\n\n const plugins = await this.traverseAndLoadPlugins(this.plugins);\n this.pluginsContainer = new PluginsContainer(plugins);\n\n return this.pluginsContainer;\n }\n\n private async traverseAndLoadPlugins(plugins: Plugin | PluginCollection): Promise<Plugin[]> {\n if (!Array.isArray(plugins)) {\n return [plugins];\n }\n\n return plugins.reduce<Promise<Plugin[]>>((acc, item) => {\n return acc.then(async plugins => {\n if (isPluginLoader(item)) {\n const lazyPlugins = await item();\n return [...plugins, ...(await this.traverseAndLoadPlugins(lazyPlugins))];\n }\n\n return [...plugins, ...(await this.traverseAndLoadPlugins(item))];\n });\n }, Promise.resolve([]));\n }\n}\n"],"mappings":"AACA,SAASA,gBAAgB;AAEzB,MAAMC,cAAc,GAAIC,KAAc,IAA6B;EAC/D,OAAO,OAAOA,KAAK,KAAK,UAAU;AACtC,CAAC;AAED,OAAO,MAAMC,qBAAqB,CAAC;EAI/BC,WAAWA,CAACC,OAA4C,EAAE;IACtD,IAAI,CAACA,OAAO,GAAGA,OAAO,YAAYL,gBAAgB,GAAGK,OAAO,CAACC,GAAG,CAAC,CAAC,GAAGD,OAAO;EAChF;EAEA,MAAME,IAAIA,CAAA,EAAG;IACT,IAAI,IAAI,CAACC,gBAAgB,EAAE;MACvB,OAAO,IAAI,CAACA,gBAAgB;IAChC;IAEA,MAAMH,OAAO,GAAG,MAAM,IAAI,CAACI,sBAAsB,CAAC,IAAI,CAACJ,OAAO,CAAC;IAC/D,IAAI,CAACG,gBAAgB,GAAG,IAAIR,gBAAgB,CAACK,OAAO,CAAC;IAErD,OAAO,IAAI,CAACG,gBAAgB;EAChC;EAEA,MAAcC,sBAAsBA,CAACJ,OAAkC,EAAqB;IACxF,IAAI,CAACK,KAAK,CAACC,OAAO,CAACN,OAAO,CAAC,EAAE;MACzB,OAAO,CAACA,OAAO,CAAC;IACpB;IAEA,OAAOA,OAAO,CAACO,MAAM,CAAoB,CAACC,GAAG,EAAEC,IAAI,KAAK;MACpD,OAAOD,GAAG,CAACE,IAAI,CAAC,MAAMV,OAAO,IAAI;QAC7B,IAAIJ,cAAc,CAACa,IAAI,CAAC,EAAE;UACtB,MAAME,WAAW,GAAG,MAAMF,IAAI,CAAC,CAAC;UAChC,OAAO,CAAC,GAAGT,OAAO,EAAE,IAAI,MAAM,IAAI,CAACI,sBAAsB,CAACO,WAAW,CAAC,CAAC,CAAC;QAC5E;QAEA,OAAO,CAAC,GAAGX,OAAO,EAAE,IAAI,MAAM,IAAI,CAACI,sBAAsB,CAACK,IAAI,CAAC,CAAC,CAAC;MACrE,CAAC,CAAC;IACN,CAAC,EAAEG,OAAO,CAACC,OAAO,CAAC,EAAE,CAAC,CAAC;EAC3B;AACJ","ignoreList":[]} |
+3
-3
@@ -1,5 +0,5 @@ | ||
| import { PluginsContainer } from "./PluginsContainer"; | ||
| import { AsyncPluginsContainer } from "./AsyncPluginsContainer"; | ||
| import { Plugin } from "./Plugin"; | ||
| import { PluginsContainer } from "./PluginsContainer.js"; | ||
| import { AsyncPluginsContainer } from "./AsyncPluginsContainer.js"; | ||
| import { Plugin } from "./Plugin.js"; | ||
| declare const plugins: PluginsContainer; | ||
| export { Plugin, PluginsContainer, plugins, AsyncPluginsContainer }; |
+5
-28
@@ -1,30 +0,7 @@ | ||
| "use strict"; | ||
| import { PluginsContainer } from "./PluginsContainer.js"; | ||
| import { AsyncPluginsContainer } from "./AsyncPluginsContainer.js"; | ||
| import { Plugin } from "./Plugin.js"; | ||
| const plugins = new PluginsContainer(); | ||
| export { Plugin, PluginsContainer, plugins, AsyncPluginsContainer }; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| Object.defineProperty(exports, "AsyncPluginsContainer", { | ||
| enumerable: true, | ||
| get: function () { | ||
| return _AsyncPluginsContainer.AsyncPluginsContainer; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "Plugin", { | ||
| enumerable: true, | ||
| get: function () { | ||
| return _Plugin.Plugin; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "PluginsContainer", { | ||
| enumerable: true, | ||
| get: function () { | ||
| return _PluginsContainer.PluginsContainer; | ||
| } | ||
| }); | ||
| exports.plugins = void 0; | ||
| var _PluginsContainer = require("./PluginsContainer"); | ||
| var _AsyncPluginsContainer = require("./AsyncPluginsContainer"); | ||
| var _Plugin = require("./Plugin"); | ||
| const plugins = exports.plugins = new _PluginsContainer.PluginsContainer(); | ||
| //# sourceMappingURL=index.js.map |
+1
-1
@@ -1,1 +0,1 @@ | ||
| {"version":3,"names":["_PluginsContainer","require","_AsyncPluginsContainer","_Plugin","plugins","exports","PluginsContainer"],"sources":["index.ts"],"sourcesContent":["import { PluginsContainer } from \"./PluginsContainer\";\nimport { AsyncPluginsContainer } from \"./AsyncPluginsContainer\";\nimport { Plugin } from \"./Plugin\";\n\nconst plugins = new PluginsContainer();\n\nexport { Plugin, PluginsContainer, plugins, AsyncPluginsContainer };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,iBAAA,GAAAC,OAAA;AACA,IAAAC,sBAAA,GAAAD,OAAA;AACA,IAAAE,OAAA,GAAAF,OAAA;AAEA,MAAMG,OAAO,GAAAC,OAAA,CAAAD,OAAA,GAAG,IAAIE,kCAAgB,CAAC,CAAC","ignoreList":[]} | ||
| {"version":3,"names":["PluginsContainer","AsyncPluginsContainer","Plugin","plugins"],"sources":["index.ts"],"sourcesContent":["import { PluginsContainer } from \"./PluginsContainer.js\";\nimport { AsyncPluginsContainer } from \"./AsyncPluginsContainer.js\";\nimport { Plugin } from \"./Plugin.js\";\n\nconst plugins = new PluginsContainer();\n\nexport { Plugin, PluginsContainer, plugins, AsyncPluginsContainer };\n"],"mappings":"AAAA,SAASA,gBAAgB;AACzB,SAASC,qBAAqB;AAC9B,SAASC,MAAM;AAEf,MAAMC,OAAO,GAAG,IAAIH,gBAAgB,CAAC,CAAC;AAEtC,SAASE,MAAM,EAAEF,gBAAgB,EAAEG,OAAO,EAAEF,qBAAqB","ignoreList":[]} |
+8
-10
| { | ||
| "name": "@webiny/plugins", | ||
| "version": "5.44.1-beta.1", | ||
| "version": "5.45.0-beta.0", | ||
| "type": "module", | ||
| "main": "index.js", | ||
@@ -19,6 +20,7 @@ "repository": { | ||
| "devDependencies": { | ||
| "@types/uniqid": "5.3.2", | ||
| "@webiny/project-utils": "5.44.1-beta.1", | ||
| "rimraf": "6.0.1", | ||
| "typescript": "5.3.3" | ||
| "@types/uniqid": "5.3.4", | ||
| "@webiny/build-tools": "5.45.0-beta.0", | ||
| "rimraf": "6.1.3", | ||
| "typescript": "5.9.3", | ||
| "vitest": "4.0.18" | ||
| }, | ||
@@ -29,7 +31,3 @@ "publishConfig": { | ||
| }, | ||
| "scripts": { | ||
| "build": "node ../cli/bin.js run build", | ||
| "watch": "node ../cli/bin.js run watch" | ||
| }, | ||
| "gitHead": "ff5f085c417040291d39fdbe5153067f3a23ed86" | ||
| "gitHead": "b85c33cfbe7c02c130445c918d913ef4b2c09cb2" | ||
| } |
+1
-8
@@ -1,8 +0,2 @@ | ||
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports.Plugin = void 0; | ||
| class Plugin { | ||
| export class Plugin { | ||
| constructor() { | ||
@@ -17,4 +11,3 @@ if (!this.constructor.type) { | ||
| } | ||
| exports.Plugin = Plugin; | ||
| //# sourceMappingURL=Plugin.js.map |
+1
-1
@@ -1,1 +0,1 @@ | ||
| {"version":3,"names":["Plugin","constructor","type","Error","name","exports"],"sources":["Plugin.ts"],"sourcesContent":["export abstract class Plugin {\n public static readonly type: string;\n public name?: string;\n\n constructor() {\n if (!(this.constructor as typeof Plugin).type) {\n throw Error(`Missing \"type\" definition in \"${this.constructor.name}\"!`);\n }\n }\n\n get type() {\n return (this.constructor as typeof Plugin).type;\n }\n}\n"],"mappings":";;;;;;AAAO,MAAeA,MAAM,CAAC;EAIzBC,WAAWA,CAAA,EAAG;IACV,IAAI,CAAE,IAAI,CAACA,WAAW,CAAmBC,IAAI,EAAE;MAC3C,MAAMC,KAAK,CAAC,iCAAiC,IAAI,CAACF,WAAW,CAACG,IAAI,IAAI,CAAC;IAC3E;EACJ;EAEA,IAAIF,IAAIA,CAAA,EAAG;IACP,OAAQ,IAAI,CAACD,WAAW,CAAmBC,IAAI;EACnD;AACJ;AAACG,OAAA,CAAAL,MAAA,GAAAA,MAAA","ignoreList":[]} | ||
| {"version":3,"names":["Plugin","constructor","type","Error","name"],"sources":["Plugin.ts"],"sourcesContent":["export abstract class Plugin {\n public static readonly type: string;\n public name?: string;\n\n constructor() {\n if (!(this.constructor as typeof Plugin).type) {\n throw Error(`Missing \"type\" definition in \"${this.constructor.name}\"!`);\n }\n }\n\n get type() {\n return (this.constructor as typeof Plugin).type;\n }\n}\n"],"mappings":"AAAA,OAAO,MAAeA,MAAM,CAAC;EAIzBC,WAAWA,CAAA,EAAG;IACV,IAAI,CAAE,IAAI,CAACA,WAAW,CAAmBC,IAAI,EAAE;MAC3C,MAAMC,KAAK,CAAC,iCAAiC,IAAI,CAACF,WAAW,CAACG,IAAI,IAAI,CAAC;IAC3E;EACJ;EAEA,IAAIF,IAAIA,CAAA,EAAG;IACP,OAAQ,IAAI,CAACD,WAAW,CAAmBC,IAAI;EACnD;AACJ","ignoreList":[]} |
@@ -1,2 +0,2 @@ | ||
| import { Plugin, PluginCollection } from "./types"; | ||
| import type { Plugin, PluginCollection } from "./types.js"; | ||
| export type WithName<T extends Plugin> = T & { | ||
@@ -3,0 +3,0 @@ name: string; |
+3
-11
@@ -1,9 +0,2 @@ | ||
| "use strict"; | ||
| var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| exports.PluginsContainer = void 0; | ||
| var _uniqid = _interopRequireDefault(require("uniqid")); | ||
| import uniqid from "uniqid"; | ||
| const isOptionsObject = item => item && !Array.isArray(item) && !item.type && !item.name; | ||
@@ -37,3 +30,3 @@ const normalizeArgs = args => { | ||
| if (!name) { | ||
| plugin.name = name = (0, _uniqid.default)(plugin.type + "-"); | ||
| plugin.name = name = uniqid(plugin.type + "-"); | ||
| } | ||
@@ -48,3 +41,3 @@ | ||
| }; | ||
| class PluginsContainer { | ||
| export class PluginsContainer { | ||
| plugins = {}; | ||
@@ -114,4 +107,3 @@ _byTypeCache = {}; | ||
| } | ||
| exports.PluginsContainer = PluginsContainer; | ||
| //# sourceMappingURL=PluginsContainer.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"names":["_uniqid","_interopRequireDefault","require","isOptionsObject","item","Array","isArray","type","name","normalizeArgs","args","options","length","splice","assign","plugins","target","plugin","Error","_name","uniqid","skipExisting","init","PluginsContainer","_byTypeCache","constructor","register","byName","byType","from","findByType","atLeastOneByType","list","oneByType","merge","input","all","mergeByType","container","Object","values","unregister","filter","pl","exports"],"sources":["PluginsContainer.ts"],"sourcesContent":["import { Plugin, PluginCollection } from \"./types\";\nimport uniqid from \"uniqid\";\n\nexport type WithName<T extends Plugin> = T & { name: string };\n\nconst isOptionsObject = (item?: any) => item && !Array.isArray(item) && !item.type && !item.name;\nconst normalizeArgs = (args: any[]): [Plugin[], any] => {\n let options = {};\n\n // Check if last item in the plugins array is actually an options object.\n if (isOptionsObject(args[args.length - 1])) {\n [options] = args.splice(-1, 1);\n }\n\n return [args, options];\n};\n\nconst assign = (\n plugins: Plugin[] | Plugin[][],\n options: any,\n target: Record<string, any>\n): void => {\n for (const plugin of plugins) {\n if (Array.isArray(plugin)) {\n assign(plugin, options, target);\n continue;\n }\n\n if (typeof plugin !== \"object\") {\n throw new Error(\n `Could not register plugin. Expected an object, but got ${typeof plugin}.`\n );\n }\n\n if (!plugin.type) {\n let name = \"\";\n if (plugin.name) {\n name = ` \"${plugin.name}\"`;\n }\n throw new Error(`Could not register plugin${name}. Missing \"type\" definition.`);\n }\n\n let name = plugin._name || plugin.name;\n if (!name) {\n plugin.name = name = uniqid(plugin.type + \"-\");\n }\n\n // If skip existing was set to true, and a plugin with the same name was already registered, skip registration.\n if (!options.skipExisting || !target[name]) {\n target[name] = plugin;\n plugin.init && plugin.init();\n }\n }\n};\n\nexport class PluginsContainer {\n private plugins: Record<string, Plugin> = {};\n private _byTypeCache: Record<string, WithName<Plugin>[]> = {};\n\n constructor(...args: PluginCollection) {\n this.register(...args);\n }\n\n public byName<T extends Plugin>(name: T[\"name\"]) {\n if (!name) {\n return null;\n }\n /**\n * We can safely cast name as string, we know it is so.\n */\n return this.plugins[name as string] as WithName<T>;\n }\n\n public byType<T extends Plugin>(type: T[\"type\"]) {\n if (this._byTypeCache[type]) {\n return Array.from(this._byTypeCache[type]) as WithName<T>[];\n }\n const plugins = this.findByType<T>(type);\n this._byTypeCache[type] = plugins;\n return Array.from(plugins);\n }\n\n public atLeastOneByType<T extends Plugin>(type: T[\"type\"]) {\n const list = this.byType<T>(type);\n if (list.length === 0) {\n throw new Error(`There are no plugins by type \"${type}\".`);\n }\n return list;\n }\n\n public oneByType<T extends Plugin>(type: T[\"type\"]) {\n const list = this.atLeastOneByType<T>(type);\n if (list.length > 1) {\n throw new Error(\n `There is a requirement for plugin of type \"${type}\" to be only one registered.`\n );\n }\n return list[0];\n }\n\n public merge(input: PluginsContainer | PluginCollection): void {\n if (input instanceof PluginsContainer) {\n this.register(...input.all());\n return;\n }\n this.register(input);\n }\n\n public mergeByType(container: PluginsContainer, type: string): void {\n this.register(...container.byType(type));\n }\n\n public all<T extends Plugin>(): T[] {\n return Object.values(this.plugins) as T[];\n }\n\n public register(...args: any): void {\n // reset the cache when adding new plugins\n this._byTypeCache = {};\n const [plugins, options] = normalizeArgs(args);\n assign(plugins, options, this.plugins);\n }\n\n public unregister(name: string): void {\n // reset the cache when removing a plugin\n this._byTypeCache = {};\n delete this.plugins[name];\n }\n\n private findByType<T extends Plugin>(type: T[\"type\"]) {\n return Object.values(this.plugins).filter(\n (pl): pl is T => pl.type === type\n ) as WithName<T>[];\n }\n}\n"],"mappings":";;;;;;;AACA,IAAAA,OAAA,GAAAC,sBAAA,CAAAC,OAAA;AAIA,MAAMC,eAAe,GAAIC,IAAU,IAAKA,IAAI,IAAI,CAACC,KAAK,CAACC,OAAO,CAACF,IAAI,CAAC,IAAI,CAACA,IAAI,CAACG,IAAI,IAAI,CAACH,IAAI,CAACI,IAAI;A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AIM,KAAK,CACX,8CAA8CX,IAAI,8BACtD,CAAC;IACL;IACA,OAAOyB,IAAI,CAAC,CAAC,CAAC;EAClB;EAEOE,KAAKA,CAACC,KAA0C,EAAQ;IAC3D,IAAIA,KAAK,YAAYZ,gBAAgB,EAAE;MACnC,IAAI,CAACG,QAAQ,CAAC,GAAGS,KAAK,CAACC,GAAG,CAAC,CAAC,CAAC;MAC7B;IACJ;IACA,IAAI,CAACV,QAAQ,CAACS,KAAK,CAAC;EACxB;EAEOE,WAAWA,CAACC,SAA2B,EAAE/B,IAAY,EAAQ;IAChE,IAAI,CAACmB,QAAQ,CAAC,GAAGY,SAAS,CAACV,MAAM,CAACrB,IAAI,CAAC,CAAC;EAC5C;EAEO6B,GAAGA,CAAA,EAA0B;IAChC,OAAOG,MAAM,CAACC,MAAM,CAAC,IAAI,CAACzB,OAAO,CAAC;EACtC;EAEOW,QAAQA,CAAC,GAAGhB,IAAS,EAAQ;IAChC;IACA,IAAI,CAACc,YAAY,GAAG,CAAC,CAAC;IACtB,MAAM,CAACT,OAAO,EAAEJ,OAAO,CAAC,GAAGF,aAAa,CAACC,IAAI,CAAC;IAC9CI,MAAM,CAACC,OAAO,EAAEJ,OAAO,EAAE,IAAI,CAACI,OAAO,CAAC;EAC1C;EAEO0B,UAAUA,CAACjC,IAAY,EAAQ;IAClC;IACA,IAAI,CAACgB,YAAY,GAAG,CAAC,CAAC;IACtB,OAAO,IAAI,CAACT,OAAO,CAACP,IAAI,CAAC;EAC7B;EAEQsB,UAAUA,CAAmBvB,IAAe,EAAE;IAClD,OAAOgC,MAAM,CAACC,MAAM,CAAC,IAAI,CAACzB,OAAO,CAAC,CAAC2B,MAAM,CACpCC,EAAE,IAAcA,EAAE,CAACpC,IAAI,KAAKA,IACjC,CAAC;EACL;AACJ;AAACqC,OAAA,CAAArB,gBAAA,GAAAA,gBAAA","ignoreList":[]} | ||
| {"version":3,"names":["uniqid","isOptionsObject","item","Array","isArray","type","name","normalizeArgs","args","options","length","splice","assign","plugins","target","plugin","Error","_name","skipExisting","init","PluginsContainer","_byTypeCache","constructor","register","byName","byType","from","findByType","atLeastOneByType","list","oneByType","merge","input","all","mergeByType","container","Object","values","unregister","filter","pl"],"sources":["PluginsContainer.ts"],"sourcesContent":["import type { Plugin, PluginCollection } from \"./types.js\";\nimport uniqid from \"uniqid\";\n\nexport type WithName<T extends Plugin> = T & { name: string };\n\nconst isOptionsObject = (item?: any) => item && !Array.isArray(item) && !item.type && !item.name;\nconst normalizeArgs = (args: any[]): [Plugin[], any] => {\n let options = {};\n\n // Check if last item in the plugins array is actually an options object.\n if (isOptionsObject(args[args.length - 1])) {\n [options] = args.splice(-1, 1);\n }\n\n return [args, options];\n};\n\nconst assign = (\n plugins: Plugin[] | Plugin[][],\n options: any,\n target: Record<string, any>\n): void => {\n for (const plugin of plugins) {\n if (Array.isArray(plugin)) {\n assign(plugin, options, target);\n continue;\n }\n\n if (typeof plugin !== \"object\") {\n throw new Error(\n `Could not register plugin. Expected an object, but got ${typeof plugin}.`\n );\n }\n\n if (!plugin.type) {\n let name = \"\";\n if (plugin.name) {\n name = ` \"${plugin.name}\"`;\n }\n throw new Error(`Could not register plugin${name}. Missing \"type\" definition.`);\n }\n\n let name = plugin._name || plugin.name;\n if (!name) {\n plugin.name = name = uniqid(plugin.type + \"-\");\n }\n\n // If skip existing was set to true, and a plugin with the same name was already registered, skip registration.\n if (!options.skipExisting || !target[name]) {\n target[name] = plugin;\n plugin.init && plugin.init();\n }\n }\n};\n\nexport class PluginsContainer {\n private plugins: Record<string, Plugin> = {};\n private _byTypeCache: Record<string, WithName<Plugin>[]> = {};\n\n constructor(...args: PluginCollection) {\n this.register(...args);\n }\n\n public byName<T extends Plugin>(name: T[\"name\"]) {\n if (!name) {\n return null;\n }\n /**\n * We can safely cast name as string, we know it is so.\n */\n return this.plugins[name as string] as WithName<T>;\n }\n\n public byType<T extends Plugin>(type: T[\"type\"]) {\n if (this._byTypeCache[type]) {\n return Array.from(this._byTypeCache[type]) as WithName<T>[];\n }\n const plugins = this.findByType<T>(type);\n this._byTypeCache[type] = plugins;\n return Array.from(plugins);\n }\n\n public atLeastOneByType<T extends Plugin>(type: T[\"type\"]) {\n const list = this.byType<T>(type);\n if (list.length === 0) {\n throw new Error(`There are no plugins by type \"${type}\".`);\n }\n return list;\n }\n\n public oneByType<T extends Plugin>(type: T[\"type\"]) {\n const list = this.atLeastOneByType<T>(type);\n if (list.length > 1) {\n throw new Error(\n `There is a requirement for plugin of type \"${type}\" to be only one registered.`\n );\n }\n return list[0];\n }\n\n public merge(input: PluginsContainer | PluginCollection): void {\n if (input instanceof PluginsContainer) {\n this.register(...input.all());\n return;\n }\n this.register(input);\n }\n\n public mergeByType(container: PluginsContainer, type: string): void {\n this.register(...container.byType(type));\n }\n\n public all<T extends Plugin>(): T[] {\n return Object.values(this.plugins) as T[];\n }\n\n public register(...args: any): void {\n // reset the cache when adding new plugins\n this._byTypeCache = {};\n const [plugins, options] = normalizeArgs(args);\n assign(plugins, options, this.plugins);\n }\n\n public unregister(name: string): void {\n // reset the cache when removing a plugin\n this._byTypeCache = {};\n delete this.plugins[name];\n }\n\n private findByType<T extends Plugin>(type: T[\"type\"]) {\n return Object.values(this.plugins).filter(\n (pl): pl is T => pl.type === type\n ) as WithName<T>[];\n }\n}\n"],"mappings":"AACA,OAAOA,MAAM,MAAM,QAAQ;AAI3B,MAAMC,eAAe,GAAIC,IAAU,IAAKA,IAAI,IAAI,CAACC,KAAK,CAACC,OAAO,CAACF,IAAI,CAAC,IAAI,CAACA,IAAI,CAACG,IAAI,IAAI,CAACH,IAAI,CAACI,IAAI;AAChG,MAAMC,aAAa,GAAIC,IAAW,IAAsB;EACpD,IAAIC,OAAO,GAAG,CAAC,CAAC;;EAEhB;EACA,IAAIR,eAAe,CAACO,IAAI,CAACA,IAAI,CAACE,MAAM,GAAG,CAAC,CAAC,CAAC,EAAE;IACxC,CAACD,OAAO,CAAC,GAAGD,IAAI,CAACG,MAAM,CAAC,CAAC,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,CAAC,CAAC;MAC7B;IACJ;IACA,IAAI,CAACV,QAAQ,CAACS,KAAK,CAAC;EACxB;EAEOE,WAAWA,CAACC,SAA2B,EAAE9B,IAAY,EAAQ;IAChE,IAAI,CAACkB,QAAQ,CAAC,GAAGY,SAAS,CAACV,MAAM,CAACpB,IAAI,CAAC,CAAC;EAC5C;EAEO4B,GAAGA,CAAA,EAA0B;IAChC,OAAOG,MAAM,CAACC,MAAM,CAAC,IAAI,CAACxB,OAAO,CAAC;EACtC;EAEOU,QAAQA,CAAC,GAAGf,IAAS,EAAQ;IAChC;IACA,IAAI,CAACa,YAAY,GAAG,CAAC,CAAC;IACtB,MAAM,CAACR,OAAO,EAAEJ,OAAO,CAAC,GAAGF,aAAa,CAACC,IAAI,CAAC;IAC9CI,MAAM,CAACC,OAAO,EAAEJ,OAAO,EAAE,IAAI,CAACI,OAAO,CAAC;EAC1C;EAEOyB,UAAUA,CAAChC,IAAY,EAAQ;IAClC;IACA,IAAI,CAACe,YAAY,GAAG,CAAC,CAAC;IACtB,OAAO,IAAI,CAACR,OAAO,CAACP,IAAI,CAAC;EAC7B;EAEQqB,UAAUA,CAAmBtB,IAAe,EAAE;IAClD,OAAO+B,MAAM,CAACC,MAAM,CAAC,IAAI,CAACxB,OAAO,CAAC,CAAC0B,MAAM,CACpCC,EAAE,IAAcA,EAAE,CAACnC,IAAI,KAAKA,IACjC,CAAC;EACL;AACJ","ignoreList":[]} |
+6
-69
| # @webiny/plugins | ||
| [](https://www.npmjs.com/package/@webiny/plugins) | ||
| [](https://www.npmjs.com/package/@webiny/plugins) | ||
| [](https://github.com/prettier/prettier) | ||
| [](http://makeapullrequest.com) | ||
| A simple registry that stores all plugins in a shared object. | ||
| The only requirement for a plugin is to have a `name` and a `type` properties. | ||
| The rest is entirely up to you. | ||
| > [!NOTE] | ||
| > This package is part of the [Webiny](https://www.webiny.com) monorepo. | ||
| > It’s **included in every Webiny project by default** and is not meant to be used as a standalone package. | ||
| There is nothing spectacular going on under the hood, just a simple | ||
| object for storing references and a few utility functions. | ||
| 📘 **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs) | ||
| For more information, please visit [the official docs](https://docs.webiny.com/docs/developer-tutorials/plugins-crash-course). | ||
| ## Install | ||
| ``` | ||
| npm install --save @webiny/plugins | ||
| ``` | ||
| --- | ||
| Or if you prefer yarn: | ||
| ``` | ||
| yarn add @webiny/plugins | ||
| ``` | ||
| ## Usage | ||
| ### Adding a plugin | ||
| ``` | ||
| import { plugins } from "@webiny/plugins"; | ||
| // Add a plugin | ||
| plugins.register({ | ||
| name: "my-plugin", | ||
| type: "say-hi", | ||
| salute: () => "Hi!" | ||
| }); | ||
| plugins.register({ | ||
| name: "my-second-plugin", | ||
| type: "say-hi", | ||
| salute: () => "Yo!" | ||
| }); | ||
| ``` | ||
| ### Getting plugins by type | ||
| ``` | ||
| // anywhere in your app | ||
| import { plugins } from "@webiny/plugins"; | ||
| const pluginList = plugins.byType("say-hi"); | ||
| pluginList.forEach(plugin => { | ||
| // Call "salute" function | ||
| plugin.salute(); | ||
| }); | ||
| ``` | ||
| ### Getting a single plugin by name | ||
| ``` | ||
| // anywhere in your app | ||
| import { plugins } from "@webiny/plugins"; | ||
| const plugin = plugins.byName("my-plugin"); | ||
| // Call "salute" function | ||
| plugin.salute(); | ||
| ``` | ||
| ### Removing a plugin | ||
| ``` | ||
| // anywhere in your app | ||
| import { plugins } from "@webiny/plugins"; | ||
| plugins.unregister("my-plugin"); | ||
| ``` | ||
| _This README file is automatically generated during the publish process._ |
+1
-1
@@ -1,2 +0,2 @@ | ||
| export { PluginsContainer } from "./PluginsContainer"; | ||
| export { PluginsContainer } from "./PluginsContainer.js"; | ||
| export type Plugin<T = Record<string, any>> = { | ||
@@ -3,0 +3,0 @@ type: string; |
+1
-12
@@ -1,14 +0,3 @@ | ||
| "use strict"; | ||
| export { PluginsContainer } from "./PluginsContainer.js"; | ||
| Object.defineProperty(exports, "__esModule", { | ||
| value: true | ||
| }); | ||
| Object.defineProperty(exports, "PluginsContainer", { | ||
| enumerable: true, | ||
| get: function () { | ||
| return _PluginsContainer.PluginsContainer; | ||
| } | ||
| }); | ||
| var _PluginsContainer = require("./PluginsContainer"); | ||
| //# sourceMappingURL=types.js.map |
+1
-1
@@ -1,1 +0,1 @@ | ||
| {"version":3,"names":["_PluginsContainer","require"],"sources":["types.ts"],"sourcesContent":["export { PluginsContainer } from \"./PluginsContainer\";\n\nexport type Plugin<T = Record<string, any>> = {\n type: string;\n name?: string;\n init?: () => void;\n [key: string]: any;\n} & T;\n\nexport type PluginCollection = (Plugin | PluginFactory | PluginCollection)[];\n\nexport type PluginFactory = () => Promise<Plugin | PluginCollection>;\n"],"mappings":";;;;;;;;;;;AAAA,IAAAA,iBAAA,GAAAC,OAAA","ignoreList":[]} | ||
| {"version":3,"names":["PluginsContainer"],"sources":["types.ts"],"sourcesContent":["export { PluginsContainer } from \"./PluginsContainer.js\";\n\nexport type Plugin<T = Record<string, any>> = {\n type: string;\n name?: string;\n init?: () => void;\n [key: string]: any;\n} & T;\n\nexport type PluginCollection = (Plugin | PluginFactory | PluginCollection)[];\n\nexport type PluginFactory = () => Promise<Plugin | PluginCollection>;\n"],"mappings":"AAAA,SAASA,gBAAgB","ignoreList":[]} |
Yes
NaN21537
-12.57%5
25%197
-20.24%11
-85.33%