From 52cf509335e2f42a6277f7a97eebb2c3e9b6c0a4 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 18 Jan 2024 18:04:01 -0500 Subject: [PATCH 1/2] feat: support compatible node modules without prefixes --- .changeset/old-ravens-learn.md | 6 ++++ packages/adapter-cloudflare-workers/index.js | 29 ++++++++++-------- packages/adapter-cloudflare/index.js | 32 +++++++++++--------- 3 files changed, 39 insertions(+), 28 deletions(-) create mode 100644 .changeset/old-ravens-learn.md 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..05fee094b41b 100644 --- a/packages/adapter-cloudflare-workers/index.js +++ b/packages/adapter-cloudflare-workers/index.js @@ -15,6 +15,20 @@ import { fileURLToPath } from 'node:url'; * }} WranglerConfig */ +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 +78,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 +90,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..890cdd380396 100644 --- a/packages/adapter-cloudflare/index.js +++ b/packages/adapter-cloudflare/index.js @@ -3,6 +3,20 @@ import * as path from 'node:path'; import { fileURLToPath } from 'node:url'; import * as esbuild from 'esbuild'; +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 +67,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 +82,8 @@ export default function (options = {}) { loader: { '.wasm': 'copy' }, - external + external, + alias: Object.fromEntries(compatible_node_modules.map((id) => [id, `node:${id}`])) }); } }; From 55a7dc8405482e18b861d849a15349fb5f14c857 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 18 Jan 2024 18:19:05 -0500 Subject: [PATCH 2/2] Apply suggestions from code review --- packages/adapter-cloudflare-workers/index.js | 1 + packages/adapter-cloudflare/index.js | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/adapter-cloudflare-workers/index.js b/packages/adapter-cloudflare-workers/index.js index 05fee094b41b..53b3e09795fc 100644 --- a/packages/adapter-cloudflare-workers/index.js +++ b/packages/adapter-cloudflare-workers/index.js @@ -15,6 +15,7 @@ import { fileURLToPath } from 'node:url'; * }} WranglerConfig */ +// list from https://developers.cloudflare.com/workers/runtime-apis/nodejs/ const compatible_node_modules = [ 'assert', 'async_hooks', diff --git a/packages/adapter-cloudflare/index.js b/packages/adapter-cloudflare/index.js index 890cdd380396..0acb132d4f82 100644 --- a/packages/adapter-cloudflare/index.js +++ b/packages/adapter-cloudflare/index.js @@ -3,6 +3,7 @@ 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',