You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 70c275d
Browse filesBrowse the repository at this point in the historyBrowse files
authored
fix(deploy): bundle framework Workers with Cloudflare module rules and Worker conditions (#540)
Fixes#539
`prepareBundledBuildOutput` bundles framework Worker output again with a
plain Vite lib build. It now matches `@cloudflare/vite-plugin` in two
ways:
- **Additional modules.** A plugin applies the Cloudflare Vite plugin's
module rules: `.wasm` and `.wasm?module`, `.bin`, `.txt`, `.html` and
`.sql`, including `#` subpath imports that resolve to them. Matching
imports stay external, and each file is emitted beside the bundle with a
path relative to the chunk that imports it. `copyBundle` already uploads
these files as `wasm`, `data` and `text` modules. The Cloudflare plugin
does not export its rules, so the plugin repeats them.
- **Resolve conditions.** The build uses `WORKER_CONDITIONS`, which
Void's other Worker bundles already use. The constant moves from
`wrapper.ts` to its own module so that `cf-build-output.ts` still loads
Vite lazily.
Both changes apply to every bundle that `prepareBundledBuildOutput`
builds, including the static Worker.
## Tests
`packages/void/test/unit/cf-build-output.test.ts`:
- A framework Worker with wasm-bindgen-style and plain Wasm, text and
data imports, a `#` subpath import and a dynamic import produces `wasm`,
`text` and `data` modules and passes the `cf` dry run.
- A dependency with `workerd` and `browser` exports resolves to its
`workerd` build.
Each test fails when its change is removed. `vp run test`, `vp run
--filter ./packages/void typecheck`, `vp lint` and `vp fmt --check`
pass.
I also prepared the output of a SvelteKit app that uses
`@resvg/resvg-wasm` and micromark: `index_bg.wasm` is uploaded as a
`wasm` module, and the bundle contains the Worker build of
`decode-named-character-reference`.
## Follow-up changes (21e826b, 1cb2d9f)
```
import './a.txt' ─┐ ┌─> a.txt (text)
import './a.bin' ─┤ same bytes, before: ──> │ one file, one type ✗
│ same bytes, now: ──> └─> a.txt (text) + a.bin (data) ✓
import './sa"me.bin' before: ENOENT ✗ now: sa_me.bin ✓
esm-env DEV before: not folded now: folded to false ✓
```
- Each module file is emitted once per source path, with its own name
and extension. Files with the same bytes are no longer merged, so each
import keeps its module type.
- The external id holds a counter key instead of the path, and emitted
names keep only letters, digits, `.`, `_` and `-`. A quote in a path no
longer breaks the import.
- The build adds the `production` condition, like
`@cloudflare/vite-plugin` does for production builds. It is fixed rather
than taken from `NODE_ENV`, because a deploy bundle is always a
production build. Without it, the SvelteKit playground bundle lost the
production build of `esm-env`.
- The reference regex is replaced by a small `indexOf` loop. Each build
uses a random marker, so app strings that look like a module id stay as
they are.
Tests: the Wasm test adds same-bytes `.txt`/`.bin` files, a quoted file
name (`'` on Windows) and an app string that looks like the old marker;
the conditions test adds a package with `development`/`production`
exports.
Left out, since they behave the same before this PR (and in
`@cloudflare/vite-plugin` for the second):
- Packages without `exports` still prefer the `browser` main field.
- Wasm reached through an export subpath without a `.wasm` suffix is not
kept as a module.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: LongYinan <github@lyn.one>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
0 commit comments