diff --git a/.changeset/old-ravens-learn.md b/.changeset/old-ravens-learn.md new file mode 100644 index 000000000000..1831ff0d695a --- /dev/null +++ b/.changeset/old-ravens-learn.md @@ -0,0 +1,6 @@ +--- +'@sveltejs/adapter-cloudflare-workers': minor +'@sveltejs/adapter-cloudflare': minor +--- + +feat: support compatible node modules without prefixes diff --git a/packages/adapter-cloudflare-workers/index.js b/packages/adapter-cloudflare-workers/index.js index c3bbb78126ea..53b3e09795fc 100644 --- a/packages/adapter-cloudflare-workers/index.js +++ b/packages/adapter-cloudflare-workers/index.js @@ -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 { @@ -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({ @@ -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' diff --git a/packages/adapter-cloudflare/index.js b/packages/adapter-cloudflare/index.js index 2e9bc47a2968..0acb132d4f82 100644 --- a/packages/adapter-cloudflare/index.js +++ b/packages/adapter-cloudflare/index.js @@ -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 { @@ -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', @@ -81,7 +83,8 @@ export default function (options = {}) { loader: { '.wasm': 'copy' }, - external + external, + alias: Object.fromEntries(compatible_node_modules.map((id) => [id, `node:${id}`])) }); } };