Skip to content

Commit

Permalink
feat(vercel): ESM support and Node.js 16 (#4904)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
JuanM04 and Rich-Harris committed May 13, 2022
1 parent 1f6eb44 commit 7609ad0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
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 };
}

0 comments on commit 7609ad0

Please sign in to comment.