Socket
Book a DemoSign in
Socket

@webiny/plugins

Package Overview
Dependencies
Maintainers
1
Versions
662
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@webiny/plugins - npm Package Compare versions

Comparing version
0.0.0-unstable.7f63ea0744
to
0.0.0-unstable.81ae05e56b
+9
AsyncPluginsContainer.d.ts
import type { PluginCollection } from "./types.js";
import { PluginsContainer } from "./types.js";
export declare class AsyncPluginsContainer {
private readonly plugins;
private pluginsContainer;
constructor(plugins: PluginCollection | PluginsContainer);
init(): Promise<PluginsContainer>;
private traverseAndLoadPlugins;
}
import { PluginsContainer } from "./types.js";
const isPluginLoader = value => {
return typeof value === "function";
};
export class AsyncPluginsContainer {
constructor(plugins) {
this.plugins = plugins instanceof PluginsContainer ? plugins.all() : plugins;
}
async init() {
if (this.pluginsContainer) {
return this.pluginsContainer;
}
const plugins = await this.traverseAndLoadPlugins(this.plugins);
this.pluginsContainer = new PluginsContainer(plugins);
return this.pluginsContainer;
}
async traverseAndLoadPlugins(plugins) {
if (!Array.isArray(plugins)) {
return [plugins];
}
return plugins.reduce((acc, item) => {
return acc.then(async plugins => {
if (isPluginLoader(item)) {
const lazyPlugins = await item();
return [...plugins, ...(await this.traverseAndLoadPlugins(lazyPlugins))];
}
return [...plugins, ...(await this.traverseAndLoadPlugins(item))];
});
}, Promise.resolve([]));
}
}
//# sourceMappingURL=AsyncPluginsContainer.js.map
{"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":[]}
+4
-3

@@ -1,4 +0,5 @@

import { PluginsContainer } from "./PluginsContainer";
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 };
export { Plugin, PluginsContainer, plugins, AsyncPluginsContainer };

@@ -1,22 +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, "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 _Plugin = require("./Plugin");
const plugins = new _PluginsContainer.PluginsContainer();
exports.plugins = plugins;
//# sourceMappingURL=index.js.map

@@ -1,1 +0,1 @@

