Modern.js provides a powerful plugin system that allows developers to extend the framework's functionality, customize the build process, and meet a variety of personalized development needs. Whether you want to add a custom command, optimize build output, or implement a unique deployment solution, Modern.js's plugin system provides robust support.
In web application development, we often encounter needs that the framework itself cannot directly satisfy, such as:
.xyz.Without a plugin system, these requirements might require modifying the framework's source code or resorting to cumbersome hacks. Modern.js's plugin system offers an elegant, flexible, and maintainable solution.
Modern.js offers two main types of plugins: Modern.js framework plugins and Rsbuild build plugins. The choice of which plugin to use depends on your specific needs:
Rsbuild Build Plugins: If your needs are closely related to the build process, especially involving modifications to Webpack or Rspack configuration, then you should choose an Rsbuild plugin. For example:
loader or plugin configurations.Modern.js Framework Plugins: If your needs relate to the extension of Modern.js framework itself, runtime behavior, or server-side logic, then you should choose a Modern.js plugin. For example:
In short, use Rsbuild plugins when you need to modify Webpack/Rspack configurations; use Modern.js plugins for other framework-related extensions.
Modern.js framework plugins can be further divided into three categories:
CLI plugins are used to provide additional functionality when running modern commands in the application, such as adding commands, modifying configurations, and listening for file changes. Most build-related capabilities can be implemented through CLI plugins.
CLI plugins can be configured via the plugins field in modern.config.ts.
// an example for bff
import { appTools, defineConfig } from '@modern-js/app-tools';
import { bffPlugin } from '@modern-js/plugin-bff';
export default defineConfig({
plugins: [appTools(), bffPlugin()],
});Runtime plugins are used to provide additional functionality when the application is running React code, such as performing initialization behaviors and implementing React Higher-Order Component (HOC) encapsulation.
Runtime plugins are configured via the plugins field in src/modern.runtime.ts.
import { defineRuntimeConfig } from '@modern-js/runtime';
import { routerPlugin } from '@modern-js/runtime/router';
export default defineRuntimeConfig({
plugins: [routerPlugin()],
});Server plugins are used to provide additional functionality when the application receives requests, such as adding middleware and modifying request responses.
Server plugins are configured via the plugins field in server/modern.server.ts.
import { defineServerConfig } from '@modern-js/server-runtime';
export default defineServerConfig({
plugins: [
{
name: 'custom-plugin-in-config',
setup: api => {
api.onPrepare(() => {
const { middlewares } = api.getServerContext();
middlewares?.push({
name: 'server-plugin-middleware',
handler: async (c, next) => {
c.res.headers.set('x-render-middleware-plugin', 'ok');
await next();
},
});
});
},
},
],
});
Modern.js officially uses the new plugin mechanism starting from 2.66.0.
If your current version is lower than 2.66.0, you can upgrade by running npx modern upgrade.
If you need to develop Modern.js framework plugins, please read Modern.js Plugin System for more information.
Rsbuild is the underlying build tool for Modern.js. By adding Rsbuild plugins, you can modify the default build behavior and add various additional functionalities, including but not limited to:
You can register Rsbuild plugins in modern.config.ts via the builderPlugins option. See builderPlugins for details.
Starting from 2.46.0, Modern.js upgraded its underlying build tool to Rsbuild.
If your current version is lower than 2.46.0, you can upgrade by running npx modern upgrade.
You can read Rsbuild Official Website - Plugins to learn more about the Rsbuild plugin system.
The following are official Rsbuild plugins that are already built into Modern.js. They can be enabled without installation:
| Plugin | Description | Modern.js Link |
|---|---|---|
| React Plugin | Provides support for React | - |
| SVGR Plugin | Supports converting SVG images into React components | output.disableSvgr output.svgDefaultExport |
| Assets Retry Plugin | Automatically retries requests when static asset loading fails | output.assetsRetry |
| Type Check Plugin | Runs TypeScript type checking in a separate process | output.disableTsChecker tools.tsChecker |
| Source Build Plugin | For monorepo scenarios, supports referencing source code from other subdirectories and completing builds and hot updates | experiments.sourceBuild |
| Check Syntax Plugin | Analyzes the syntax compatibility of the build artifacts to determine if there are any advanced syntax features that cause compatibility issues | security.checkSyntax |
| CSS Minimizer Plugin | Used to customize the CSS compression tool, switch to cssnano or other tools for CSS compression | tools.minifyCss |
| Rem Plugin | Implements rem adaptive layout for mobile pages | output.convertToRem |
The following are official Rsbuild plugins that are not built into Modern.js: