Why is ES6 code not translated to ES5 when Vite builds JS library?
Apr 04, 2025 am 11:30 AMVite 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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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.

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.

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

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.

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.

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