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

feat(vercel): ESM support and Node.js 16 #4904

Merged
merged 10 commits into from
May 13, 2022
5 changes: 5 additions & 0 deletions .changeset/breezy-grapes-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-vercel': patch
---

Support for Node.js 16
5 changes: 5 additions & 0 deletions .changeset/tall-ties-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-vercel': patch
---

The output of serverless now is ESM instead of CJS
34 changes: 25 additions & 9 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
Expand Down Expand Up @@ -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...');

Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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}` });
}
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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 };
}