8000 fix(cloudflare): do not polyfill non-node.js modules (#214) · sam-goodwin/alchemy@423e8ca · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 423e8ca

Browse files
authored
fix(cloudflare): do not polyfill non-node.js modules (#214)
1 parent c8fb357 commit 423e8ca

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

alchemy/src/cloudflare/bundle/nodejs-compat.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import type { Plugin, PluginBuild } from "esbuild";
66
import assert from "node:assert";
7-
import { builtinModules, createRequire } from "node:module";
7+
import module, { createRequire } from "node:module";
88
import nodePath from "node:path";
99
import { dedent } from "../../util/dedent.js";
1010

@@ -39,7 +39,18 @@ export async function nodeJsCompatPlugin(): Promise<Plugin> {
3939
};
4040
}
4141

42-
const NODEJS_MODULES_RE = new RegExp(`^(node:)?(${builtinModules.join("|")})$`);
42+
const NODEJS_MODULES_RE = new RegExp(
43+
`^(node:)?(${module.builtinModules
44+
.filter(
45+
(m) =>
46+
![
47+
// in some runtimes (like bun), `ws` is a built-in module but is not in `node`
48+
// bundling for `nodejs_compat` should not polyfill these modules
49+
"ws",
50+
].includes(m),
51+
)
52+
.join("|")})$`,
53+
);
4354

4455
/**
4556
* If we are bundling a "Service Worker" formatted Worker, imports of external modules,

alchemy/test/cloudflare/bundle-handler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default {
1313
console.log(crypto.randomBytes(10));
1414
console.log(crypto2.randomBytes(10));
1515
console.log(logger);
16+
require("ws");
1617
return new Response("Hello World!");
1718
},
1819
};

alchemy/test/cloudflare/bundle.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe("Bundle Worker Test", () => {
2424
format: "esm", // Assuming bundle-handler.ts is ESM
2525
url: true, // Enable workers.dev URL to test the worker
2626
compatibilityFlags: ["nodejs_compat"],
27+
adopt: true,
2728
});
2829

2930
await new Promise((resolve) => setTimeout(resolve, 1000));
@@ -48,6 +49,7 @@ describe("Bundle Worker Test", () => {
4849
format: "esm", // Assuming bundle-handler.ts is ESM
4950
url: true, // Enable workers.dev URL to test the worker
5051
compatibilityFlags: ["nodejs_als"],
52+
adopt: true,
5153
});
5254

5355
await new Promise((resolve) => setTimeout(resolve, 1000));
@@ -73,6 +75,7 @@ describe("Bundle Worker Test", () => {
7375
url: true,
7476
compatibilityDate: "2024-09-22", // v1 mode (before Sept 23rd 2024)
7577
compatibilityFlags: ["nodejs_compat"],
78+
adopt: true,
7679
}),
7780
).rejects.toThrow(
7881
"You must set your compatibilty date >= 2024-09-23 when using 'nodejs_compat' compatibility flag",

0 commit comments

Comments
 (0)
0