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
.
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
.
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 |
Styled Components Plugin | Provides compile-time support for styled-components | tools.styledComponents |
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 |
Node Polyfill Plugin | Injects polyfills for Node core modules in the browser | output.disableNodePolyfill |
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 |
Pug Plugin | Provides support for the Pug template engine | tools.pug |
Rem Plugin | Implements rem adaptive layout for mobile pages | output.convertToRem |
YAML Plugin | Used to reference YAML files and convert them to JavaScript objects | YAML Files |
TOML Plugin | Used to reference TOML files and convert them to JavaScript objects | TOML Files |
The following are official Rsbuild plugins that are not built into Modern.js:
Image Compress Plugin: Compresses image resources used in the project.
Stylus Plugin: Uses Stylus as the CSS preprocessor.
UMD Plugin: Used to build UMD format artifacts.