Skip to content

Commit

Permalink
[fix] don't polyfill undici if using Deno or Bun (#8338)
Browse files Browse the repository at this point in the history
* [fix] conditionally polyfill but better to support Deno

* fix

* oops

* always polyfill on node

* simplify

* add todo

* blacklist and check outside polyfill function

* Update .changeset/wise-adults-pull.md

* un-negate

Co-authored-by: Rich Harris <hello@rich-harris.dev>
  • Loading branch information
benmccann and Rich-Harris authored Jan 5, 2023
1 parent e69f360 commit 3a23b96
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-adults-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] don't polyfill undici if using Deno or Bun
7 changes: 5 additions & 2 deletions packages/kit/src/core/prerender/prerender.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { readFileSync, writeFileSync } from 'fs';
import { dirname, join } from 'path';
import { pathToFileURL, URL } from 'url';
import { mkdirp, posixify, walk } from '../../utils/filesystem.js';
import { installPolyfills } from '../../exports/node/polyfills.js';
import { mkdirp, posixify, walk } from '../../utils/filesystem.js';
import { should_polyfill } from '../../utils/platform.js';
import { is_root_relative, resolve } from '../../utils/url.js';
import { queue } from './queue.js';
import { crawl } from './crawl.js';
Expand Down Expand Up @@ -89,7 +90,9 @@ export async function prerender() {
verbose: verbose === 'true'
});

installPolyfills();
if (should_polyfill) {
installPolyfills();
}

const server_root = join(config.outDir, 'output');

Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/exports/node/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const globals = {
};

// exported for dev/preview and node environments
// TODO: remove this once we only support Node 18.11+ (the version multipart/form-data was added)
export function installPolyfills() {
for (const name in globals) {
Object.defineProperty(globalThis, name, {
Expand Down
5 changes: 4 additions & 1 deletion packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getRequest, setResponse } from '../../../exports/node/index.js';
import { installPolyfills } from '../../../exports/node/polyfills.js';
import { coalesce_to_error } from '../../../utils/error.js';
import { posixify, resolve_entry, to_fs } from '../../../utils/filesystem.js';
import { should_polyfill } from '../../../utils/platform.js';
import { load_error_page, load_template } from '../../../core/config/index.js';
import { SVELTE_KIT_ASSETS } from '../../../constants.js';
import * as sync from '../../../core/sync/sync.js';
Expand All @@ -24,7 +25,9 @@ const cwd = process.cwd();
* @return {Promise<Promise<() => void>>}
*/
export async function dev(vite, vite_config, svelte_config) {
installPolyfills();
if (should_polyfill) {
installPolyfills();
}

sync.init(svelte_config, vite_config.mode);

Expand Down
7 changes: 5 additions & 2 deletions packages/kit/src/exports/vite/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import fs from 'fs';
import { join } from 'path';
import sirv from 'sirv';
import { pathToFileURL } from 'url';
import { loadEnv, normalizePath } from 'vite';
import { getRequest, setResponse } from '../../../exports/node/index.js';
import { installPolyfills } from '../../../exports/node/polyfills.js';
import { SVELTE_KIT_ASSETS } from '../../../constants.js';
import { loadEnv, normalizePath } from 'vite';
import { should_polyfill } from '../../../utils/platform.js';
import { not_found } from '../utils.js';

/** @typedef {import('http').IncomingMessage} Req */
Expand All @@ -21,7 +22,9 @@ import { not_found } from '../utils.js';
* @param {import('types').ValidatedConfig} svelte_config
*/
export async function preview(vite, vite_config, svelte_config) {
installPolyfills();
if (should_polyfill) {
installPolyfills();
}

const { paths } = svelte_config.kit;
const base = paths.base;
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/utils/platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const should_polyfill = typeof Deno === 'undefined' && typeof Bun === 'undefined';
2 changes: 2 additions & 0 deletions packages/kit/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,6 @@ declare global {
const __SVELTEKIT_BROWSER__: boolean;
const __SVELTEKIT_DEV__: boolean;
const __SVELTEKIT_EMBEDDED__: boolean;
var Bun: object;
var Deno: object;
}

0 comments on commit 3a23b96

Please sign in to comment.