Skip to content

Commit

Permalink
simplify vercel adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Apr 18, 2021
1 parent 9a86d48 commit 38d0243
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 48 deletions.
3 changes: 1 addition & 2 deletions packages/adapter-vercel/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
.DS_Store
node_modules
/files
node_modules
File renamed without changes.
9 changes: 9 additions & 0 deletions packages/adapter-vercel/files/routes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"handle": "filesystem"
},
{
"src": "/.*",
"dest": ".vercel/functions/render"
}
]
47 changes: 19 additions & 28 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,46 @@
const { writeFileSync } = require('fs');
const { resolve, join } = require('path');
const { join } = require('path');
const esbuild = require('esbuild');

module.exports = function () {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
name: '@sveltejs/adapter-vercel',

async adapt(utils) {
const vercel_output_directory = resolve('.vercel_build_output');
utils.rimraf(vercel_output_directory);
const dir = '.vercel_build_output';
utils.rimraf(dir);

const config_directory = join(vercel_output_directory, 'config');
const static_directory = join(vercel_output_directory, 'static');
const lambda_directory = join(vercel_output_directory, 'functions', 'node', 'render');
const files = join(__dirname, 'files');

utils.copy(join(__dirname, 'src/entry.js'), '.svelte/vercel/entry.js');
const dirs = {
static: join(dir, 'static'),
lambda: join(dir, 'functions/node/render')
};

const esbuild = (await import('esbuild')).default;
utils.log.minor('Generating serverless function...');
utils.copy(join(files, 'entry.js'), '.svelte/vercel/entry.js');

await esbuild.build({
entryPoints: ['.svelte/vercel/entry.js'],
outfile: join(lambda_directory, 'index.js'),
outfile: join(dirs.lambda, 'index.js'),
bundle: true,
platform: 'node'
});

writeFileSync(join(lambda_directory, 'package.json'), JSON.stringify({ type: 'commonjs' }));

utils.log.minor('Writing client application...');
utils.copy_static_files(static_directory);
utils.copy_client_files(static_directory);
writeFileSync(join(dirs.lambda, 'package.json'), JSON.stringify({ type: 'commonjs' }));

utils.log.minor('Prerendering static pages...');
await utils.prerender({
dest: static_directory
dest: dirs.static
});

utils.log.minor('Copying assets...');
utils.copy_static_files(dirs.static);
utils.copy_client_files(dirs.static);

utils.log.minor('Writing routes...');
utils.mkdirp(config_directory);
writeFileSync(
join(config_directory, 'routes.json'),
JSON.stringify([
{
handle: 'filesystem'
},
{
src: '/.*',
dest: '.vercel/functions/render'
}
])
);
utils.copy(join(files, 'routes.json'), join(dir, 'config/routes.json'));
}
};

Expand Down
7 changes: 1 addition & 6 deletions packages/adapter-vercel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@
"files"
],
"scripts": {
"dev": "rollup -cw",
"build": "rollup -c",
"lint": "eslint --ignore-path .gitignore \"**/*.{ts,js,svelte}\" && npm run check-format",
"format": "prettier --write . --config ../../.prettierrc --ignore-path .gitignore",
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
"prepublishOnly": "npm run build"
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore"
},
"devDependencies": {
"@sveltejs/kit": "workspace:*",
"esbuild": "^0.11.12",
"rollup": "^2.41.1",
"sirv": "^1.0.11",
"typescript": "^4.2.3"
}
}
4 changes: 0 additions & 4 deletions packages/adapter-vercel/src/index.cjs

This file was deleted.

2 changes: 1 addition & 1 deletion packages/create-svelte/templates/default/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.1",
"scripts": {
"dev": "svelte-kit dev",
"build": "svelte-kit build",
"build": "svelte-kit build --verbose",
"start": "svelte-kit start"
},
"devDependencies": {
Expand Down
58 changes: 51 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 38d0243

Please sign in to comment.