Let's inject a global component as an example to see how to define and use plugins.
In order to get better type hints, you can install @rspress/shared
in the project, and then import the RspressPlugin
type through import type { RspressPlugin } from '@rspress/shared'
.
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.
Register plugins via plugins
in 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.