Skip to content

Commit

Permalink
Mark node: as external for Node.js compatibility (#10544)
Browse files Browse the repository at this point in the history
* make `cloudflare:email` as external

* make cloudflare:email as external

* add changeset

* format

* fix changeset

* add for adapter-cloudflare-worker, too

* add changeset for adapter-cloudflare-worker

* Update .changeset/eighty-fans-rescue.md

Co-authored-by: Willow (GHOST) <ghostdevbusiness@gmail.com>

* Update .changeset/friendly-dragons-hide.md

Co-authored-by: Willow (GHOST) <ghostdevbusiness@gmail.com>

* Update .changeset/eighty-fans-rescue.md

* Update .changeset/friendly-dragons-hide.md

* Update packages/adapter-cloudflare/index.js

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>

* Update index.js

* Update .changeset/friendly-dragons-hide.md

* Update .changeset/eighty-fans-rescue.md

* mark `node:` as external

* remove old changesets

* options.nodeCompat

* add docs

* Update documentation/docs/25-build-and-deploy/70-adapter-cloudflare-workers.md

* switch it to just the list of APIs that Cloudflare supports with their node compatibility

* read `nodejs_compat` flag from the wrangler.toml to avoid making the user specify it twice

* changeset

* remove `nodeCompat` option, always assume compatibility

* Apply suggestions from code review

* Update .changeset/pretty-geese-drum.md

---------

Co-authored-by: Willow (GHOST) <ghostdevbusiness@gmail.com>
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
Co-authored-by: Rich Harris <hello@rich-harris.dev>
  • Loading branch information
4 people authored Jan 18, 2024
1 parent da1f2d1 commit 040f826
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/pretty-geese-drum.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: Add Node.js compatibility
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ export default {
};
```

If you would like to enable [Node.js compatibility](https://developers.cloudflare.com/workers/runtime-apis/nodejs/#enable-nodejs-from-the-cloudflare-dashboard), you can add "nodejs_compat" flag to `wrangler.toml`:

```toml
/// file: wrangler.toml
compatibility_flags = [ "nodejs_compat" ]
```

## Bindings

The [`env`](https://developers.cloudflare.com/workers/runtime-apis/fetch-event#parameters) object contains your project's [bindings](https://developers.cloudflare.com/workers/platform/environment-variables/), which consist of KV/DO namespaces, etc. It is passed to SvelteKit via the `platform` property, along with `context` and `caches`, meaning that you can access it in hooks and endpoints:
Expand Down
6 changes: 5 additions & 1 deletion packages/adapter-cloudflare-workers/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Adapter } from '@sveltejs/kit';
import './ambient.js';

export default function plugin(options?: { config?: string }): Adapter;
export default function plugin(options?: AdapterOptions): Adapter;

export interface AdapterOptions {
config?: string;
}
22 changes: 20 additions & 2 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { fileURLToPath } from 'node:url';
* site: {
* bucket: string;
* }
* compatibility_flags?: string[];
* }} WranglerConfig
*/

Expand All @@ -20,7 +21,7 @@ export default function ({ config = 'wrangler.toml' } = {}) {
name: '@sveltejs/adapter-cloudflare-workers',

async adapt(builder) {
const { main, site } = validate_config(builder, config);
const { main, site, compatibility_flags } = validate_config(builder, config);

const files = fileURLToPath(new URL('./files', import.meta.url).href);
const tmp = builder.getBuildDirectory('cloudflare-workers-tmp');
Expand Down Expand Up @@ -61,6 +62,23 @@ export default function ({ config = 'wrangler.toml' } = {}) {
})};\n\nexport const prerendered = new Map(${JSON.stringify(prerendered_entries)});\n`
);

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'
);
}

await esbuild.build({
platform: 'browser',
conditions: ['worker', 'browser'],
Expand All @@ -69,7 +87,7 @@ export default function ({ config = 'wrangler.toml' } = {}) {
entryPoints: [`${tmp}/entry.js`],
outfile: main,
bundle: true,
external: ['__STATIC_CONTENT_MANIFEST', 'cloudflare:*'],
external,
format: 'esm',
loader: {
'.wasm': 'copy'
Expand Down
17 changes: 16 additions & 1 deletion packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ 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'
];

await esbuild.build({
platform: 'browser',
conditions: ['worker', 'browser'],
Expand All @@ -66,7 +81,7 @@ export default function (options = {}) {
loader: {
'.wasm': 'copy'
},
external: ['cloudflare:*']
external
});
}
};
Expand Down

0 comments on commit 040f826

Please sign in to comment.