{"version":3,"names":["plugins","PluginsContainer"],"sources":["index.ts"],"sourcesContent":["import { PluginsContainer } from \"./PluginsContainer\";\nimport { Plugin } from \"./Plugin\";\n\nconst plugins = new PluginsContainer();\n\nexport { Plugin, PluginsContainer, plugins };\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AACA;AAEA,MAAMA,OAAO,GAAG,IAAIC,kCAAgB,EAAE;AAAC"}
{"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":[]}
{
"name": "@webiny/plugins",
"version": "0.0.0-unstable.7f63ea0744",
"version": "0.0.0-unstable.81ae05e56b",
"type": "module",
"main": "index.js",

@@ -16,14 +17,10 @@ "repository": {

"dependencies": {
"@babel/runtime": "7.20.13",
"uniqid": "5.4.0"
},
"devDependencies": {
"@babel/cli": "^7.19.3",
"@babel/core": "^7.19.3",
"@types/uniqid": "^5.3.2",
"@webiny/cli": "^0.0.0-unstable.7f63ea0744",
"@webiny/project-utils": "^0.0.0-unstable.7f63ea0744",
"rimraf": "^3.0.2",
"ttypescript": "^1.5.13",
"typescript": "4.7.4"
"@types/uniqid": "5.3.4",
"@webiny/build-tools": "0.0.0-unstable.81ae05e56b",
"rimraf": "6.1.3",
"typescript": "5.9.3",
"vitest": "4.1.0"
},

@@ -34,7 +31,3 @@ "publishConfig": {

},
"scripts": {
"build": "yarn webiny run build",
"watch": "yarn webiny run watch"
},
"gitHead": "7f63ea0744c9e31977e5dabb95538d22b4db585c"
"gitHead": "81ae05e56bcc774d1e077ca615a0005e736aa5a6"
}

@@ -1,12 +0,3 @@

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Plugin = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
class Plugin {
export class Plugin {
constructor() {
(0, _defineProperty2.default)(this, "name", void 0);
if (!this.constructor.type) {

@@ -20,3 +11,3 @@ throw Error(`Missing "type" definition in "${this.constructor.name}"!`);

}
exports.Plugin = Plugin;
(0, _defineProperty2.default)(Plugin, "type", void 0);
//# sourceMappingURL=Plugin.js.map

@@ -1,1 +0,1 @@

{"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":";;;;;;;;AAAO,MAAeA,MAAM,CAAC;EAIzBC,WAAW,GAAG;IAAA;IACV,IAAI,CAAE,IAAI,CAACA,WAAW,CAAmBC,IAAI,EAAE;MAC3C,MAAMC,KAAK,CAAE,iCAAgC,IAAI,CAACF,WAAW,CAACG,IAAK,IAAG,CAAC;IAC3E;EACJ;EAEA,IAAIF,IAAI,GAAG;IACP,OAAQ,IAAI,CAACD,WAAW,CAAmBC,IAAI;EACnD;AACJ;AAAC;AAAA,8BAbqBF,MAAM"}
{"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,5 @@

import { Plugin, PluginCollection } from "./types";
import type { Plugin, PluginCollection } from "./types.js";
export type WithName<T extends Plugin> = T & {
name: string;
};
export declare class PluginsContainer {

@@ -6,6 +9,8 @@ private plugins;

constructor(...args: PluginCollection);
byName<T extends Plugin>(name: T["name"]): T | null;
byType<T extends Plugin>(type: T["type"]): T[];
atLeastOneByType<T extends Plugin>(type: T["type"]): T[];
oneByType<T extends Plugin>(type: T["type"]): T;
byName<T extends Plugin>(name: T["name"]): WithName<T> | null;
byType<T extends Plugin>(type: T["type"]): WithName<T>[];
atLeastOneByType<T extends Plugin>(type: T["type"]): WithName<T>[];
oneByType<T extends Plugin>(type: T["type"]): WithName<T>;
merge(input: PluginsContainer | PluginCollection): void;
mergeByType(container: PluginsContainer, type: string): void;
all<T extends Plugin>(): T[];

@@ -12,0 +17,0 @@ register(...args: any): void;

@@ -1,10 +0,2 @@

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.PluginsContainer = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _uniqid = _interopRequireDefault(require("uniqid"));
import uniqid from "uniqid";
const isOptionsObject = item => item && !Array.isArray(item) && !item.type && !item.name;

@@ -21,4 +13,3 @@ const normalizeArgs = args => {

const assign = (plugins, options, target) => {
for (let i = 0; i < plugins.length; i++) {
const plugin = plugins[i];
for (const plugin of plugins) {
if (Array.isArray(plugin)) {

@@ -28,5 +19,15 @@ assign(plugin, options, target);

}
if (typeof plugin !== "object") {
throw new Error(`Could not register plugin. Expected an object, but got ${typeof plugin}.`);
}
if (!plugin.type) {
let name = "";
if (plugin.name) {
name = ` "${plugin.name}"`;
}
throw new Error(`Could not register plugin${name}. Missing "type" definition.`);
}
let name = plugin._name || plugin.name;
if (!name) {
plugin.name = name = (0, _uniqid.default)(plugin.type + "-");
plugin.name = name = uniqid(plugin.type + "-");
}

@@ -41,6 +42,6 @@

};
class PluginsContainer {
export class PluginsContainer {
plugins = {};
_byTypeCache = {};
constructor(...args) {
(0, _defineProperty2.default)(this, "plugins", {});
(0, _defineProperty2.default)(this, "_byTypeCache", {});
this.register(...args);

@@ -79,2 +80,12 @@ }

}
merge(input) {
if (input instanceof PluginsContainer) {
this.register(...input.all());
return;
}
this.register(input);
}
mergeByType(container, type) {
this.register(...container.byType(type));
}
all() {

@@ -98,2 +109,3 @@ return Object.values(this.plugins);

}
exports.PluginsContainer = PluginsContainer;
//# sourceMappingURL=PluginsContainer.js.map

@@ -1,1 +0,1 @@

{"version":3,"names":["isOptionsObject","item","Array","isArray","type","name","normalizeArgs","args","options","length","splice","assign","plugins","target","i","plugin","_name","uniqid","skipExisting","init","PluginsContainer","constructor","register","byName","byType","_byTypeCache","from","findByType","atLeastOneByType","list","Error","oneByType","all","Object","values","unregister","filter","pl"],"sources":["PluginsContainer.ts"],"sourcesContent":["import { Plugin, PluginCollection } from \"./types\";\nimport uniqid from \"uniqid\";\n\nconst isOptionsObject = (item?: any) => item && !Array.isArray(item) && !item.type && !item.name;\nconst normalizeArgs = (args: 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 = (plugins: any, options: any, target: Record<string, any>): void => {\n for (let i = 0; i < plugins.length; i++) {\n const plugin = plugins[i];\n if (Array.isArray(plugin)) {\n assign(plugin, options, target);\n continue;\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, Plugin[]> = {};\n\n constructor(...args: PluginCollection) {\n this.register(...args);\n }\n\n public byName<T extends Plugin>(name: T[\"name\"]): T | null {\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 T;\n }\n\n public byType<T extends Plugin>(type: T[\"type\"]): T[] {\n if (this._byTypeCache[type]) {\n return Array.from(this._byTypeCache[type]) as 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\"]): T[] {\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\"]): T {\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 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\"]): T[] {\n return (Object.values(this.plugins) as T[]).filter(pl => pl.type === type) as T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,CAAC,CAAC;EAClB;EAEOG,GAAG,GAA0B;IAChC,OAAOC,MAAM,CAACC,MAAM,CAAC,IAAI,CAACtB,OAAO,CAAC;EACtC;EAEOU,QAAQ,CAAC,GAAGf,IAAS,EAAQ;IAChC;IACA,IAAI,CAACkB,YAAY,GAAG,CAAC,CAAC;IACtB,MAAM,CAACb,OAAO,EAAEJ,OAAO,CAAC,GAAGF,aAAa,CAACC,IAAI,CAAC;IAC9CI,MAAM,CAACC,OAAO,EAAEJ,OAAO,EAAE,IAAI,CAACI,OAAO,CAAC;EAC1C;EAEOuB,UAAU,CAAC9B,IAAY,EAAQ;IAClC;IACA,IAAI,CAACoB,YAAY,GAAG,CAAC,CAAC;IACtB,OAAO,IAAI,CAACb,OAAO,CAACP,IAAI,CAAC;EAC7B;EAEQsB,UAAU,CAAmBvB,IAAe,EAAO;IACvD,OAAQ6B,MAAM,CAACC,MAAM,CAAC,IAAI,CAACtB,OAAO,CAAC,CAASwB,MAAM,CAACC,EAAE,IAAIA,EAAE,CAACjC,IAAI,KAAKA,IAAI,CAAC;EAC9E;AACJ;AAAC"}
{"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,CAAC,EAAE,CAAC,CAAC;EAClC;EAEA,OAAO,CAACH,IAAI,EAAEC,OAAO,CAAC;AAC1B,CAAC;AAED,MAAMG,MAAM,GAAGA,CACXC,OAA8B,EAC9BJ,OAAY,EACZK,MAA2B,KACpB;EACP,KAAK,MAAMC,MAAM,IAAIF,OAAO,EAAE;IAC1B,IAAIV,KAAK,CAACC,OAAO,CAACW,MAAM,CAAC,EAAE;MACvBH,MAAM,CAACG,MAAM,EAAEN,OAAO,EAAEK,MAAM,CAAC;MAC/B;IACJ;IAEA,IAAI,OAAOC,MAAM,KAAK,QAAQ,EAAE;MAC5B,MAAM,IAAIC,KAAK,CACX,0DAA0D,OAAOD,MAAM,GAC3E,CAAC;IACL;IAEA,IAAI,CAACA,MAAM,CAACV,IAAI,EAA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AM,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":[]}
# @webiny/plugins
[![](https://img.shields.io/npm/dw/@webiny/plugins.svg)](https://www.npmjs.com/package/@webiny/plugins)
[![](https://img.shields.io/npm/v/@webiny/plugins.svg)](https://www.npmjs.com/package/@webiny/plugins)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](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,3 +0,3 @@

export { PluginsContainer } from "./PluginsContainer";
export declare type Plugin<T = Record<string, any>> = {
export { PluginsContainer } from "./PluginsContainer.js";
export type Plugin<T = Record<string, any>> = {
type: string;

@@ -8,2 +8,3 @@ name?: string;

} & T;
export declare type PluginCollection = (Plugin | PluginCollection)[];
export type PluginCollection = (Plugin | PluginFactory | PluginCollection)[];
export type PluginFactory = () => Promise<Plugin | PluginCollection>;

@@ -1,12 +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 +0,1 @@

{"version":3,"names":[],"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 | PluginCollection)[];\n"],"mappings":";;;;;;;;;;;AAAA"}
{"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":[]}