deno bundle
deno bundle 当前是一个实验性子命令,可能会发生变化。deno bundle 会将你的模块及其所有依赖合并为一个
JavaScript 文件,底层使用 esbuild。它
适合将项目作为单个优化后的文件进行部署或分发,
但目前并不打算替代诸如 Vite 或
webpack 之类复杂或交互式的
构建工具。
基本用法 Jump to heading
deno bundle -o output.js main.ts
如果不使用 -o/--output,bundle 会写入标准输出。然后可以使用 Deno 或其他 JavaScript 运行时来运行该输出文件:
deno run output.js
常用选项 Jump to heading
使用 --platform 指定目标平台(默认为 deno,也可以是 browser),使用 --minify 缩减输出,并使用 --sourcemap 生成源映射:
deno bundle --platform=browser --minify --sourcemap -o dist/app.js main.ts
使用 --code-splitting 和输出目录将共享代码拆分为单独的 chunks:
deno bundle --code-splitting --outdir dist/ main.ts worker.ts
使用 --external 将依赖项排除在 bundle 之外:
deno bundle --external npm:sharp -o output.js main.ts
使用 --declaration 在 JS 输出旁生成 TypeScript 声明。Deno 会将每个入口点的类型打包为一个独立、自包含的 .d.ts 文件:
deno bundle main.ts --outdir dist --declaration
# 生成 dist/main.js 和 dist/main.d.ts
类型检查 Jump to heading
deno bundle 默认不会对你的代码进行类型检查。使用 --check 标志启用类型检查:
# bundle 时检查本地模块的类型
deno bundle --check -o output.js main.ts
# 也检查远程模块的类型
deno bundle --check=all -o output.js main.ts
你也可以使用 --no-check 显式跳过类型检查,而
--no-check=remote 仅忽略来自远程模块的诊断信息。
browser 字段 Jump to heading
当使用 --platform=browser 进行打包时,Deno 会遵循依赖项 package.json 中的 npm browser 字段,包括其对象形式。该对象将模块映射到浏览器特定的替代项:
{
"browser": {
"./server.js": "./client.js",
"crypto": false,
"foo": "./shims/foo.js"
}
}
- 相对路径键会将已解析的文件重映射为浏览器特定版本(
./server.js变为./client.js)。 - 裸标识符键会重映射导入(
foo变为./shims/foo.js)。在查找之前,会去掉前缀node:,因此import "node:crypto"会匹配"crypto"键。 - 值为
false时会排除该模块:打包器会替换为空的占位模块。
有关使用 Deno 的打包策略的更多信息,请参阅 打包 指南。
deno bundle [OPTIONS] [file]...Output a single JavaScript file with all dependencies.
deno bundle jsr:@std/http/file-server -o file-server.bundle.js
If no output file is given, the output is written to standard output:
deno bundle jsr:@std/http/file-server
Type checking options Jump to heading
--check<CHECK_TYPE>optionalEnable type-checking. This subcommand does not type-check by default; pass --check=all to also type-check remote modules. Alternatively, use the 'deno check' subcommand.
--no-check<NO_CHECK_TYPE>optionalSkip type-checking. If the value of "remote" is supplied, diagnostic errors from remote modules will be ignored.
Dependency management options Jump to heading
--frozen<BOOLEAN>optionalError out if lockfile is out of date.
Load import map file from local file or remote URL.
--lock<FILE>optionalCheck the specified lock file. (If value is not provided, defaults to "./deno.lock").
--no-lockDisable auto discovery of the lock file.
--no-npmDo not resolve npm modules.
--no-remoteDo not resolve remote modules.
--node-modules-dir<MODE>optionalSelects the node_modules directory mode for npm packages (not a path). One of: auto (create a local node_modules directory and install npm packages into it), manual (use the existing local node_modules directory, do not modify it), none (do not use a local node_modules directory; resolve npm packages from the global cache). Defaults to auto when the flag is passed without a value.
--node-modules-linker<MODE>Sets the linker mode for npm packages (isolated or hoisted).
--reload, -r<CACHE_BLOCKLIST>optionalReload source code cache (recompile TypeScript). With no value, reloads everything. Pass a comma-separated list of specifiers to reload only those modules; npm: reloads all npm modules; npm:chalk reloads a single npm module; jsr:@std/http/file-server,jsr:@std/assert/assert-equals reloads specific modules.
--vendor<vendor>optionalToggles local vendor folder usage for remote modules and a node_modules folder for npm packages.
Options Jump to heading
--allow-import, -I<IP_OR_HOSTNAME>optionalAllow importing from remote hosts. Optionally specify allowed IP addresses and host names, with ports as necessary. Default value: deno.land:443,jsr.io:443,esm.sh:443,raw.esm.sh:443,cdn.jsdelivr.net:443,raw.githubusercontent.com:443,gist.githubusercontent.com:443.
--allow-scripts<PACKAGE>optionalAllow running npm lifecycle scripts for the given packages
Note: Scripts will only be executed when using a node_modules directory (--node-modules-dir).
--cert<FILE>Load certificate authority from PEM encoded file.
--code-splittingEnable code splitting.
--conditions<conditions>Use this argument to specify custom conditions for npm package exports. You can also use DENO_CONDITIONS env var. .
Configure different aspects of deno including TypeScript, linting, and code formatting.
Typically the configuration file will be called deno.json or deno.jsonc and
automatically detected; in that case this flag is not necessary.
--declarationGenerate .d.ts declaration files alongside the bundle.
--deny-import<IP_OR_HOSTNAME>optionalDeny importing from remote hosts. Optionally specify denied IP addresses and host names, with ports as necessary.
--env-file<FILE>optionalLoad environment variables from local file Only the first environment variable with a given key is used. Existing process environment variables are not overwritten, so if variables with the same names already exist in the environment, their values will be preserved. Where multiple declarations for the same environment variable exist in your .env file, the first one encountered is applied. This is determined by the order of the files you pass as arguments.
--external<external>--format<format>--inline-imports<inline-imports>optionalWhether to inline imported modules into the importing file [default: true]
--keep-namesKeep function and class names.
--minifyMinify the output.
--minimum-dependency-age<minimum-dependency-age>(Unstable) The age in minutes, ISO-8601 duration or RFC3339 absolute timestamp (e.g. '120' for two hours, 'P2D' for two days, '2025-09-16' for cutoff date, '2025-09-16T12:00:00+00:00' for cutoff time, '0' to disable).
--no-configDisable automatic loading of the configuration file.
--outdir<outdir>Output directory for bundled files.
--output, -o<output>Output path`.
--packages<packages>How to handle packages. Accepted values are 'bundle' or 'external'.
--platform<platform>Platform to bundle for. Accepted values are 'browser' or 'deno'.
--sourcemap<sourcemap>optionalGenerate source map. Accepted values are 'linked', 'inline', or 'external'.
--watchWatch and rebuild on changes.
Last updated on