Skip to content

Commit db7b501

Browse files
committed
fix(dev): alias debug -> obug shim so astro dev runs on cloudflare v13
astro dev 500s on every request with "module is not defined" when the @cloudflare/vite-plugin runner loads a component whose dep graph includes the CJS `debug` package: it references `module.exports`, and `module` is not a global in the workerd runner. Astro core already replaced `debug` with `obug` (ESM fork) in withastro/astro#15565, but integrations like expressive-code still pull `debug` via their markdown/rehype pipelines (expressive-code/expressive-code#439 tracks the same error). `obug` only exposes named exports, so a raw alias breaks consumers that do `import debug from "debug"`. Add `src/shims/debug.js` which re-exports obug and re-exposes a default, then wire it in via vite.resolve.alias. Install obug as a direct dependency so the shim resolves in the Workers runner (it's only a transitive dep of astro otherwise). Also add .wrangler to .gitignore - the new dev mode writes local state there. Verified: all routes (/, /blog/, /posts/..., /about/, /archive/, /projects/) return 200 under pnpm dev; pnpm check and pnpm build stay green.
1 parent bc7cfa0 commit db7b501

5 files changed

Lines changed: 29 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,6 @@ yarn.lock
3333

3434
# generated resume PDFs (see scripts/generate-resume.ts)
3535
public/resume/*.pdf
36+
37+
# wrangler configuration
38+
.wrangler

astro.config.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ export default defineConfig({
157157

158158
vite: {
159159
plugins: [tailwindcss()],
160+
resolve: {
161+
alias: {
162+
// Workaround for "module is not defined" in astro dev under
163+
// @cloudflare/vite-plugin. The CJS `debug` package references
164+
// `module.exports`, which fails in the Workers runner. The
165+
// shim forwards to `obug` (ESM fork) and re-exposes a default
166+
// export so consumers that do `import debug from "debug"`
167+
// keep working. See withastro/astro#15565 and
168+
// expressive-code/expressive-code#439.
169+
debug: new URL("./src/shims/debug.js", import.meta.url).pathname,
170+
},
171+
},
160172
build: {
161173
rollupOptions: {
162174
onwarn(warning, warn) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"katex": "^0.16.37",
4545
"markdown-it": "^14.1.1",
4646
"mdast-util-to-string": "^4.0.0",
47+
"obug": "^2.1.1",
4748
"overlayscrollbars": "^2.14.0",
4849
"pagefind": "^1.4.0",
4950
"photoswipe": "^5.4.4",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/shims/debug.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Drop-in ESM shim for the CJS `debug` package. The real `debug`
2+
// references `module.exports`, which throws "module is not defined"
3+
// when astro dev runs under @cloudflare/vite-plugin's workerd runner.
4+
// `obug` is a fork that is ESM-native but exports only named bindings;
5+
// most consumers do `import debug from "debug"` or `require("debug")`
6+
// and then call `debug("ns")`, so we re-expose a compatible default.
7+
import { createDebug, disable, enable, enabled, namespaces } from "obug";
8+
9+
export default createDebug;
10+
export { createDebug, disable, enable, enabled, namespaces };

0 commit comments

Comments
 (0)