feat(edge): a Cloudflare Workers target, and a doctor for the quiet failures
`matcha new --runtime edge` scaffolds a Worker: `nodejs_compat`, `edgeHandler` as the default export, and wrangler's `[assets]` serving `./public` so `/` is the same page as on the other three runtimes and `/zen` the same stream from the worker. `matcha run` is `wrangler dev`, which is real workerd, so edge stops being a deploy-only target. Runtime detection puts `wrangler.toml` ahead of `package.json`, since an edge project has both, and the graph resolves under node — `src/app.ts` imports core, never `edgeHandler`. Getting there turned up a core defect, filed as Green-Tea/core#93: the npm ESM build cannot load on workerd at all. tsup's `createRequire(import.meta.url)` banner runs at module scope and `import.meta.url` is undefined there, so `@green-tea/core/edge` throws before any of its own code runs — while `src/node-require.ts` is written precisely to make that lazy and survivable. core's own edge test cannot see it: it bundles `src/`, so `dist/` is the one artifact users install and the one nothing exercises. The scaffold installs from JSR, which publishes source with no banner and boots clean; the `.npmrc` says why and links the issue, and both it and the alias can go once it is fixed. `matcha doctor` reports the misconfigurations that fail confusingly rather than loudly: `experimentalDecorators` missing, which turns every `@Route` into a syntax error blaming the decorator; a core pin that cannot resolve or that does not match what is installed; an entry that does not export `app`; and `@Ws` or multipart routes whose lazy-loaded peer dependency is absent, which compile and then fail at the first request. Every check is a file read — no runtime spawned, nothing fetched — so it works before the project installs. It reads the installed version from the package's own manifest rather than core's exported `VERSION`, which is stale in the published artifact (Green-Tea/core#92) and would give the wrong answer to the one check that exists to catch a wrong version. The starter smoke and the CI matrix gained edge as a fourth runtime. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012x1UPw987zNSxjH2DxHy2C
This commit is contained in:
18 files changed
+606
-13
No files matched your search
@@ -39,6 +39,7 @@ matcha create controller Users # generate + auto-wire into @Module
|
||||
matcha add sse # add a capability to your controller
|
||||
matcha graph # draw the dependency graph
|
||||
matcha explain /users/:id # one route's chain, in execution order
|
||||
matcha doctor # check the project for confusing misconfigurations
|
||||
```
|
||||
|
||||
### The starter
|
||||
@@ -102,16 +103,45 @@ and wire pieces into the right `@Module` array or controller. Edits are
|
||||
idempotent and revert themselves if they would break the file's syntax. Pass
|
||||
`--check` to `create` to type-check with your project's runtime afterward.
|
||||
|
||||
### `matcha doctor`
|
||||
|
||||
The checks whose failures are confusing rather than loud: `experimentalDecorators`
|
||||
missing (every `@Route` becomes a syntax error, and the message blames the
|
||||
decorator), a core pin that cannot resolve, an installed version that does not
|
||||
match the manifest, an entry that does not export `app`, and `@Ws`/multipart
|
||||
routes whose optional peer dependency is not installed — core lazy-loads `ws`
|
||||
and `busboy`, so those compile and fail at the first request instead.
|
||||
|
||||
Every check is a file read: no runtime is spawned and nothing is fetched, so it
|
||||
works before the project installs. Exits non-zero on a `✗`.
|
||||
|
||||
## Runtime detection
|
||||
|
||||
`matcha run` picks the runtime by precedence, first match wins:
|
||||
|
||||
1. `matcha.toml` — `runtime = "node" | "deno" | "bun"`
|
||||
2. `deno.json` / `deno.jsonc` → **deno**
|
||||
3. `bun.lockb` / `bun.lock` → **bun**
|
||||
4. `package.json` → **node**
|
||||
1. `matcha.toml` — `runtime = "node" | "deno" | "bun" | "edge"`
|
||||
2. `wrangler.toml` / `wrangler.jsonc` → **edge**
|
||||
3. `deno.json` / `deno.jsonc` → **deno**
|
||||
4. `bun.lockb` / `bun.lock` → **bun**
|
||||
5. `package.json` → **node**
|
||||
|
||||
Edge is a deploy target, not a `matcha run` target.
|
||||
`wrangler.toml` is checked before `package.json` because an edge project has both.
|
||||
|
||||
### Edge
|
||||
|
||||
`matcha new my-api --runtime edge` scaffolds a Cloudflare Worker: `wrangler.toml`
|
||||
with `nodejs_compat`, `edgeHandler(app)` as the default export, and wrangler's
|
||||
`[assets]` serving `./public` — so `/` is the same page as on the other runtimes
|
||||
and `/zen` is the same stream, from the worker. `matcha run` is `wrangler dev`,
|
||||
which boots real workerd locally.
|
||||
|
||||
Two differences the edge cannot hide. `@Html('file')`, `static` and multipart
|
||||
uploads need a filesystem workerd does not have, and shutdown teardown never
|
||||
runs — an isolate is discarded, not closed. And core is installed from **JSR**
|
||||
here rather than npm: the npm ESM build carries a `createRequire(import.meta.url)`
|
||||
banner that throws at load on workerd. The scaffolded `.npmrc` says so and links
|
||||
[the issue](https://github.com/Expressive-Tea/green-tea/issues/93); both it and
|
||||
the alias can go once that is fixed.
|
||||
|
||||
## Development
|
||||
|
||||
|
||||
Reference in new issue
Block a user