Skip to content

Commit

Permalink
fix: hybrid nodejs compat now supports requiring the default export o…
Browse files Browse the repository at this point in the history
…f a CJS module (#6039)

Fixes [#6028](#6028)
  • Loading branch information
petebacondarwin committed Jun 14, 2024
1 parent a534cbc commit dc597a3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/afraid-mails-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: hybrid nodejs compat now supports requiring the default export of a CJS module

Fixes [#6028](https://github.com/cloudflare/workers-sdk/issues/6028)
4 changes: 4 additions & 0 deletions fixtures/nodejs-hybrid-app/src/dep.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const Stream = require("stream");

const s = new Stream();
module.exports.s = s;
4 changes: 4 additions & 0 deletions fixtures/nodejs-hybrid-app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// node:assert/strict is currently an unenv alias to node:assert
// this is not very common, but happens and we need to support it
import assert from "node:assert/strict";
import { Stream } from "node:stream";
import { Client } from "pg";
import { s } from "./dep.cjs";

assert(s instanceof Stream, "expected s to be an instance of Stream");

assert(true, "the world is broken");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { builtinModules } from "node:module";
import nodePath from "node:path";
import dedent from "ts-dedent";
import { cloudflare, env, nodeless } from "unenv";
import { getBasePath } from "../../paths";
import type { Plugin, PluginBuild } from "esbuild";
Expand Down Expand Up @@ -39,7 +40,9 @@ function handleRequireCallsToNodeJSBuiltins(build: PluginBuild) {
{ filter: /.*/, namespace: REQUIRED_NODE_BUILT_IN_NAMESPACE },
({ path }) => {
return {
contents: `export * from '${path}'`,
contents: dedent`
import libDefault from '${path}';
module.exports = libDefault;`,
loader: "js",
};
}
Expand Down

0 comments on commit dc597a3

Please sign in to comment.