From 2c764df0b9df7a64e9e31549a36b1fc1a91f7ec4 Mon Sep 17 00:00:00 2001 From: GHOST Date: Fri, 10 Feb 2023 18:52:20 +0000 Subject: [PATCH 1/3] add polyfill option --- .changeset/strange-sheep-grab.md | 5 +++++ packages/adapter-node/index.d.ts | 1 + packages/adapter-node/index.js | 10 ++++++++-- packages/adapter-node/rollup.config.js | 11 ++++++++++- packages/adapter-node/src/handler.js | 2 +- 5 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 .changeset/strange-sheep-grab.md diff --git a/.changeset/strange-sheep-grab.md b/.changeset/strange-sheep-grab.md new file mode 100644 index 000000000000..3d083ed64867 --- /dev/null +++ b/.changeset/strange-sheep-grab.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-node': minor +--- + +add polyfill option diff --git a/packages/adapter-node/index.d.ts b/packages/adapter-node/index.d.ts index d32510e25186..12ea6273dd66 100644 --- a/packages/adapter-node/index.d.ts +++ b/packages/adapter-node/index.d.ts @@ -9,6 +9,7 @@ interface AdapterOptions { out?: string; precompress?: boolean; envPrefix?: string; + polyfill?: boolean; } export default function plugin(options?: AdapterOptions): Adapter; diff --git a/packages/adapter-node/index.js b/packages/adapter-node/index.js index 85371fd9711a..607e3a81797a 100644 --- a/packages/adapter-node/index.js +++ b/packages/adapter-node/index.js @@ -9,7 +9,7 @@ const files = fileURLToPath(new URL('./files', import.meta.url).href); /** @type {import('.').default} */ export default function (opts = {}) { - const { out = 'build', precompress, envPrefix = '' } = opts; + const { out = 'build', precompress, envPrefix = '', polyfill = true } = opts; return { name: '@sveltejs/adapter-node', @@ -72,10 +72,16 @@ export default function (opts = {}) { ENV: './env.js', HANDLER: './handler.js', MANIFEST: './server/manifest.js', - SERVER: `./server/index.js`, + SERVER: './server/index.js', + SHIMS: './shims.js', ENV_PREFIX: JSON.stringify(envPrefix) } }); + + // If polyfills aren't wanted then clear the file + if (!polyfill) { + writeFileSync(`${out}/shims.js`, '', 'utf-8'); + } } }; } diff --git a/packages/adapter-node/rollup.config.js b/packages/adapter-node/rollup.config.js index 4c00ce5f1eb0..16c4a20c37a7 100644 --- a/packages/adapter-node/rollup.config.js +++ b/packages/adapter-node/rollup.config.js @@ -30,6 +30,15 @@ export default [ inlineDynamicImports: true }, plugins: [nodeResolve(), commonjs(), json()], - external: ['ENV', 'MANIFEST', 'SERVER', ...builtinModules] + external: ['ENV', 'MANIFEST', 'SERVER', 'SHIMS', ...builtinModules] + }, + { + input: 'src/shims.js', + output: { + file: 'files/shims.js', + format: 'esm' + }, + plugins: [nodeResolve(), commonjs()], + external: builtinModules } ]; diff --git a/packages/adapter-node/src/handler.js b/packages/adapter-node/src/handler.js index 331b6cccaff2..b7660db2030f 100644 --- a/packages/adapter-node/src/handler.js +++ b/packages/adapter-node/src/handler.js @@ -1,4 +1,4 @@ -import './shims'; +import 'SHIMS'; import fs from 'node:fs'; import path from 'node:path'; import sirv from 'sirv'; From 8ac6fd35286d5dd9362221cbf9c78a2a70a88133 Mon Sep 17 00:00:00 2001 From: GHOST Date: Sat, 11 Feb 2023 16:42:47 +0000 Subject: [PATCH 2/3] update docs on polyfill option --- documentation/docs/25-build-and-deploy/40-adapter-node.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/documentation/docs/25-build-and-deploy/40-adapter-node.md b/documentation/docs/25-build-and-deploy/40-adapter-node.md index 2cc9c182be77..8824cfd48051 100644 --- a/documentation/docs/25-build-and-deploy/40-adapter-node.md +++ b/documentation/docs/25-build-and-deploy/40-adapter-node.md @@ -122,7 +122,8 @@ export default { // default options are shown out: 'build', precompress: false, - envPrefix: '' + envPrefix: '', + polyfill: false }) } }; @@ -140,6 +141,10 @@ Enables precompressing using gzip and brotli for assets and prerendered pages. I If you need to change the name of the environment variables used to configure the deployment (for example, to deconflict with environment variables you don't control), you can specify a prefix: +### polyfill + +Controlls 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. + ```js envPrefix: 'MY_CUSTOM_'; ``` From ac3b06a017bc11d9c0104a9ef0d4eda5d1023ebd Mon Sep 17 00:00:00 2001 From: GHOST Date: Sat, 11 Feb 2023 16:44:01 +0000 Subject: [PATCH 3/3] nice one --- documentation/docs/25-build-and-deploy/40-adapter-node.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation/docs/25-build-and-deploy/40-adapter-node.md b/documentation/docs/25-build-and-deploy/40-adapter-node.md index 8824cfd48051..ca370579e99e 100644 --- a/documentation/docs/25-build-and-deploy/40-adapter-node.md +++ b/documentation/docs/25-build-and-deploy/40-adapter-node.md @@ -123,7 +123,7 @@ export default { out: 'build', precompress: false, envPrefix: '', - polyfill: false + polyfill: true }) } };