React / 09
Browser targets and browserslist
Tell your build tools which browsers you support, and they rewrite modern syntax only as far back as needed. Missing features still need polyfills.
Same source, different output
your source
const count = order?.items?.length ?? 0;target · modern browsers
const count=order?.items?.length??0;kept as is (just minified)
target · 2015-era browsers (ES2015)
var _a, _b; const count = (_b = (_a = order === null || order === void 0 ? void 0 : order.items) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;rewritten into older syntax: much bigger
The bundler compares each piece of syntax with your targets. If every target supports it, the code stays as written. If not, it's rewritten into older syntax that does the same thing.
browserslist: one list for many tools
browserslist is a shared format for "these are my browsers". Put it in package.json:
{
"browserslist": "> 0.5%, last 2 versions, not dead"
}| Query | Means |
|---|---|
defaults | > 0.5%, last 2 versions, Firefox ESR, not dead. A sensible start |
> 0.5% | Every browser version with more than 0.5% global usage |
last 2 versions | The two newest versions of each browser |
not dead | Drop browsers with no official updates for 24 months (like IE 11) |
Run npx browserslist in the project to see exactly which browsers your query picks.
Who reads it: Parcel (for JS and CSS), Babel's preset-env, Autoprefixer and other PostCSS tools, and Next.js. One list, and they all agree.
Vite uses build.target instead
Vite doesn't read browserslist for its build. It has its own setting:
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
build: {
target: "baseline-widely-available", // the default
},
});The default, baseline-widely-available, means browsers that support features that have been widely available for a while. In Vite 8 that's Chrome 111, Edge 111, Firefox 114, Safari 16.4. You can pass your own list instead, like ["es2020", "safari15"]. To support truly old browsers, the official @vitejs/plugin-legacy adds a second build with polyfills.
Transpiling isn't polyfilling
Don't
Assuming the bundler makes everything work
const byType = Object.groupBy(menu, (item) => item.type);This is new API, not new syntax. There's nothing to rewrite, so it ships unchanged, and crashes with Object.groupBy is not a function in Safari 16.4, a browser inside Vite's default targets.
Do
Check support, then polyfill or avoid
const byType = {};
for (const item of menu) {
(byType[item.type] ??= []).push(item);
}Use older APIs, or add a polyfill (a library like core-js, or Vite's legacy plugin) that defines the missing function.
- Transpiling rewrites syntax:
?.,??,class, arrow functions. - A polyfill adds missing functions at runtime:
Object.groupBy,structuredClone.
Check new APIs on caniuse.com or MDN's compatibility tables before relying on them.
Gotchas
Recall
Can you answer these?
0/4
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
