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
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
---

You can now select a Node.js version for your serverless functions
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
1 change: 1 addition & 0 deletions packages/adapter-vercel/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type Options = {
edge?: boolean;
external?: string[];
split?: boolean;
nodeVersion?: '16.x' | '14.x' | '12.x';
JuanM04 marked this conversation as resolved.
Show resolved Hide resolved
};

declare function plugin(options?: Options): Adapter;
Expand Down
28 changes: 19 additions & 9 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,24 @@ const redirects = {
};

/** @type {import('.')} **/
export default function ({ external = [], edge, split } = {}) {
export default function ({ external = [], edge, split, nodeVersion } = {}) {
return {
name: '@sveltejs/adapter-vercel',

async adapt(builder) {
if (process.env.ENABLE_VC_BUILD) {
await v3(builder, external, edge, split);
if (edge && nodeVersion) {
throw new Error(
'`nodeVersion` option can only be used with serverless functions, not edge functions'
);
}

await v3(builder, external, edge, split, nodeVersion || '16.x');
} else {
if (edge || split) {
throw new Error('edge and split options can only be used with ENABLE_VC_BUILD');
if (edge || split || nodeVersion) {
throw new Error(
'`edge`, `split` and `nodeVersion` options can only be used with ENABLE_VC_BUILD'
);
}

await v1(builder, external);
Expand Down Expand Up @@ -198,8 +206,9 @@ async function v1(builder, external) {
* @param {string[]} external
* @param {boolean} edge
* @param {boolean} split
* @param {'16.x' | '14.x' | '12.x'} nodeVersion
*/
async function v3(builder, external, edge, split) {
async function v3(builder, external, edge, split, nodeVersion) {
const dir = '.vercel/output';

const tmp = builder.getBuildDirectory('vercel-tmp');
Expand Down Expand Up @@ -263,23 +272,24 @@ async function v3(builder, external, edge, split) {
await esbuild.build({
entryPoints: [`${tmp}/serverless.js`],
outfile: `${dirs.functions}/${name}.func/index.js`,
target: 'node14',
target:
nodeVersion === '12.x' ? 'node12.22' : nodeVersion === '14.x' ? 'node14.19' : 'node16.15',
bundle: true,
platform: 'node',
format: 'cjs',
format: 'esm',
external
});

write(
`${dirs.functions}/${name}.func/.vc-config.json`,
JSON.stringify({
runtime: 'nodejs14.x',
runtime: `nodejs${nodeVersion}`,
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