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/documentation/docs/25-build-and-deploy/40-adapter-node.md b/documentation/docs/25-build-and-deploy/40-adapter-node.md index 2cc9c182be77..ca370579e99e 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: true }) } }; @@ -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_'; ``` 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';