Skip to content

Commit

Permalink
feat: support compatible node modules without prefixes (#11672)
Browse files Browse the repository at this point in the history
* feat: support compatible node modules without prefixes

* Apply suggestions from code review

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
  • Loading branch information
Rich-Harris and Rich-Harris authored Jan 18, 2024
1 parent 040f826 commit 7d3cccd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
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

0 comments on commit 7d3cccd

Please sign in to comment.