You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

替换已弃用的deno bundle命令遇阻,求esbuild正确用法或替代方案

替代deno bundle的解决方案

一、调整esbuild参数匹配deno bundle行为

deno bundle默认输出支持顶层await的自包含模块,针对你的报错和输出问题,可使用以下esbuild命令:

./node_modules/.bin/esbuild --bundle lib/commands/abc/main.ts --outfile=mod.abc.ts --format=esm --platform=node --external:deno:*

参数说明:

  • --format=esm:ES模块格式支持顶层await,解决你遇到的iife格式报错问题
  • --platform=node:适配Node/Deno环境,正确处理内置模块导入
  • --external:deno:*:忽略Deno特有API的打包错误,避免esbuild因无法识别deno内置模块报错

如果仍存在导入内容未被打包的情况,检查代码中是否有远程URL导入(如https://xxx):esbuild默认不处理远程依赖,需先执行deno cache lib/commands/abc/main.ts将依赖下载到Deno缓存目录,再让esbuild解析本地缓存文件,或使用esbuild-plugin-deno-resolver插件处理远程导入。

二、官方推荐替代工具

1. 生成可执行文件(无需修改代码)

如果目标是生成可直接运行的文件,用deno官方替代命令deno compile:

deno compile --output mod.abc lib/commands/abc/main.ts

该命令会将代码和所有依赖打包为二进制可执行文件,完全兼容原Deno代码。

2. 用Rollup打包成TS/JS文件

若需生成源码文件,可结合Rollup及对应插件:

  1. 安装依赖:
npm install rollup @rollup/plugin-node-resolve @rollup/plugin-typescript typescript -D
  1. 创建rollup.config.js配置文件:
import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';

export default {
  input: 'lib/commands/abc/main.ts',
  output: {
    file: 'mod.abc.ts',
    format: 'es',
    sourcemap: false
  },
  plugins: [
    resolve({
      extensions: ['.ts'],
      preferBuiltins: false,
      modulePaths: ['./node_modules', './.deno/deps']
    }),
    typescript({ tsconfig: './tsconfig.json' })
  ]
};
  1. 执行打包:
npx rollup -c

内容的提问来源于stack exchange,提问作者Kim Yu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 22:32:06