Skip to content

Commit

Permalink
remove polyfill option and should_polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Dec 4, 2023
1 parent e29bbc2 commit 051ea09
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-penguins-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-node': major
---

breaking: remove polyfill option. fetch APIs will now always come from the platform being used. File and crypto APIs will be polyfilled if not available
9 changes: 1 addition & 8 deletions documentation/docs/25-build-and-deploy/40-adapter-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ export default {
// default options are shown
out: 'build',
precompress: false,
envPrefix: '',
polyfill: true
envPrefix: ''
})
}
};
Expand Down Expand Up @@ -161,12 +160,6 @@ MY_CUSTOM_ORIGIN=https://my.site \
node build
```

### polyfill

Controls whether your build will load polyfills for missing modules. It defaults to `true`, and should only be disabled when using Node 18.11 or greater.

Note: to use Node's built-in `crypto` global with Node 18 you will need to use the `--experimental-global-webcrypto` flag. This flag is not required with Node 20.

## Custom server

The adapter creates two files in your build directory — `index.js` and `handler.js`. Running `index.js` — e.g. `node build`, if you use the default build directory — will start a server on the configured port.
Expand Down
1 change: 0 additions & 1 deletion packages/adapter-node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ interface AdapterOptions {
out?: string;
precompress?: boolean;
envPrefix?: string;
polyfill?: boolean;
}

export default function plugin(options?: AdapterOptions): Adapter;
7 changes: 1 addition & 6 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const files = fileURLToPath(new URL('./files', import.meta.url).href);

/** @type {import('./index.js').default} */
export default function (opts = {}) {
const { out = 'build', precompress, envPrefix = '', polyfill = true } = opts;
const { out = 'build', precompress, envPrefix = '' } = opts;

return {
name: '@sveltejs/adapter-node',
Expand Down Expand Up @@ -86,11 +86,6 @@ export default function (opts = {}) {
ENV_PREFIX: JSON.stringify(envPrefix)
}
});

// If polyfills aren't wanted then clear the file
if (!polyfill) {
writeFileSync(`${out}/shims.js`, '', 'utf-8');
}
}
};
}
5 changes: 1 addition & 4 deletions packages/kit/src/core/postbuild/analyse.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from '../../utils/exports.js';
import { load_config } from '../config/index.js';
import { forked } from '../../utils/fork.js';
import { should_polyfill } from '../../utils/platform.js';
import { installPolyfills } from '../../exports/node/polyfills.js';
import { resolvePath } from '../../exports/index.js';
import { ENDPOINT_METHODS } from '../../constants.js';
Expand All @@ -36,9 +35,7 @@ async function analyse({ manifest_path, env }) {
/** @type {import('types').ServerInternalModule} */
const internal = await import(pathToFileURL(`${server_root}/server/internal.js`).href);

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

// configure `import { building } from '$app/environment'` —
// essential we do this before analysing the code
Expand Down
5 changes: 1 addition & 4 deletions packages/kit/src/core/postbuild/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { dirname, join } from 'node:path';
import { pathToFileURL } from 'node:url';
import { installPolyfills } from '../../exports/node/polyfills.js';
import { mkdirp, posixify, walk } from '../../utils/filesystem.js';
import { should_polyfill } from '../../utils/platform.js';
import { decode_uri, is_root_relative, resolve } from '../../utils/url.js';
import { escape_html_attr } from '../../utils/escape.js';
import { logger } from '../utils.js';
Expand Down Expand Up @@ -94,9 +93,7 @@ async function prerender({ out, manifest_path, metadata, verbose, env }) {
/** @type {import('types').Logger} */
const log = logger({ verbose });

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

/** @type {Map<string, string>} */
const saved = new Map();
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/exports/node/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const globals = {
*/
export function installPolyfills() {
for (const name in globals) {
if (name in globalThis) continue;

Object.defineProperty(globalThis, name, {
enumerable: true,
configurable: true,
Expand Down
5 changes: 1 addition & 4 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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 } from '../../../core/config/index.js';
import { SVELTE_KIT_ASSETS } from '../../../constants.js';
import * as sync from '../../../core/sync/sync.js';
Expand All @@ -26,9 +25,7 @@ const cwd = process.cwd();
* @return {Promise<Promise<() => void>>}
*/
export async function dev(vite, vite_config, svelte_config) {
if (should_polyfill) {
installPolyfills();
}
installPolyfills();

const fetch = globalThis.fetch;
globalThis.fetch = (info, init) => {
Expand Down
5 changes: 1 addition & 4 deletions packages/kit/src/exports/vite/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ 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 { should_polyfill } from '../../../utils/platform.js';

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

const { paths } = svelte_config.kit;
const assets = paths.assets ? SVELTE_KIT_ASSETS : paths.base;
Expand Down
1 change: 0 additions & 1 deletion packages/kit/src/utils/platform.js

This file was deleted.

0 comments on commit 051ea09

Please sign in to comment.