DEBUG.WIKI
PanstarCloud:部署应用,接入 AI,云服务器与 AI 网关
晚安云;香港 CN2 ¥15/月
V.PS:让项目靠近用户,亚太、欧洲、北美 KVM VPS

解决 esbuild 报错 Transforming let or const to ES5 is not supported 目标降级修复

排查 使用 esbuild 快速编译现代前端工程时,配置 target: 'es5' 抛出 Error: Transforming const to the configured target environment (es5) is not supported,分析 esbuild 设计哲学与 Babel 协作分工。

问题现象

在构建配置中将输出目标设为旧版浏览器环境时,esbuild 编译直接报错中断:

error: Transforming const to the configured target environment ("es5") is not supported yet
    src/index.ts:12:0:
      12 │ const server = express();
         ╵ ~~~~~

触发根因

  1. esbuild 为了维持纳秒级的极致编译性能,官方明确放弃了对 ES5 的全面 AST 降级转换支持(特别是复杂的原型链模拟、let/const 块级作用域变量提升仿真等);
  2. esbuild 的编译目标(target)最低安全标准为 es2015(ES6);
  3. 企业级系统强制要求兼容古董浏览器时未配置二次降级工具链。

解决步骤

将 esbuild 的 target 明确设定为 es2015 或现代浏览器矩阵

修改构建参数:

require('esbuild').build({
  entryPoints: ['src/index.ts'],
  bundle: true,
  target: ['es2015', 'chrome58', 'firefox57'],
  outfile: 'dist/bundle.js',
});

若必须兼容低版本环境,采用 esbuild 做快速打包再通过 Babel 极速转译

利用双管道兼顾构建速度与古董浏览器兼容性。

验证确认

重新执行构建:

esbuild 在 80ms 内完成全量编译打包,无任何不支持语法报错,产物整洁高效。