Configure esbuild's target based on a browserslist query
yarn add esbuild-plugin-browserslist esbuild browserslist
import esbuild from "esbuild";
import browserslist from "browserslist";
import {
esbuildPluginBrowserslist,
resolveToEsbuildTarget,
} from "esbuild-plugin-browserslist";
await esbuild.build({
entryPoints: ["./foo/bar.ts"],
// ... other options (except `target`) ...
plugins: [
esbuildPluginBrowserslist(browserslist("defaults"), {
printUnknownTargets: false,
}),
],
});
// Or:
const target = resolveToEsbuildTarget(browserslist("defaults"), {
printUnknownTargets: false,
});
await esbuild.build({
entryPoints: ["./foo/bar.ts"],
target,
});
ℹ️ CJS usage instructions
Adjust the imports as follows:
const esbuild = require("esbuild");
const browserslist = require("browserslist");
const {
esbuildPluginBrowserslist,
resolveToEsbuildTarget,
} = require("esbuild-plugin-browserslist");
- Only
edge
,firefox
,chrome
,safari
,ios_saf
, andnode
have direct equivalents for esbuild targets. android
andand_chr
are mapped to thechrome
target, andand_ff
is mapped to thefirefox
target.- All other browsers are ignored (
and_qq
,samsung
,opera
,op_mini
,op_mob
,ie
,ie_mob
,bb
,baidu
, andkaios
)
You can turn on debug logs (which will print all resolutions or failures) using DEBUG=esbuild-plugin-browserslist