@devsnicket/plugin-discovery-create-repository
v1.5.1
Published
A factory function that creates a repository to which functions/objects etc can be plugged into and discovered from elsewhere. Discovery requires a Babel plug-in to add module imports @devsnicket/plugin-discovery-commonjs-babel-plugin.
Downloads
23
Maintainers
Readme
Plug-in Discovery
DevSnicket Plug-in Discovery consists of a JavaScript repository object and a Babel plug-in that when used together can invert dependencies between modules and turn them into discovered plug-ins.
before | after ------ | ----- |
Currently only CommonJS modules are supported (e.g. not ECMAScript ones).
Plug-in and repository files/modules in packages are supported (see "Forwarder lookup" in @devsnicket/plugin-discovery-commonjs-babel-plugin).
Create repository factory function / package
Install using npm
:
npm install @devsnicket/plugin-discovery-create-repository
Or with yarn
:
yarn add @devsnicket/plugin-discovery-create-repository
The create repository package contains a factory function. When this function is called and the return repository object is exported a plug-in contract is defined (in this example "someSortOfPlugins").
// someSortOfPlugins.js
module.exports =
require("@devsnicket/plugin-discovery-create-repository")
();
This can then be plugged into by objects/functions etc from other files.
// plugin1.js
require("./someSortOfPlugins")
.plugin("plug-in number 1");
The plug-ins can then be iterated and used from other files.
// consoleLogPlugins.js
for (const plugin require("./someSortOfPlugins"))
console.log(plugin);
// output:
// plug-in number 1
The code above alone won't work as there isn't a module require call to import the 2nd plug-in file from the 3rd file that iterates the plug-ins. The purpose of the repository object is so plug-ins can be identified by the @devsnicket/plugin-discovery-commonjs-babel-plugin package as it will automatically add the missing module require calls.
Example
An example of Plug-in Discovery in use can be found in Eunice. A plug-in repository is defined for its test harnesses (Harnesses/processorPlugins.js) so processors can be discovered and included automatically.