On this page
deno transpile
deno transpile 命令会从 TypeScript、JSX 或 TSX 源文件生成 JavaScript。当你需要将普通的 .js 输出交付给不理解 TypeScript 的运行时,或传入期望 JavaScript 的构建步骤时,它会很有用。
对于大多数工作流,你不需要 deno transpile——deno run、deno test 和 deno serve 已经可以直接接受 .ts / .tsx 文件。以下情况可以使用它:
- 你正在向直接运行
node或浏览器打包器的使用者发布库。(对于 npm tarball,优先使用deno pack,它会将deno transpile与打包逻辑结合起来。) - 下游工具只理解
.js。 - 你希望将预编译的
.js检入构建产物,而不是在运行时即时转译。
用法 Jump to heading
deno transpile <files...> [flags]
<files> 是一个或多个源文件路径(glob 会由 shell 展开)。deno transpile 不会 递归扫描目录——请传入你希望转译的文件,也可以通过 shell glob 传入。
输出模式 Jump to heading
| 模式 | 效果 |
|---|---|
| 无输出标志 | 将转译后的输出写入标准输出。 |
-o <path>, --output <path> |
将输出写入单个文件。与 --outdir 冲突。 |
--outdir <dir> |
将每个输入文件写入 <dir>,并保持源代码目录结构。 |
# 打印到 stdout
deno transpile main.ts
# 写入单个文件
deno transpile main.ts -o dist/main.js
# 将多个文件转译到输出目录
deno transpile src/main.ts src/helpers.ts --outdir dist
# 使用 shell glob
deno transpile src/*.ts --outdir dist
输入 / 输出扩展名映射 Jump to heading
| 输入 | 输出 | 源映射 |
|---|---|---|
.ts |
.js |
.js.map |
.tsx |
.js |
.js.map |
.jsx |
.js |
.js.map |
.mts |
.mjs |
.mjs.map |
.cts |
.cjs |
.cjs.map |
JSX 转换、装饰器、目标版本以及其他发射设置都来自你的 deno.json(或 tsconfig.json)中的 compilerOptions,因此输出会与 deno run 的执行结果一致。有关完整列表,请参阅
TypeScript 编译器选项。
源映射 Jump to heading
使用 --source-map 并指定以下其中一种:
| 模式 | 效果 |
|---|---|
none |
(默认)不生成源映射 |
inline |
将源映射作为 data: 注释嵌入每个文件中 |
separate |
写入同级的 .js.map(或 .mjs.map / .cjs.map) |
deno transpile main.ts -o dist/main.js --source-map separate
类型声明 Jump to heading
使用 --declaration 输出 .d.ts 声明文件。声明文件由 tsc(使用内置的 TypeScript)生成,因此此标志会遵循你在 deno.json / tsconfig.json 中的 compilerOptions。
即使 JavaScript 输出将写入标准输出或单个 -o 文件,.d.ts 文件也总是会写入磁盘——如果未设置输出位置,则写在源文件旁边;如果提供了 --outdir,则写入其中。
deno transpile src/*.ts --outdir dist --declaration
与 tsc 的区别 Jump to heading
deno transpile 和 TypeScript 的 tsc 有所重叠,但它们并不能直接互相替代:
| 关注点 | deno transpile |
tsc |
|---|---|---|
| 类型检查 | 无——只负责输出。请单独使用 deno check。 |
默认进行完整类型检查。 |
.d.ts 生成 |
可以,使用 --declaration。委托给内置的 tsc。 |
可以。 |
| JSR / npm / 远程导入 | 可以解析。 | 不能解析。 |
| 配置来源 | deno.json(或 tsconfig.json)。 |
仅 tsconfig.json。 |
| 速度 | 基于 SWC 的快速输出。 | 较慢(包含类型检查)。 |
如果你希望暴露类型错误,请在转译前或转译后运行
deno check。deno transpile 会愉快地输出那些无法通过类型检查的代码。
注意事项 Jump to heading
- 不进行打包。 每个输入文件只会生成一个输出文件。导入会仅按扩展名重写(
./foo.ts→./foo.js),但生成的依赖图仍然需要能够解析这些导入的运行时。若要生成单文件输出,请使用deno bundle。 - 不会递归扫描目录。 传入目录不会产生任何效果——请显式传入文件(
src/**/*.ts)或通过 glob 传入。 - 源映射与 stdout。 当输出到标准输出时,
--source-map inline可以工作;separate则需要一个可在其旁边写入映射文件的输出路径。
另请参阅 Jump to heading
deno bundle— 生成单个打包后的 JavaScript 文件deno pack— 构建可发布到 npm 的 tarball (内部使用deno transpile)deno check— 仅进行类型检查,不输出代码- TypeScript 支持 — Deno 中 TypeScript 工作方式的概览
deno transpile [OPTIONS] [file]...Transpile TypeScript/JSX/TSX files to JavaScript.
deno transpile main.ts
Output to a specific file:
deno transpile main.ts -o main.js
Output to a directory:
deno transpile src/*.ts --outdir dist
With source maps:
deno transpile main.ts --source-map separate
Generate declaration files:
deno transpile main.ts -o out.js --declaration
Note: --declaration always writes .d.ts files to disk (next to the source or in --outdir).
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>optionalSets the node modules management mode for npm packages.
--node-modules-linker<MODE>Sets the linker mode for npm packages (isolated or hoisted).
--reload, -r<CACHE_BLOCKLIST>optionalReload source code cache (recompile TypeScript) no value Reload everything jsr:@std/http/file-server,jsr:@std/assert/assert-equals Reloads specific modules npm: Reload all npm modules npm:chalk Reload specific npm module.
--vendor<vendor>optionalToggles local vendor folder usage for remote modules and a node_modules folder for npm packages.
Options Jump to heading
--cert<FILE>Load certificate authority from PEM encoded file.
--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 (requires type-checking via tsc).
--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 transpiled files.
--output, -o<output>Output file path (for single file transpilation).
--source-map<source-map>Source map mode: none, inline, or separate.