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

Add polyfill option to adapter-node #8991

Merged
merged 3 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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/strange-sheep-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-node': minor
---

add polyfill option
7 changes: 6 additions & 1 deletion documentation/docs/25-build-and-deploy/40-adapter-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ export default {
// default options are shown
out: 'build',
precompress: false,
envPrefix: ''
envPrefix: '',
polyfill: true
})
}
};
Expand All @@ -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_';
```
Expand Down
1 change: 1 addition & 0 deletions packages/adapter-node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface AdapterOptions {
out?: string;
precompress?: boolean;
envPrefix?: string;
polyfill?: boolean;
}

export default function plugin(options?: AdapterOptions): Adapter;
10 changes: 8 additions & 2 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('.').default} */
export default function (opts = {}) {
const { out = 'build', precompress, envPrefix = '' } = opts;
const { out = 'build', precompress, envPrefix = '', polyfill = true } = opts;

return {
name: '@sveltejs/adapter-node',
Expand Down Expand Up @@ -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');
}
}
};
}
11 changes: 10 additions & 1 deletion packages/adapter-node/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
];
2 changes: 1 addition & 1 deletion packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import './shims';
import 'SHIMS';
import fs from 'node:fs';
import path from 'node:path';
import sirv from 'sirv';
Expand Down