Write a plugin
Let's inject a global component as an example to see how to define and use plugins.
1. Define a plugin
plugin.ts
Example.tsx
A plugin is generally a function that receives some plugin params (optional) and returns an object that contains the name of the plugin and other config.
In the above example, we define a plugin named plugin-example, which will define a global environment variable process.env.SLUG during the build phase, and inject a global component Example.tsx in the document.
2. Use a plugin
Register plugins via plugins in rspress.config.ts:
rspress.config.ts
Then the Example component will be injected into the page and we can access the process.env.SLUG variable in the component.