Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/old-ravens-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sveltejs/adapter-cloudflare-workers': minor
'@sveltejs/adapter-cloudflare': minor
---

feat: support compatible node modules without prefixes
30 changes: 17 additions & 13 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ import { fileURLToPath } from 'node:url';
* }} WranglerConfig
*/

// list from https://developers.cloudflare.com/workers/runtime-apis/nodejs/
const compatible_node_modules = [
'assert',
'async_hooks',
'buffer',
'crypto',
'diagnostics_channel',
'events',
'path',
'process',
'stream',
'string_decoder',
'util'
];

/** @type {import('./index.js').default} */
export default function ({ config = 'wrangler.toml' } = {}) {
return {
Expand Down Expand Up @@ -64,19 +79,7 @@ export default function ({ config = 'wrangler.toml' } = {}) {

const external = ['__STATIC_CONTENT_MANIFEST', 'cloudflare:*'];
if (compatibility_flags && compatibility_flags.includes('nodejs_compat')) {
external.push(
'node:assert',
'node:async_hooks',
'node:buffer',
'node:crypto',
'node:diagnostics_channel',
'node:events',
'node:path',
'node:process',
'node:stream',
'node:string_decoder',
'node:util'
);
external.push(...compatible_node_modules.map((id) => `node:${id}`));
}

await esbuild.build({
Expand All @@ -88,6 +91,7 @@ export default function ({ config = 'wrangler.toml' } = {}) {
outfile: main,
bundle: true,
external,
alias: Object.fromEntries(compatible_node_modules.map((id) => [id, `node:${id}`])),
format: 'esm',
loader: {
'.wasm': 'copy'
Expand Down
33 changes: 18 additions & 15 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ import * as path from 'node:path';
import { fileURLToPath } from 'node:url';
import * as esbuild from 'esbuild';

// list from https://developers.cloudflare.com/workers/runtime-apis/nodejs/
const compatible_node_modules = [
'assert',
'async_hooks',
'buffer',
'crypto',
'diagnostics_channel',
'events',
'path',
'process',
'stream',
'string_decoder',
'util'
];

/** @type {import('./index.js').default} */
export default function (options = {}) {
return {
Expand Down Expand Up @@ -53,20 +68,7 @@ export default function (options = {}) {
}
});

const external = [
'cloudflare:*',
'node:assert',
'node:async_hooks',
'node:buffer',
'node:crypto',
'node:diagnostics_channel',
'node:events',
'node:path',
'node:process',
'node:stream',
'node:string_decoder',
'node:util'
];
const external = ['cloudflare:*', ...compatible_node_modules.map((id) => `node:${id}`)];

await esbuild.build({
platform: 'browser',
Expand All @@ -81,7 +83,8 @@ export default function (options = {}) {
loader: {
'.wasm': 'copy'
},
external
external,
alias: Object.fromEntries(compatible_node_modules.map((id) => [id, `node:${id}`]))
});
}
};
Expand Down