javascript-plugin-architecture-with-typescript-definitions
Plugin architecture example with full TypeScript support
The goal of this repository is to provide a template of a simple plugin Architecture which allows plugins to created and authored as separate npm modules and shared as official or 3rd party plugins.
Usage
Try it in TypeScript's playground editor
import { Base } from "javascript-plugin-architecture-with-typescript-definitions";
function myFooPlugin(instance: Base) {
return {
foo: () => "foo",
};
}
function myBarPlugin(instance: Base) {
return {
bar: () => "bar",
};
}
const FooTest = Base.plugin(myFooPlugin);
const fooTest = new FooTest();
fooTest.foo(); // has full TypeScript intellisense
const FooBarTest = Base.plugin(myFooPlugin, myBarPlugin);
const fooBarTest = new FooBarTest();
fooBarTest.foo(); // has full TypeScript intellisense
fooBarTest.bar(); // has full TypeScript intellisense
The constructor accepts an optional options
object which is passed to the plugins as second argument and stored in instance.options
. Default options can be set using Base.defaults(options)
const BaseWithOptions = Base.defaults({ foo: "bar" });
const instance = new BaseWithOptions();
instance.options; // {foo: 'bar'}
Defaults
TypeScript will not complain when chaining .defaults()
calls endlessly: the static .defaultOptions
property will be set correctly. However, when instantiating from a class with 4+ chained .defaults()
calls, then only the defaults from the first 3 calls are supported. See #57 for details.
Credit
This plugin architecture was extracted from @octokit/core
. The implementation was made possible by help from @karol-majewski, @dragomirtitian, and StackOverflow user "hackape".