logo
  • Guide
  • Config
  • Plugin
  • API
  • Examples
  • Community
  • Modern.js 2.x Docs
  • English
    • 简体中文
    • English
    • Start
      Introduction
      Quick Start
      Upgrading
      Glossary
      Tech Stack
      Core Concept
      Page Entry
      Build Engine
      Web Server
      Basic Features
      Routes
      Routing
      Config Routes
      Data Solution
      Data Fetching
      Data Writing
      Data Caching
      Rendering
      Server-Side Rendering
      Streaming SSR
      Rendering Cache
      Static Site Generation
      Render Preprocessing
      Styling
      Styling
      Use CSS Modules
      Using CSS-in-JS
      Using Tailwind CSS
      HTML Template
      Import Static Assets
      Import JSON Files
      Import SVG Assets
      Import Wasm Assets
      Debug
      Data Mocking
      Network Proxy
      Using Rsdoctor
      Using Storybook
      Testing
      Playwright
      Vitest
      Jest
      Cypress
      Path Alias
      Environment Variables
      Output Files
      Deploy Application
      Advanced Features
      Using Rspack
      Using BFF
      Basic Usage
      Runtime Framework
      Extend BFF Server
      Extend Request SDK
      File Upload
      Cross-Project Invocation
      Optimize Page Performance
      Code Splitting
      Inline Static Assets
      Bundle Size Optimization
      React Compiler
      Improve Build Performance
      Browser Compatibility
      Low-Level Tools
      Source Code Build Mode
      Server Monitor
      Monitors
      Logs Events
      Metrics Events
      Internationalization
      Basic Concepts
      Quick Start
      Configuration
      Locale Detection
      Resource Loading
      Routing Integration
      API Reference
      Advanced Usage
      Best Practices
      Custom Web Server
      Topic Detail
      Module Federation
      Introduction
      Getting Started
      Application-Level Modules
      Server-Side Rendering
      Deployment
      Integrating Internationalization
      FAQ
      Dependencies FAQ
      CLI FAQ
      Build FAQ
      HMR FAQ
      Deprecated
      📝 Edit this page
      Previous pageImprove Build PerformanceNext pageLow-Level Tools

      #Browser Compatibility

      #Browserslist Configuration

      Modern.js supports three ways to set the range of browsers that a web application needs to support.

      #Method 1: Configure via .browserslistrc File

      Modern.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.

      .browserslistrc
      chrome >= 51
      edge >= 15
      firefox >= 54
      safari >= 10
      ios_saf >= 10

      When the overrideBrowserslist configuration is not specified in the project, this .browserslistrc file will take effect.

      #Method 2: Configure via package.json

      You can also configure browserslist by setting the browserslist field in the package.json file:

      package.json
      {
        "browserslist": [
          "chrome >= 51"
          // Other browser configurations...
        ]
      }

      #Method 3: Configure via output.overrideBrowserslist

      You can also configure browserslist by setting the output.overrideBrowserslist field in the modern.config.js file:

      modern.config.js
      export default {
        output: {
          overrideBrowserslist: [
            'chrome >= 51',
            // Other browser configurations...
          ],
        },
      };

      #Configuration Priority

      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.

      Tip

      Please refer to Rsbuild - Setting Browser Range for more information.

      #Polyfill

      #Compile-time Polyfill

      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.

      Info

      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.

      #运行时按需 Polyfill

      Modern.js 中还提供了基于浏览器 UA 信息的运行时按需 Polyfill 方案,相比于 Babel 优势如下:

      • 不会插入到代码中,只根据访问页面的设备,按需下发 Polyfill 代码 ,减少整体代码体积。
      • 相同浏览器会公用一份 Polyfill 代码。因此,随着项目越来越多,基于 UA 的 Polyfill 代码下发速度会越来越快,综合速度超过常规方案。

      可以通过微生成器开启该功能:

      ? 请选择你想要的操作 启用可选功能
      ? 请选择功能名称 启用「基于 UA 的 Polyfill」功能

      执行命令后,在 modern.config.ts 中注册 Polyfill 插件:

      modern.config.ts
      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__ 返回内容如下:

      ua-polyfill

      注意

      该功能只有在使用 Modern.js 内置的 Web Server 时才会生效。

      如果有自定义模版的需求,请参考 HTML 模板。通过 html.template 或 tools.html 手动修改模版时,可能会导致该功能无法正确生效。