Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] don't polyfill undici if using Deno or Bun #8338

Merged
merged 9 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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] conditionally polyfill but better to support Deno
benmccann marked this conversation as resolved.
Show resolved Hide resolved
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 { non_node_runtime } 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 (!non_node_runtime()) {
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 { non_node_runtime } 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 (!non_node_runtime()) {
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 { non_node_runtime } 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 (!non_node_runtime()) {
installPolyfills();
}

const { paths } = svelte_config.kit;
const base = paths.base;
Expand Down
3 changes: 3 additions & 0 deletions packages/kit/src/utils/platform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function non_node_runtime() {
return typeof globalThis.Deno !== 'undefined' || typeof globalThis.Bun !== 'undefined';
}
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
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;
}