@cloudflare/codemods

中文文档
0.7.5发布于 2026/11/09MIT OR Apache-2.0TypeScriptES Module零依赖构建来源已验证近 30 天下载 121.2万

Codemods for upgrading Cloudflare developer projects

原始使用文档

@cloudflare/codemods

Codemods for upgrading Cloudflare developer projects.

Run a codemod by name:

npx @cloudflare/codemods vitest:v3-to-v4

Available codemods:

  • wrangler-to-cf - migrate a Wrangler configuration to the new cf configuration format
  • vitest:v3-to-v4 — migrate @cloudflare/vitest-pool-workers configuration from Vitest v3 to v4
  • vitest:pool-workers-to-vitest-plugin — rename @cloudflare/vitest-pool-workers to @cloudflare/vitest-plugin v1

Use --dry-run to list changes without writing them, --cwd <path> to target another project, or repeat --files <glob> to restrict the files considered. When the target is inside a Git worktree, the worktree must be clean. Use --force to bypass this safety check.

To migrate a Wrangler project with the default Vite bundler:

npx @cloudflare/codemods wrangler-to-cf --cwd ./path/to/project

The CLI detects a single root wrangler.json, wrangler.jsonc, or wrangler.toml. Pass --config <path> to select another file, or pass --bundler wrangler to retain Wrangler-specific tooling configuration. For this codemod, --files filters the selected Wrangler config and skips the migration when it does not match.

Programmatic usage

Codemods can also be run from JavaScript or TypeScript:

import { runCodemod } from "@cloudflare/codemods";

const { changedFiles } = await runCodemod("vitest:v3-to-v4", {
    cwd: process.cwd(),
    dryRun: false,
});

runCodemod accepts the same codemod names and aliases as the CLI. Its context also accepts optional files and force fields corresponding to the CLI flags, and setting dryRun lists changed files without writing them. It returns a CodemodResult containing changedFiles and rejects if the codemod cannot be run. The CLI's clean-worktree check also applies to programmatic runs.

Migrate Wrangler config to cf

Pass the exact Wrangler config file to migrateWranglerToCf:

import { migrateWranglerToCf } from "@cloudflare/codemods";

const result = await migrateWranglerToCf("./wrangler.jsonc", {
    bundler: "vite",
});

The options object and all of its fields are optional. The defaults are the Vite bundler, writes enabled, dirty-worktree protection enabled, and automatic dependency installation enabled. The migration installs cf when missing, installs @cloudflare/vite-plugin@beta when Vite is selected and the plugin is missing or incompatible, and upgrades Wrangler when generated Wrangler tooling requires a newer version. The Vite plugin dependency remains set to the beta dist tag in package.json; package manager installation updates the project's lockfile. Project-managed Vite plugin specifiers such as workspace: and catalog: are preserved; incompatible or unverified versions require a manual update. Set installDependencies to false to prevent dependency changes; required dependencies are then returned as blocking follow-ups. The migration always creates cloudflare.config.ts; selecting the Wrangler bundler also creates wrangler.config.ts when Wrangler-specific tooling config exists. Existing target files are never overwritten. Inspect result.followUps when result.status is needs-intervention.

版本与发布记录

@cloudflare/codemods 共发布 18 个版本,首次发布于 2026/08/21;过去 12 个月发布了 18 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.7.5latest2026/11/09今天
0.7.42026/11/054 天前
0.7.32026/11/036 天前
0.7.22026/11/027 天前
0.7.12026/11/028 天前
0.7.02026/10/2912 天前

关于 @cloudflare/codemods 的常见问题

@cloudflare/codemods 是什么?

Codemods for upgrading Cloudflare developer projects

如何安装 @cloudflare/codemods?

在项目目录运行 npm install @cloudflare/codemods,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 cloudflare-codemods,可以用 npx cloudflare-codemods 直接运行。

@cloudflare/codemods 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cloudflare/codemods 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cloudflare/codemods 可以商用吗?

@cloudflare/codemods 使用 MIT OR Apache-2.0 许可。可在 MIT、Apache-2.0 中任选其一遵守。选择 MIT 时,宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cloudflare/codemods 的下载量有多少?

根据 npm 官方下载统计,@cloudflare/codemods 在 2026/10/10 – 2026/11/08 这 30 天内被下载 1,211,906 次,日均约 40,397 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cloudflare/codemods 还在维护吗?

最新版本 0.7.5 发布于 2026/11/09(今天);至今共发布 18 个版本,其中过去 12 个月发布了 18 个;首次发布于 2026/08/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。