From 7609ad0759c895600198eaf4345d735c7c6d1166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Seery?= Date: Fri, 13 May 2022 20:40:32 -0300 Subject: [PATCH] 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 --- .changeset/breezy-grapes-smash.md | 5 +++++ .changeset/tall-ties-juggle.md | 5 +++++ packages/adapter-vercel/index.js | 34 +++++++++++++++++++++++-------- 3 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 .changeset/breezy-grapes-smash.md create mode 100644 .changeset/tall-ties-juggle.md diff --git a/.changeset/breezy-grapes-smash.md b/.changeset/breezy-grapes-smash.md new file mode 100644 index 000000000000..43b7d732414f --- /dev/null +++ b/.changeset/breezy-grapes-smash.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-vercel': patch +--- + +Support for Node.js 16 diff --git a/.changeset/tall-ties-juggle.md b/.changeset/tall-ties-juggle.md new file mode 100644 index 000000000000..9ba00a39f1a3 --- /dev/null +++ b/.changeset/tall-ties-juggle.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-vercel': patch +--- + +The output of serverless now is ESM instead of CJS diff --git a/packages/adapter-vercel/index.js b/packages/adapter-vercel/index.js index f98c2c4b5137..e246c8891c23 100644 --- a/packages/adapter-vercel/index.js +++ b/packages/adapter-vercel/index.js @@ -90,7 +90,7 @@ export default function ({ external = [], edge, split } = {}) { await v3(builder, external, edge, split); } else { if (edge || split) { - throw new Error('edge and split options can only be used with ENABLE_VC_BUILD'); + throw new Error('`edge` and `split` options can only be used with ENABLE_VC_BUILD'); } await v1(builder, external); @@ -104,6 +104,8 @@ export default function ({ external = [], edge, split } = {}) { * @param {string[]} external */ async function v1(builder, external) { + const node_version = get_node_version(); + const dir = '.vercel_build_output'; const tmp = builder.getBuildDirectory('vercel-tmp'); @@ -139,13 +141,14 @@ async function v1(builder, external) { await esbuild.build({ entryPoints: [`${tmp}/serverless.js`], outfile: `${dirs.lambda}/index.js`, - target: 'node14', + target: `node${node_version.full}`, bundle: true, platform: 'node', - external + external, + format: 'esm' }); - writeFileSync(`${dirs.lambda}/package.json`, JSON.stringify({ type: 'commonjs' })); + writeFileSync(`${dirs.lambda}/package.json`, JSON.stringify({ type: 'module' })); builder.log.minor('Copying assets...'); @@ -200,6 +203,8 @@ async function v1(builder, external) { * @param {boolean} split */ async function v3(builder, external, edge, split) { + const node_version = get_node_version(); + const dir = '.vercel/output'; const tmp = builder.getBuildDirectory('vercel-tmp'); @@ -263,23 +268,23 @@ async function v3(builder, external, edge, split) { await esbuild.build({ entryPoints: [`${tmp}/serverless.js`], outfile: `${dirs.functions}/${name}.func/index.js`, - target: 'node14', + target: `node${node_version.full}`, bundle: true, platform: 'node', - format: 'cjs', + format: 'esm', external }); write( `${dirs.functions}/${name}.func/.vc-config.json`, JSON.stringify({ - runtime: 'nodejs14.x', + runtime: `nodejs${node_version.major}.x`, handler: 'index.js', launcherType: 'Nodejs' }) ); - write(`${dirs.functions}/${name}.func/package.json`, JSON.stringify({ type: 'commonjs' })); + write(`${dirs.functions}/${name}.func/package.json`, JSON.stringify({ type: 'module' })); routes.push({ src: pattern, dest: `/${name}` }); } @@ -308,7 +313,7 @@ async function v3(builder, external, edge, split) { await esbuild.build({ entryPoints: [`${tmp}/edge.js`], outfile: `${dirs.functions}/${name}.func/index.js`, - target: 'node14', + target: 'es2020', // TODO verify what the edge runtime supports bundle: true, platform: 'node', format: 'esm', @@ -385,3 +390,14 @@ function write(file, data) { writeFileSync(file, data); } + +function get_node_version() { + const full = process.version.slice(1); // 'v16.5.0' --> '16.5.0' + const major = parseInt(full.split('.')[0]); // '16.5.0' --> 16 + + if (major < 14) { + 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`) + } + + return { major, full }; +}