Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: extensible plugins #174

Closed
jwr1 opened this issue Oct 2, 2022 · 1 comment
Closed

Feature: extensible plugins #174

jwr1 opened this issue Oct 2, 2022 · 1 comment

Comments

@jwr1
Copy link
Contributor

jwr1 commented Oct 2, 2022

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;
}
@jwr1 jwr1 changed the title Feature: extendable plugins Feature: extensible plugins Oct 2, 2022
@jwr1
Copy link
Contributor Author

jwr1 commented Oct 18, 2022

I'll close this for now because #176 was merged and provides similar functionality.

@jwr1 jwr1 closed this as completed Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant