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

feat: support for plugin communication #1972

Merged
merged 1 commit into from
Apr 1, 2024
Merged

Conversation

chenjiahan
Copy link
Member

Summary

This PR introduced two new plugin API for plugin communication:

const parentSymbol = Symbol('parent-api');

const pluginParent: RsbuildPlugin = {
  name: 'plugin-parent',
  setup(api) {
    api.expose<ParentAPI>(parentSymbol, {
      initial: 1,
      double: (val: number) => val * 2,
    });
  },
};

const pluginChild: RsbuildPlugin = {
  name: 'plugin-child',
  setup(api) {
    const parentAPI = api.useExposed<ParentAPI>(parentSymbol);
    expect(parentAPI?.double(parentAPI.initial)).toEqual(2);
  },
};

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

Copy link

netlify bot commented Apr 1, 2024

Deploy Preview for rsbuild ready!

Name Link
🔨 Latest commit c36f0f4
🔍 Latest deploy log https://app.netlify.com/sites/rsbuild/deploys/660aaf28f501f20008136015
😎 Deploy Preview https://deploy-preview-1972--rsbuild.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 75 (🔴 down 1 from production)
Accessibility: 97 (no change from production)
Best Practices: 100 (no change from production)
SEO: 92 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@chenjiahan chenjiahan merged commit 4c64a78 into main Apr 1, 2024
11 checks passed
@chenjiahan chenjiahan deleted the plugin_export_0401 branch April 1, 2024 13:07
const exposed: Array<{ id: string | symbol; api: any }> = [];

const expose = (id: string | symbol, api: any) => {
exposed.push({ id, api });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we freeze the api to avoid useExposed modifying it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should do this, and the RsbuildPluginAPI object should also be readonly.

But I want to avoid introducing runtime overhead, I will add TypeScript ReadOnly<T> first.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reconsidered and it seems can be allowed to modify the exposed object.

For example, pluginA can expose an object that contains some properties, and maybe pluginA want pluginB to modify the properties for communication in some cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants