@cf-wasm/plugins

中文文档
0.2.0发布于 2026/08/01MITTypeScriptES Module构建来源已验证近 30 天下载 1.2万

Plugins for cf-wasm

原始使用文档
文档目录7 节
  1. Cloudflare Modules Vite
  2. Why You Need It
  3. Installation
  4. Usage
  5. Example: SvelteKit + Cloudflare Adapter
  6. Supported Extensions
  7. How It Works

@cf-wasm/plugins

Plugins to make Cloudflare Workers compatible modules work seamlessly with modern build tools such as Vite.

Cloudflare Modules Vite

The Vite plugin exported from the @cf-wasm/plugins/vite-additional-modules submodule enables proper resolution of ESM-style imports that are natively supported on Cloudflare Workers, such as:

  • .wasm?module
  • .wasm
  • .txt
  • .bin

These import types typically fail in standard development environments since they rely on Cloudflare’s module loader behavior. This plugin bridges that gap by emulating Cloudflare’s module import system during development and build time.

Why You Need It

When using frameworks or adapters that target Cloudflare Workers (for example, @sveltejs/adapter-cloudflare in a SvelteKit app), you might encounter build-time errors like:

error during build:
[vite:wasm-fallback] Could not load /home/user/svelte-kit-app/src/lib/resvg.wasm (imported by src/routes/resvg/+server.ts): "ESM integration proposal for Wasm" is not supported currently. Use vite-plugin-wasm or other community plugins to handle this. Alternatively, you can use `.wasm?init` or `.wasm?url`. See https://vite.dev/guide/features.html#webassembly for more details.

This happens because Vite (and most bundlers) do not natively understand Cloudflare’s special module query syntax.

The vite-additional-modules plugin solves this problem by teaching Vite how to handle these imports.

Installation

npm install @cf-wasm/plugins --save-dev

Usage

Add the plugin to your vite.config.js (or vite.config.ts):

// vite.config.{ts|js}
import additionalModules from "@cf-wasm/plugins/vite-additional-modules";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [additionalModules()],
});

[!NOTE] If a dependency is using Cloudflare supported ESM imports syntax (for example, @cf-wasm/satori), you need to ensure Vite does not externalize @cf-wasm/satori modules during SSR bundling:

// vite.config.{ts|js}
import additionalModules from "@cf-wasm/plugins/vite-additional-modules";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [additionalModules()],
  ssr: {
    noExternal: [/^@cf-wasm\/.*/],
  },
});

Example: SvelteKit + Cloudflare Adapter

If you are using @sveltejs/adapter-cloudflare, simply include this plugin in your Vite configuration:

// vite.config.{ts|js}
import additionalModules from "@cf-wasm/plugins/vite-additional-modules";
import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [sveltekit(), additionalModules()],
});
// svelte.config.js
import adapter from "@sveltejs/adapter-cloudflare";
import { vitePreprocess } from "@sveltejs/vite-plugin-svelte";

/** @type {import('@sveltejs/kit').Config} */
const config = {
  // Consult https://svelte.dev/docs/kit/integrations
  // for more information about preprocessors
  preprocess: vitePreprocess(),

  kit: {
    adapter: adapter(),
  },
};

export default config;

Now, you can safely use imports like:

// src/routes/api/+server.ts
import wasmModule from "$/lib/my-module.wasm?module";
import textData from "$/lib/my-schema.txt";

export async function GET() {
  //...
}

These will work correctly both in development and when deployed to Cloudflare Workers.

Supported Extensions

ExtensionDescriptionOutput Type
.wasm?moduleWebAssembly modulesWebAssembly.Module
.wasmWebAssembly modulesWebAssembly.Module
.txtPlain text filesstring
.binBinary filesArrayBuffer

How It Works

  • In development, the plugin intercepts Vite’s module resolution and simulates Cloudflare’s import behavior, compiling or serving the file as needed.
  • In production builds, the plugin ensures that these files are emitted correctly and referenced as standard ESM imports compatible with the Cloudflare's workerd runtime.

版本与发布记录

@cf-wasm/plugins 共发布 12 个版本,首次发布于 2025/11/06;过去 12 个月发布了 10 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.0latest2026/08/013 个月前
0.1.102026/05/046 个月前
0.1.92025/11/3011 个月前
0.1.82025/11/2911 个月前
0.1.72025/11/1811 个月前
0.1.62025/11/1611 个月前

关于 @cf-wasm/plugins 的常见问题

@cf-wasm/plugins 是什么?

Plugins for cf-wasm

如何安装 @cf-wasm/plugins?

在项目目录运行 npm install @cf-wasm/plugins,或使用 pnpm add / yarn add / bun add。

@cf-wasm/plugins 支持 TypeScript 吗?

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

@cf-wasm/plugins 支持 ESM 还是 CommonJS?

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

@cf-wasm/plugins 可以商用吗?

@cf-wasm/plugins 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cf-wasm/plugins 的下载量有多少?

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

@cf-wasm/plugins 还在维护吗?

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