File tree Expand file tree Collapse file tree 3 files changed +17
-2
lines changed Expand file tree Collapse file tree 3 files changed +17
-2
lines changed Original file line number Diff line number Diff line change 4
4
5
5
import type { Plugin , PluginBuild } from "esbuild" ;
6
6
import assert from "node:assert" ;
7
- import { builtinModules , createRequire } from "node:module" ;
7
+ import module , { createRequire } from "node:module" ;
8
8
import nodePath from "node:path" ;
9
9
import { dedent } from "../../util/dedent.js" ;
10
10
@@ -39,7 +39,18 @@ export async function nodeJsCompatPlugin(): Promise<Plugin> {
39
39
} ;
40
40
}
41
41
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
+ ) ;
43
54
44
55
/**
45
56
* If we are bundling a "Service Worker" formatted Worker, imports of external modules,
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ export default {
13
13
console . log ( crypto . randomBytes ( 10 ) ) ;
14
14
console . log ( crypto2 . randomBytes ( 10 ) ) ;
15
15
console . log ( logger ) ;
16
+ require ( "ws" ) ;
16
17
return new Response ( "Hello World!" ) ;
17
18
} ,
18
19
} ;
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ describe("Bundle Worker Test", () => {
24
24
format : "esm" , // Assuming bundle-handler.ts is ESM
25
25
url : true , // Enable workers.dev URL to test the worker
26
26
compatibilityFlags : [ "nodejs_compat" ] ,
27
+ adopt : true ,
27
28
} ) ;
28
29
29
30
await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
@@ -48,6 +49,7 @@ describe("Bundle Worker Test", () => {
48
49
format : "esm" , // Assuming bundle-handler.ts is ESM
49
50
url : true , // Enable workers.dev URL to test the worker
50
51
compatibilityFlags : [ "nodejs_als" ] ,
52
+ adopt : true ,
51
53
} ) ;
52
54
53
55
await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
@@ -73,6 +75,7 @@ describe("Bundle Worker Test", () => {
73
75
url : true ,
74
76
compatibilityDate : "2024-09-22" , // v1 mode (before Sept 23rd 2024)
75
77
compatibilityFlags : [ "nodejs_compat" ] ,
78
+ adopt : true ,
76
79
} ) ,
77
80
) . rejects . toThrow (
78
81
"You must set your compatibilty date >= 2024-09-23 when using 'nodejs_compat' compatibility flag" ,
You can’t perform that action at this time.
0 commit comments