国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Web Front-end JS Tutorial Why is ES6 code not translated to ES5 when Vite builds JS library?

Why is ES6 code not translated to ES5 when Vite builds JS library?

Apr 04, 2025 am 11:30 AM
Browser ai

Why is ES6 code not translated to ES5 when Vite builds JS library?

Vite builds JS library: Solution for ES6 code not translated to ES5

When using Vite to build JS libraries, developers often encounter problems that ES6 code is not translated to ES5, which leads to compatibility errors in the old browser environment. This article analyzes a case and provides solutions.

In the case, the developer used Vite to build the JS library and used ES6's null value merge operator (??), but the packaged code still contains ES6 syntax, resulting in an error. Its vite.config.js configuration is as follows:

 import { defineConfig } from "vite";
import babel from "@rollup/plugin-babel";

export default defineConfig({
  build: {
    lib: {
      entry: "./lib/main.js",
      name: "FastcodeTest",
      fileName: (format) => `fastcodeTest.${format}.js`,
      formats: ["es", "umd"],
    },
  },
  rollupOptions: {
    plugins: [
      babel({
        babelHelpers: "bundled",
        exclude: "node_modules/**",
        presets: [
          [
            "@babel/preset-env",
            {
              targets: {
                browsers: "> 0.25%, not dead", // Set the target browser},
              useBuiltIns: "entry",
              corejs: 3,
            },
          ],
        ],
      }),
    ],
  },
});

The root cause of the problem lies in targets: { browsers: "> 0.25%, not dead" } . This configuration sets the target browser to a browser with a share of more than 0.25% and no maintenance has been stopped. Modern browsers natively support ES6, so Babel will not translate.

Solution:

To ensure ES5 compatibility, you need to modify targets configuration to explicitly specify lower version browsers, for example:

 targets: {
  browsers: ["ie >= 11", "last 2 versions", "not dead"] // or more specific version number}

Alternatively, for more granular control, consider using a more comprehensive browser compatibility list, such as the configuration provided by browserslist . In addition, other reasons that may cause errors, such as node environment version differences, need to be ruled out. Only after other factors are excluded, the Babel configuration will be adjusted. Make sure the @babel/preset-env plugin is installed correctly.

By modifying targets configuration, Babel will correctly translate the ES6 code to ES5, thus solving the compatibility issue. Remember, choosing the right targets configuration depends on the support range of your target browser.

The above is the detailed content of Why is ES6 code not translated to ES5 when Vite builds JS library?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
Toncoin latest price trend app 24-hour TON coin k-line chart online analysis Toncoin latest price trend app 24-hour TON coin k-line chart online analysis Aug 01, 2025 pm 09:42 PM

Toncoin (TON) is a decentralized first-tier blockchain originally conceived by the Telegram team. It is known for its high performance, low cost and user-friendly features, and aims to provide an open network platform for billions of users around the world. Its native token TON is used in the network to pay transaction fees, pledge and participate in network governance.

How to download the Binance official app Binance Exchange app download link to get How to download the Binance official app Binance Exchange app download link to get Aug 04, 2025 pm 11:21 PM

As the internationally leading blockchain digital asset trading platform, Binance provides users with a safe and convenient trading experience. Its official app integrates multiple core functions such as market viewing, asset management, currency trading and fiat currency trading.

How can you check if the user's browser has JavaScript enabled? How can you check if the user's browser has JavaScript enabled? Aug 03, 2025 pm 12:19 PM

UsethetagtodisplayamessageorredirectuserswhenJavaScriptisdisabled.2.ApplygracefuldegradationbybuildingcorefunctionalitywithoutJavaScriptandenhancingitwhenavailable.3.Adda"no-js"classtotheHTMLelementanduseJavaScripttoreplaceitwith"js&qu

How to download the latest version of Binance app Binance app latest version v3.0.7 How to download the latest version of Binance app Binance app latest version v3.0.7 Aug 01, 2025 pm 09:27 PM

Binance is an internationally renowned digital asset trading platform, committed to providing global users with a safe and efficient trading experience. As its mobile application, Binance official App integrates market viewing, transaction execution and asset management, allowing users to grasp market dynamics anytime, anywhere.

The latest version of the official app of Huobi.com is installed. The official website of Huobi Exchange is the entrance address of the official website of Huobi Exchange. The latest version of the official app of Huobi.com is installed. The official website of Huobi Exchange is the entrance address of the official website of Huobi Exchange. Aug 01, 2025 pm 09:57 PM

Huobi is a world-renowned digital asset service platform, providing users with a wide range of digital asset trading and management services. With its professional service, rich transaction pairs and reliable security system, it has won the trust of many users.

Ouyi Exchange APP Android version v6.132.0 Ouyi APP official website download and installation guide 2025 Ouyi Exchange APP Android version v6.132.0 Ouyi APP official website download and installation guide 2025 Aug 04, 2025 pm 11:18 PM

OKX is a world-renowned comprehensive digital asset service platform, providing users with diversified products and services including spot, contracts, options, etc. With its smooth operation experience and powerful function integration, its official APP has become a common tool for many digital asset users.

Binance official app download latest link Binance exchange app installation portal Binance official app download latest link Binance exchange app installation portal Aug 04, 2025 pm 11:24 PM

Binance is a world-renowned digital asset trading platform, providing users with secure, stable and rich cryptocurrency trading services. Its app is simple to design and powerful, supporting a variety of transaction types and asset management tools.

Toncoin price 24-hour trend app View TON Coin latest k-line trend chart Toncoin price 24-hour trend app View TON Coin latest k-line trend chart Aug 01, 2025 pm 09:36 PM

Toncoin (TON) is a native cryptocurrency of The Open Network, attracting much attention in the digital currency field for its high transaction processing speed and scalability.

See all articles