Skip to content

Commit 7609ad0

Browse files
JuanM04Rich-Harris
andauthored
feat(vercel): ESM support and Node.js 16 (#4904)
* Added runtime option * Added support for ESM * Renamed runtime to nodeVersion * Added warning * Changeset * Added note to readme * Readme with spaces instead of tabs * Detect node version automatically * put check inside get_node_version * use esm everywhere, base target on env for v1 output as well as v3 Co-authored-by: Rich Harris <hello@rich-harris.dev>
1 parent 1f6eb44 commit 7609ad0

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

.changeset/breezy-grapes-smash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-vercel': patch
3+
---
4+
5+
Support for Node.js 16

.changeset/tall-ties-juggle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/adapter-vercel': patch
3+
---
4+
5+
The output of serverless now is ESM instead of CJS

packages/adapter-vercel/index.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export default function ({ external = [], edge, split } = {}) {
9090
await v3(builder, external, edge, split);
9191
} else {
9292
if (edge || split) {
93-
throw new Error('edge and split options can only be used with ENABLE_VC_BUILD');
93+
throw new Error('`edge` and `split` options can only be used with ENABLE_VC_BUILD');
9494
}
9595

9696
await v1(builder, external);
@@ -104,6 +104,8 @@ export default function ({ external = [], edge, split } = {}) {
104104
* @param {string[]} external
105105
*/
106106
async function v1(builder, external) {
107+
const node_version = get_node_version();
108+
107109
const dir = '.vercel_build_output';
108110

109111
const tmp = builder.getBuildDirectory('vercel-tmp');
@@ -139,13 +141,14 @@ async function v1(builder, external) {
139141
await esbuild.build({
140142
entryPoints: [`${tmp}/serverless.js`],
141143
outfile: `${dirs.lambda}/index.js`,
142-
target: 'node14',
144+
target: `node${node_version.full}`,
143145
bundle: true,
144146
platform: 'node',
145-
external
147+
external,
148+
format: 'esm'
146149
});
147150

148-
writeFileSync(`${dirs.lambda}/package.json`, JSON.stringify({ type: 'commonjs' }));
151+
writeFileSync(`${dirs.lambda}/package.json`, JSON.stringify({ type: 'module' }));
149152

150153
builder.log.minor('Copying assets...');
151154

@@ -200,6 +203,8 @@ async function v1(builder, external) {
200203
* @param {boolean} split
201204
*/
202205
async function v3(builder, external, edge, split) {
206+
const node_version = get_node_version();
207+
203208
const dir = '.vercel/output';
204209

205210
const tmp = builder.getBuildDirectory('vercel-tmp');
@@ -263,23 +268,23 @@ async function v3(builder, external, edge, split) {
263268
await esbuild.build({
264269
entryPoints: [`${tmp}/serverless.js`],
265270
outfile: `${dirs.functions}/${name}.func/index.js`,
266-
target: 'node14',
271+
target: `node${node_version.full}`,
267272
bundle: true,
268273
platform: 'node',
269-
format: 'cjs',
274+
format: 'esm',
270275
external
271276
});
272277

273278
write(
274279
`${dirs.functions}/${name}.func/.vc-config.json`,
275280
JSON.stringify({
276-
runtime: 'nodejs14.x',
281+
runtime: `nodejs${node_version.major}.x`,
277282
handler: 'index.js',
278283
launcherType: 'Nodejs'
279284
})
280285
);
281286

282-
write(`${dirs.functions}/${name}.func/package.json`, JSON.stringify({ type: 'commonjs' }));
287+
write(`${dirs.functions}/${name}.func/package.json`, JSON.stringify({ type: 'module' }));
283288

284289
routes.push({ src: pattern, dest: `/${name}` });
285290
}
@@ -308,7 +313,7 @@ async function v3(builder, external, edge, split) {
308313
await esbuild.build({
309314
entryPoints: [`${tmp}/edge.js`],
310315
outfile: `${dirs.functions}/${name}.func/index.js`,
311-
target: 'node14',
316+
target: 'es2020', // TODO verify what the edge runtime supports
312317
bundle: true,
313318
platform: 'node',
314319
format: 'esm',
@@ -385,3 +390,14 @@ function write(file, data) {
385390

386391
writeFileSync(file, data);
387392
}
393+
394+
function get_node_version() {
395+
const full = process.version.slice(1); // 'v16.5.0' --> '16.5.0'
396+
const major = parseInt(full.split('.')[0]); // '16.5.0' --> 16
397+
398+
if (major < 14) {
399+
throw new Error(`SvelteKit only support Node.js version 14 or greater (currently using v${full}). Consult the documentation: https://vercel.com/docs/runtimes#official-runtimes/node-js/node-js-version`)
400+
}
401+
402+
return { major, full };
403+
}

0 commit comments

Comments
 (0)