Feature: extensible plugins
jwr1 opened this issue · comments
This proposal would allow for plugins to expose a custom API for other plugins to use. It better follows Rollup's already existing plugin communication design compared to #171 so we can follow a more general implementation of Rollup's api. It also adds the ability to create a new instance of plugins (in case one does not already exist), suggested in #173.
Proof of Concept
Define API:
interface API {
presets: Record<string, unknown>
}
export const unplugin = createUnplugin<Options, API>((options) => {
return {
name: 'unplugin-auto-import',
api: {
//...methods and properties exposed for other plugins
presets: {
vue: '...',
react: '...',
}
},
buildStart() {
const presets = this.api.presets; // Access own API object through context
},
};
});
Use in another plugin:
interface Plugins {
UnpluginAutoImport: import('unplugin-auto-import')
}
export const unplugin = createUnplugin<Options, API, Plugins>((options) => {
return {
name: 'unplugin-auto-import-presets',
plugins: [
{
use: 'unplugin-auto-import',
orderAfter: true
},
],
buildStart() {
this.plugins.UnpluginAutoImport.presets.newPreset = '...';
}
};
});
Plugin import interface:
interface PluginImport {
/** module name or UnpluginInstance */
use: string | UnpluginInstance;
/** override context key that the imported plugin's api is injected to */
as?: string;
/** default creates new instance if one does not already exist, otherwise, creation follows boolean value */
create?: boolean;
/** if plugin is created, the options to pass to plugin */
options?: unknown;
/** if plugin is created, execute hooks after */
orderAfter?: boolean;
}
I'll close this for now because #176 was merged and provides similar functionality.