Plug-in discovery system
source link: https://www.tuicool.com/articles/hit/ARVzIbz
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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 afterThere is currently support for file-based CommonJS modules (i.e. not via packages).
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-create-repository package as it will automatically add the missing module require calls.
CommonJS Babel plug-in / package
The CommonJS Babel plug-in package discovers DevSnicket plug-in usage and will automatically add module require calls where the package @devsnicket/plugin-discovery-create-repository has been used.
Install using npm
:
npm install --save-dev @devsnicket/plugin-discovery-commonjs-babel-plugin
Or with yarn
:
yarn add --dev @devsnicket/plugin-discovery-commonjs-babel-plugin
The Babel plug-in will need to be specified in your Babel configuration , WebPack Babel Loader configuration or equivalent.
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.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK