With Vite 8, when is lowering `build.target` enough for an older browser, and when do you need `@vitejs/plugin-legacy` instead?
answer
- a default fixed per major
- syntax lowered, APIs not polyfilled
- a floor no target can lower
- nomodule scripts and SystemJS chunks
- usage-based polyfills, ES features only
basics
~20 sLowering build.target is enough when the browser has native ES module dynamic import and import.meta and lacks only newer syntax, since it adds no polyfills. Below that floor you need @vitejs/plugin-legacy, which adds nomodule legacy chunks and a polyfill chunk.
solid answer
~50 sVite 8's default `build.target` is `'baseline-widely-available'`: Chrome and Edge 111, Firefox 114, Safari 16.4. You can lower it, for example to `['chrome87', 'firefox78']`, and Oxc rewrites newer syntax, warning about what it cannot lower safely; but Vite handles syntax only and adds no polyfills, and even at the lowest target, `es2015`, the output still needs native ES module dynamic import and `import.meta` (Chrome 64, Firefox 67, Safari 11.1, Edge 79). So a lower target suffices for a browser above that floor whose only gap is syntax. For a browser below it, `@vitejs/plugin-legacy` emits a legacy chunk per chunk, transformed by `@babel/preset-env` into SystemJS modules, a polyfill chunk chosen from `targets` and actual usage, and `<script nomodule>` tags so only browsers that need them load them; the same plugin can also add polyfills to the modern build.
go deeper
Recall that build.target lowers syntax only, that its Vite 8 default is the baseline-widely-available browser set, and that old browsers may need the legacy plugin.
Explain the ES-module floor no target can lower, what Oxc's lowering warnings mean, and how the legacy plugin's nomodule chunks reach only the browsers that need them.
Choose between a lower target and the legacy plugin for a real fleet: check the floor, handle DOM polyfills, CSP hashes and the terser question, and weigh doubled build output.
Set browser support as policy: which target the shared config pins, when a legacy build is justified by real traffic, and when to retire it.
## Two different gaps An older browser can fall short of a Vite build in two ways, and each has its own fix. - **Syntax it cannot parse**, such as newer operators or class features. **Lowering** rewrites that syntax into older forms, and that is what `build.target` controls. - **Features the bundle needs at runtime**, either the module loading Vite's output relies on, or built-in APIs the app calls. Rewriting syntax cannot add those; they need **polyfills** or a different kind of bundle. ## What `build.target` does in Vite 8 - **Default:** `'baseline-widely-available'`, which resolves to `['chrome111', 'edge111', 'firefox114', 'safari16.4', 'ios16.4']`, Baseline Widely Available as of 2026-01-01. The value is fixed for each major; Vite 8 raised it from Chrome 107, Firefox 104 and Safari 16.0. - **Other values:** `'esnext'` (minimal transpiling), an ES edition such as `'es2015'`, browser versions such as `'chrome87'`, or an array. Listing the same browser twice with different versions is an error in Vite 8. - **Engine:** the **Oxc** transformer lowers the code, and the build warns about features Oxc cannot lower safely. Oxc does not lower native decorators yet. - **CSS:** `build.cssTarget` defaults to `build.target`, so CSS minification by Lightning CSS targets the same browsers unless you set it separately. - **No polyfills.** The build guide says plainly that Vite handles syntax transforms only. ### The floor no target removes Vite's output is native ES modules that use dynamic `import()` and `import.meta`. Even with `build.target: 'es2015'`, the build still requires: | Browser | Minimum version | |---|---| | Chrome | 64 | | Firefox | 67 | | Safari | 11.1 | | Edge | 79 | A browser below this cannot run a Vite build, whatever the target says. ## What `@vitejs/plugin-legacy` adds By default the plugin: 1. Generates a **legacy chunk for every chunk**, transformed with `@babel/preset-env` and emitted as **SystemJS** modules, so code splitting still works. 2. Generates a **polyfill chunk** with the SystemJS runtime and the polyfills that the `targets` query and **actual usage** in the bundle call for. 3. Injects **`<script nomodule>`** tags, so browsers that ignore `type="module"` load the legacy bundle. A browser with native modules but without the plugin's widely-available features (dynamic import, async generators, `import.meta.resolve`) is caught by a runtime check and also loads it. 4. Sets **`import.meta.env.LEGACY`** to `true` only in the legacy bundle. `targets` defaults to `'last 2 versions and not dead, > 0.3%, Firefox ESR'`, or to your browserslist config if one exists. ### Limits worth knowing - **Usage detection covers ES language features only.** Missing DOM APIs go in `additionalLegacyPolyfills` or `additionalModernPolyfills`. - **No ES5 output.** Vite 8's migration notes list transforming to ES5 and below with the legacy plugin as unsupported. - **Inline scripts.** The plugin needs inline scripts, so a strict content security policy must allow the hashes the plugin exports as `cspHashes`. - **Minifier.** Terser is needed only with Vite older than 8.1.4, or when `build.minify` is `'terser'`; from Vite 8.1.4 the plugin prefers Oxc. - **Cost.** Every chunk is built twice, so builds get slower and `dist` gets larger. ## Checking the result - **Read the build output.** Oxc's warnings name the features it could not lower safely for your target; each one is code that may still fail on the old browser. - **Test on the real browser.** Emulating a user agent does not reproduce a missing parser feature or API; the locked-down desktop does. - **Branch on `import.meta.env.LEGACY`** only for genuinely different behaviour, such as skipping a feature the old browser cannot support. - **Watch the size.** With the legacy plugin, every chunk has a legacy twin plus a polyfill chunk, so compare `dist` before and after. ## Choosing for a locked-down desktop browser 1. **Above the floor, only syntax missing:** lower `build.target` to that browser, and check the build warnings. 2. **Above the floor, APIs missing too:** lower the target and add the polyfills, by hand or through the plugin's `modernPolyfills` with `renderLegacyChunks: false`. 3. **Below the floor:** add `@vitejs/plugin-legacy` with `targets` naming that browser; current browsers keep the modern bundle. Whether to ship one modern-only build or a second legacy one is a support-policy decision. These options are how Vite 8 implements it.
- The legacy build runs on the old desktops, but a call to a DOM API still throws there. Why did the plugin not polyfill it?Its usage-based detection, done through `@babel/preset-env`, only covers ES language features such as `Promise` or `Array.prototype.includes`. DOM and web-platform APIs are not detected, so you add them yourself with `additionalLegacyPolyfills` (or `additionalModernPolyfills` for the modern bundle), naming the polyfill imports to include.
- The portal sends a strict `Content-Security-Policy` without `'unsafe-inline'`. What does `@vitejs/plugin-legacy` need from it?The plugin injects small inline scripts: the Safari 10.1 `nomodule` fix, SystemJS initialisation and the dynamic-import fallback. Add their hashes to `script-src`. The plugin exports them as `cspHashes`, and the README advises generating the header from that export, because the values can change between minor versions.
- Does a Vite 8 project using the legacy plugin still need `terser` installed?Only if Vite is older than 8.1.4 or `build.minify` is explicitly `'terser'`. From Vite 8.1.4 the plugin prefers Oxc for minifying legacy chunks, and later releases fixed it so minification does not reintroduce newer syntax into legacy and polyfill chunks.
- Setting build.target to es2015 makes a Vite build run in any ES2015 browser
- build.target makes Vite inject polyfills for APIs like structuredClone
- Vite 8 still defaults build.target to Chrome 107 and Safari 16.0
- The legacy plugin serves the legacy bundle to every browser
- The legacy plugin always needs terser installed in Vite 8
- The legacy plugin's usage detection also polyfills DOM APIs
Assumes Vite 8, whose 'baseline-widely-available' target is Chrome/Edge 111, Firefox 114 and Safari 16.4 with Oxc lowering, and @vitejs/plugin-legacy 8.x, which prefers Oxc for minification from Vite 8.1.4.