Skip to content

Commit d963394

Browse files
authored
Add polyfill option to adapter-node (#8991)
* add polyfill option * update docs on polyfill option * nice one
1 parent 74cfa8d commit d963394

File tree

6 files changed

+31
-5
lines changed

6 files changed

+31
-5
lines changed

.changeset/strange-sheep-grab.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-node': minor
3+
---
4+
5+
add polyfill option

documentation/docs/25-build-and-deploy/40-adapter-node.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ export default {
122122
// default options are shown
123123
out: 'build',
124124
precompress: false,
125-
envPrefix: ''
125+
envPrefix: '',
126+
polyfill: true
126127
})
127128
}
128129
};
@@ -140,6 +141,10 @@ Enables precompressing using gzip and brotli for assets and prerendered pages. I
140141

141142
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:
142143

144+
### polyfill
145+
146+
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.
147+
143148
```js
144149
envPrefix: 'MY_CUSTOM_';
145150
```

packages/adapter-node/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface AdapterOptions {
99
out?: string;
1010
precompress?: boolean;
1111
envPrefix?: string;
12+
polyfill?: boolean;
1213
}
1314

1415
export default function plugin(options?: AdapterOptions): Adapter;

packages/adapter-node/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const files = fileURLToPath(new URL('./files', import.meta.url).href);
99

1010
/** @type {import('.').default} */
1111
export default function (opts = {}) {
12-
const { out = 'build', precompress, envPrefix = '' } = opts;
12+
const { out = 'build', precompress, envPrefix = '', polyfill = true } = opts;
1313

1414
return {
1515
name: '@sveltejs/adapter-node',
@@ -72,10 +72,16 @@ export default function (opts = {}) {
7272
ENV: './env.js',
7373
HANDLER: './handler.js',
7474
MANIFEST: './server/manifest.js',
75-
SERVER: `./server/index.js`,
75+
SERVER: './server/index.js',
76+
SHIMS: './shims.js',
7677
ENV_PREFIX: JSON.stringify(envPrefix)
7778
}
7879
});
80+
81+
// If polyfills aren't wanted then clear the file
82+
if (!polyfill) {
83+
writeFileSync(`${out}/shims.js`, '', 'utf-8');
84+
}
7985
}
8086
};
8187
}

packages/adapter-node/rollup.config.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ export default [
3030
inlineDynamicImports: true
3131
},
3232
plugins: [nodeResolve(), commonjs(), json()],
33-
external: ['ENV', 'MANIFEST', 'SERVER', ...builtinModules]
33+
external: ['ENV', 'MANIFEST', 'SERVER', 'SHIMS', ...builtinModules]
34+
},
35+
{
36+
input: 'src/shims.js',
37+
output: {
38+
file: 'files/shims.js',
39+
format: 'esm'
40+
},
41+
plugins: [nodeResolve(), commonjs()],
42+
external: builtinModules
3443
}
3544
];

packages/adapter-node/src/handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import './shims';
1+
import 'SHIMS';
22
import fs from 'node:fs';
33
import path from 'node:path';
44
import sirv from 'sirv';

0 commit comments

Comments
 (0)