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 adapter #2915

Merged
merged 26 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules/
dist/
.output/
*.tsbuildinfo
.DS_Store
.vercel
Expand Down
44 changes: 44 additions & 0 deletions packages/integrations/vercel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# @astrojs/netlify
JuanM04 marked this conversation as resolved.
Show resolved Hide resolved

Deploy your server-side rendered (SSR) Astro app to [Netlify](https://www.netlify.com/).

Use this adapter in your Astro configuration file:

```js
import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify/functions';

export default defineConfig({
adapter: netlify()
});
```

After you build your site the `netlify/` folder will contain [Netlify Functions](https://docs.netlify.com/functions/overview/) in the `netlify/functions/` folder.

Now you can deploy!

```shell
netlify deploy
```

## Configuration

The output folder is configuration with the `dist` property when creating the adapter.

```js
import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify/functions';

export default defineConfig({
adapter: netlify({
dist: new URL('./dist/', import.meta.url)
})
});
```

And then point to the dist in your `netlify.toml`:

```toml
[functions]
directory = "dist/functions"
```
33 changes: 33 additions & 0 deletions packages/integrations/vercel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@astrojs/vercel",
"description": "Deploy your site to Vercel",
"version": "0.0.1",
"type": "module",
"types": "./dist/index.d.ts",
"author": "withastro",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/withastro/astro.git",
"directory": "packages/integrations/vercel"
},
"bugs": "https://github.com/withastro/astro/issues",
"homepage": "https://astro.build",
"exports": {
".": "./dist/index.js",
"./package.json": "./package.json"
},
"scripts": {
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
"dev": "astro-scripts dev \"src/**/*.ts\""
},
"dependencies": {
"@astrojs/webapi": "^0.11.0",
"@vercel/nft": "^0.18.0",
"@vercel/node": "^1.14.0"
},
"devDependencies": {
"astro": "workspace:*",
"astro-scripts": "workspace:*"
}
}
51 changes: 51 additions & 0 deletions packages/integrations/vercel/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { AstroIntegration, AstroConfig } from 'astro';
import fs from 'fs/promises';
import type { PathLike } from 'fs';

export type { VercelApiHandler, VercelRequest, VercelRequestBody, VercelRequestCookies, VercelRequestQuery, VercelResponse } from '@vercel/node';

const writeJson = (path: PathLike, data: any) => fs.writeFile(path, JSON.stringify(data), { encoding: 'utf-8' });

export function vercelFunctions(): AstroIntegration {
let _config: AstroConfig;
let output: URL;
return {
name: '@astrojs/vercel',
hooks: {
'astro:config:setup': ({ config }) => {
output = new URL('./.output/', config.projectRoot);
config.dist = new URL('./static/', output);
config.buildOptions.pageUrlFormat = 'directory';
},
'astro:config:done': async ({ config, setAdapter }) => {
// setAdapter(getAdapter(config.buildOptions.site));
JuanM04 marked this conversation as resolved.
Show resolved Hide resolved
_config = config;
},
'astro:build:start': async () => {
await fs.rm(output, { recursive: true });
},
'astro:build:done': async ({ pages }) => {
await Promise.all(
pages.map(async ({ pathname }) => {
const origin = new URL(`./static/${pathname}index.html`, output);
const finalDir = new URL(`./server/pages/${pathname}`, output);

await fs.mkdir(finalDir, { recursive: true });
await fs.copyFile(origin, new URL(`./index.html`, finalDir));
await fs.rm(origin);
})
);

// Routes Manifest
// https://vercel.com/docs/file-system-api#configuration/routes
await writeJson(new URL(`./routes-manifest.json`, output), {
version: 3,
basePath: '/',
pages404: false,
});
},
},
};
}

export default vercelFunctions;
10 changes: 10 additions & 0 deletions packages/integrations/vercel/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../../tsconfig.base.json",
"include": ["src"],
"compilerOptions": {
"allowJs": true,
"module": "ES2020",
"outDir": "./dist",
"target": "ES2020"
}
}
Loading