Skip to content

server: add workerd run - #8084

Draft
bartlomieju wants to merge 1 commit into
mainfrom
biwanczuk/workerd-run
Draft

bartlomieju wants to merge 1 commit into
mainfrom
biwanczuk/workerd-run

Conversation

@bartlomieju

Copy link
Copy Markdown
Collaborator

Split out of #8065, rebuilt on top of #8051.

workerd run <file> serves one JavaScript or TypeScript file as a Worker, with no config file:

workerd run worker.ts
workerd run worker.js --port 9000 --watch
workerd run worker.ts --compat-date 2026-01-01 --compat-flag nodejs_compat
  • Bundling: the file is served as a wrangler config whose only field is main. So it is bundled with esbuild exactly as serve wrangler.jsonc bundles it, using WORKERD_ESBUILD, else the nearest node_modules/.bin/esbuild, else esbuild on PATH. TypeScript, relative imports and npm packages work, and --watch reloads when any input to the bundle changes.
  • Defaults: the Worker gets the newest compatibility date this build supports, the same date as workerd bench --compat-date=latest, and listens on localhost:8787, as a wrangler config does.
  • Options: --port, --compat-date and --compat-flag change those defaults. The rest of serve's options apply, including --socket-addr.
  • Errors: a file that isn't JS/TS, or that doesn't exist, is a usage error. A failed bundle is a config error, so --watch waits for a fix.

Implementation: wrangler::load() is split into parsing and build(), which load_file() reuses with a synthesized config. The socket address is now a parameter. The esbuild-not-found message no longer assumes a wrangler config.

Out of scope here: Python entry modules, which esbuild can't bundle and so need their own path, and --print-config.

Tests: unit tests for the argument parsing, the accepted extensions, and the config run builds, using a stub esbuild in node_modules/.bin. Checked by hand with esbuild 0.28: a .ts entry with a relative import serves, and an edit to the import reloads under --watch.

`workerd run <file>` serves one JavaScript or TypeScript file as a
Worker with no config file. It is served as a wrangler config whose
only field is `main`: the file is bundled with esbuild, found the
same way (WORKERD_ESBUILD, else the nearest node_modules/.bin/esbuild,
else PATH), so TypeScript, relative imports and npm packages work,
and --watch reloads on any input to the bundle.

The Worker gets the newest compatibility date this build supports
and listens on localhost:8787, as a wrangler config does. --port,
--compat-date and --compat-flag change them, and the rest of
`serve`'s options apply.

wrangler.rs's load() is split into parsing and build(), which
load_file() reuses with a synthesized config and the socket address
as a parameter. The esbuild-not-found message no longer assumes a
wrangler config.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 87 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.15%. Comparing base (c564d8a) to head (1565d76).

Files with missing lines Patch % Lines
src/workerd/server/cli/lib.rs 0.00% 46 Missing ⚠️
src/workerd/server/cli/wrangler.rs 0.00% 41 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8084      +/-   ##
==========================================
- Coverage   68.18%   68.15%   -0.03%     
==========================================
  Files         712      712              
  Lines      185743   185821      +78     
  Branches    29255    29255              
==========================================
+ Hits       126645   126647       +2     
- Misses      42019    42096      +77     
+ Partials    17079    17078       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants