Skip to content

Commit 7d3cccd

Browse files
authored
feat: support compatible node modules without prefixes (#11672)
* feat: support compatible node modules without prefixes * Apply suggestions from code review --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 040f826 commit 7d3cccd

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

.changeset/old-ravens-learn.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@sveltejs/adapter-cloudflare-workers': minor
3+
'@sveltejs/adapter-cloudflare': minor
4+
---
5+
6+
feat: support compatible node modules without prefixes

packages/adapter-cloudflare-workers/index.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ import { fileURLToPath } from 'node:url';
1515
* }} WranglerConfig
1616
*/
1717

18+
// list from https://developers.cloudflare.com/workers/runtime-apis/nodejs/
19+
const compatible_node_modules = [
20+
'assert',
21+
'async_hooks',
22+
'buffer',
23+
'crypto',
24+
'diagnostics_channel',
25+
'events',
26+
'path',
27+
'process',
28+
'stream',
29+
'string_decoder',
30+
'util'
31+
];
32+
1833
/** @type {import('./index.js').default} */
1934
export default function ({ config = 'wrangler.toml' } = {}) {
2035
return {
@@ -64,19 +79,7 @@ export default function ({ config = 'wrangler.toml' } = {}) {
6479

6580
const external = ['__STATIC_CONTENT_MANIFEST', 'cloudflare:*'];
6681
if (compatibility_flags && compatibility_flags.includes('nodejs_compat')) {
67-
external.push(
68-
'node:assert',
69-
'node:async_hooks',
70-
'node:buffer',
71-
'node:crypto',
72-
'node:diagnostics_channel',
73-
'node:events',
74-
'node:path',
75-
'node:process',
76-
'node:stream',
77-
'node:string_decoder',
78-
'node:util'
79-
);
82+
external.push(...compatible_node_modules.map((id) => `node:${id}`));
8083
}
8184

8285
await esbuild.build({
@@ -88,6 +91,7 @@ export default function ({ config = 'wrangler.toml' } = {}) {
8891
outfile: main,
8992
bundle: true,
9093
external,
94+
alias: Object.fromEntries(compatible_node_modules.map((id) => [id, `node:${id}`])),
9195
format: 'esm',
9296
loader: {
9397
'.wasm': 'copy'

packages/adapter-cloudflare/index.js

+18-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ import * as path from 'node:path';
33
import { fileURLToPath } from 'node:url';
44
import * as esbuild from 'esbuild';
55

6+
// list from https://developers.cloudflare.com/workers/runtime-apis/nodejs/
7+
const compatible_node_modules = [
8+
'assert',
9+
'async_hooks',
10+
'buffer',
11+
'crypto',
12+
'diagnostics_channel',
13+
'events',
14+
'path',
15+
'process',
16+
'stream',
17+
'string_decoder',
18+
'util'
19+
];
20+
621
/** @type {import('./index.js').default} */
722
export default function (options = {}) {
823
return {
@@ -53,20 +68,7 @@ export default function (options = {}) {
5368
}
5469
});
5570

56-
const external = [
57-
'cloudflare:*',
58-
'node:assert',
59-
'node:async_hooks',
60-
'node:buffer',
61-
'node:crypto',
62-
'node:diagnostics_channel',
63-
'node:events',
64-
'node:path',
65-
'node:process',
66-
'node:stream',
67-
'node:string_decoder',
68-
'node:util'
69-
];
71+
const external = ['cloudflare:*', ...compatible_node_modules.map((id) => `node:${id}`)];
7072

7173
await esbuild.build({
7274
platform: 'browser',
@@ -81,7 +83,8 @@ export default function (options = {}) {
8183
loader: {
8284
'.wasm': 'copy'
8385
},
84-
external
86+
external,
87+
alias: Object.fromEntries(compatible_node_modules.map((id) => [id, `node:${id}`]))
8588
});
8689
}
8790
};

0 commit comments

Comments
 (0)