Modern.js supports three ways to set the range of browsers that a web application needs to support.
.browserslistrc FileModern.js supports setting the range of browsers that a web application needs to support. You can set the Browserslist values in the .browserslistrc file.
When you create a new Modern.js project, a default .browserslistrc configuration is included, which indicates that JavaScript code will be compiled to ES6 format.
chrome >= 51
edge >= 15
firefox >= 54
safari >= 10
ios_saf >= 10When the overrideBrowserslist configuration is not specified in the project, this .browserslistrc file will take effect.
You can also configure browserslist by setting the browserslist field in the package.json file:
{
"browserslist": [
"chrome >= 51"
// Other browser configurations...
]
}output.overrideBrowserslistYou can also configure browserslist by setting the output.overrideBrowserslist field in the modern.config.js file:
export default {
output: {
overrideBrowserslist: [
'chrome >= 51',
// Other browser configurations...
],
},
};The overrideBrowserslist configuration has a higher priority than the .browserslistrc file and the browserslist field in package.json.
In most scenarios, it is recommended to prioritize using the .browserslistrc file rather than the overrideBrowserslist configuration because the .browserslistrc file is the officially defined configuration file, has better general applicability, and can be recognized by other libraries in the community.
Please refer to Rsbuild - Setting Browser Range for more information.
Modern.js by default injects corresponding polyfill code at compile time through core-js.
By default, it will include the necessary polyfill code based on the project's Browserslist settings, so you generally do not need to worry about polyfill issues for your project source code and third-party dependencies. However, since some unused polyfill code is included, the final bundle size may increase.
For scenarios where certain third-party dependencies clearly do not require polyfills, you can set output.polyfill to usage. This way, Babel will only inject polyfill code based on the syntax used in the code during compilation.
Modern.js 中还提供了基于浏览器 UA 信息的运行时按需 Polyfill 方案,相比于 Babel 优势如下:
可以通过微生成器开启该功能:
? 请选择你想要的操作 启用可选功能
? 请选择功能名称 启用「基于 UA 的 Polyfill」功能执行命令后,在 modern.config.ts 中注册 Polyfill 插件:
import { polyfillPlugin } from '@modern-js/plugin-polyfill';
export default defineConfig({
plugins: [..., polyfillPlugin()],
});配置 output.polyfill 为 ua 并且执行 pnpm run build && pnpm run serve 启动服务器后,访问页面可以看到 HTML 产物中包含如下脚本:
<script src="/__polyfill__" crossorigin></script>在 Chrome 51 下访问页面可以看到 http://localhost:8080/__polyfill__ 返回内容如下:

该功能只有在使用 Modern.js 内置的 Web Server 时才会生效。
如果有自定义模版的需求,请参考 HTML 模板。通过 html.template 或 tools.html 手动修改模版时,可能会导致该功能无法正确生效。