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

refactor(vercel): Build Output API v3 #3216

Merged
merged 45 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
c3f976f
Removed ignores
JuanM04 Apr 26, 2022
a55569d
Migration to v3
JuanM04 Apr 26, 2022
9a64f77
Merge branch 'main' into juanm04/vercel-build-output-api
JuanM04 Apr 26, 2022
8a9d292
More changes
JuanM04 Apr 26, 2022
20adf95
Merge branch 'main' into juanm04/vercel-build-output-api
JuanM04 Apr 26, 2022
34cd040
Remove legacy redirects
JuanM04 Apr 26, 2022
0219fcd
Fail when there is no ENABLE_VC_BUILD
JuanM04 Apr 26, 2022
9de24d2
Fix edge
JuanM04 Apr 26, 2022
e11b6f1
Updated readme
JuanM04 Apr 26, 2022
f6ddaed
Changeset
JuanM04 Apr 26, 2022
a7a966e
Added static mode
JuanM04 Apr 26, 2022
c201c60
Updated documentation
JuanM04 Apr 26, 2022
5f5a0d7
Updated shim
JuanM04 Apr 27, 2022
83c29a3
Made edge work!
JuanM04 Apr 27, 2022
b32c21f
Updated changeset
JuanM04 Apr 27, 2022
aa983b4
Ensure empty dir
JuanM04 Apr 27, 2022
cff5ed0
Fixed redirects for dynamic paths
JuanM04 Apr 27, 2022
59656dc
Merge branch 'main' into juanm04/vercel-build-output-api
JuanM04 Apr 27, 2022
efa720e
Removed extra declaration
JuanM04 Apr 27, 2022
9231c7b
Merge branch 'main' into juanm04/vercel-build-output-api
JuanM04 Apr 28, 2022
916a06b
Merge branch 'main' into juanm04/vercel-build-output-api
JuanM04 Apr 30, 2022
de4568c
Splited imports
JuanM04 Apr 30, 2022
87ac091
Updated readme
JuanM04 Apr 30, 2022
595391d
Fixed some urls
JuanM04 Apr 30, 2022
036a146
Merge branch 'main' into juanm04/vercel-build-output-api
JuanM04 May 3, 2022
6fd7a48
Merge branch 'main' into juanm04/vercel-build-output-api
JuanM04 May 3, 2022
07928dc
Merge branch 'main' into juanm04/vercel-build-output-api
JuanM04 May 4, 2022
5fcd8d4
Deprecated shim!
JuanM04 May 4, 2022
a60f182
[test]: Vercel NFT
JuanM04 May 5, 2022
42a305d
Merge branch 'main' into juanm04/vercel-build-output-api
JuanM04 May 5, 2022
0830562
Beautify
JuanM04 May 5, 2022
85a797f
Edge bundle to node 14.19
JuanM04 May 5, 2022
9a67fd6
Merge branch 'main' into juanm04/vercel-build-output-api
JuanM04 May 6, 2022
5d6e273
Re-added shim (#3304)
JuanM04 May 6, 2022
af1be56
Added `node:` prefix
JuanM04 May 6, 2022
649544a
Use the same bundling as Deno for Edge
JuanM04 May 6, 2022
2716c85
Remove esbuild
JuanM04 May 6, 2022
f2fb587
Fixed shim
JuanM04 May 6, 2022
85eb0b1
Moved nft
JuanM04 May 6, 2022
a48a859
Updated changeset
JuanM04 May 6, 2022
df06593
Added note about Edge
JuanM04 May 6, 2022
dd24191
fix typo
JuanM04 May 6, 2022
34f73de
Added support for Node 16 (vercel/vercel#7772)
JuanM04 May 9, 2022
39f2663
Merge branch 'main' into juanm04/vercel-build-output-api
JuanM04 May 9, 2022
7a20b3b
Merge branch 'main' into juanm04/vercel-build-output-api
JuanM04 May 11, 2022
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
20 changes: 20 additions & 0 deletions .changeset/tiny-apes-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'@astrojs/vercel': minor
---


**[BREAKING]** Now with Build Output API (v3)! [See the README to get started](https://github.com/withastro/astro/tree/main/packages/integrations/vercel#readme).

- `trailingSlash` redirects works without a `vercel.json` file: just configure them inside your `astro.config.mjs`
- Multiple deploy targets: `edge`, `serverless` and `static`!
- When building to `serverless`, your code isn't transpiled to CJS anymore.

**Migrate from v0.1**

1. Change the import inside `astro.config.mjs`:
```diff
- import vercel from '@astrojs/vercel';
+ import vercel from '@astrojs/vercel/serverless';
```
2. Rename the `ENABLE_FILE_SYSTEM_API` environment variable to `ENABLE_VC_BUILD`, as Vercel changed it.
3. The output folder changed from `.output` to `.vercel/output` — you may need to update your `.gitignore`.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
node_modules/
dist/
.output/
*.tsbuildinfo
.DS_Store
.vercel
Expand Down
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

# Deep Directories
**/dist
**/.output
**/smoke
**/node_modules
**/fixtures
Expand Down
1 change: 0 additions & 1 deletion examples/blog-multiple-authors/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/blog/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/component/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/docs/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/env-vars/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/framework-alpine/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/framework-lit/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/framework-multiple/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/framework-preact/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/framework-react/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/framework-solid/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/framework-svelte/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/framework-vue/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/integrations-playground/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/minimal/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/non-html-pages/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/portfolio/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/starter/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/subpath/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/with-markdown-plugins/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/with-markdown-shiki/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/with-markdown/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/with-nanostores/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/with-tailwindcss/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
1 change: 0 additions & 1 deletion examples/with-vite-plugin-pwa/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# build output
dist/
.output/

# dependencies
node_modules/
Expand Down
49 changes: 41 additions & 8 deletions packages/integrations/vercel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,71 @@ Use this integration in your Astro configuration file:

```js
import { defineConfig } from 'astro/config';
import vercel from '@astrojs/vercel';
import vercel from '@astrojs/vercel/serverless';

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

When you build your project, Astro will know to use the `.output` folder format that Vercel expects.
When you build your project, Astro will know to use the `.vercel/output` folder format that Vercel expects.

```
astro build
```
## Deploying

That's it! You can deploy by CLI (`vercel deploy`) or by connecting your new repo in the [Vercel Dashboard](https://vercel.com/).
You can deploy by CLI (`vercel deploy`) or by connecting your new repo in the [Vercel Dashboard](https://vercel.com/). Alternatively, you can create a production build locally:

```sh
ENABLE_VC_BUILD=1 astro build
vercel deploy --prebuilt
```

## Requirements

**Vercel's [File System API](https://vercel.com/docs/file-system-api/v2) must be enabled.** You must enable it yourself by setting the environment variable: `ENABLE_FILE_SYSTEM_API=1`.
**Vercel's [Build Output API](https://vercel.com/docs/build-output-api/v3) must be enabled.** You must enable it yourself by setting the environment variable: `ENABLE_VC_BUILD=1`.

```js
// vercel.json
{
"build": {
"env": {
"ENABLE_FILE_SYSTEM_API": "1"
"ENABLE_VC_BUILD": "1"
}
}
}
```

[Learn more about setting enviroment variables in Vercel](https://vercel.com/docs/concepts/projects/environment-variables).

## Targets

You can deploy to different targes:

- `edge`: SSR inside a [Edge function](https://vercel.com/docs/concepts/functions/edge-functions).
- `serverless`: SSR inside a [Node.js function](https://vercel.com/docs/concepts/functions/serverless-functions).
- `static`: generates a static website following Vercel's output formats, redirects, etc.

> **Note**: deploying to the Edge has [its limitations](https://vercel.com/docs/concepts/functions/edge-functions#known-limitations) — they can't be more than 1 MB in size and they don't support native Node.js APIs, among others.

You can change where to target by changing the import:

```js
import vercel from '@astrojs/vercel/edge';
import vercel from '@astrojs/vercel/serverless';
import vercel from '@astrojs/vercel/static';
```

### Node.js version

When deploying to `serverless` you can choose what version of Node.js you want to target: `12.x`, `14.x` or `16.x` (default).

```js
import { defineConfig } from 'astro/config';
import vercel from '@astrojs/vercel/serverless';

export default defineConfig({
adapter: vercel({ nodeVersion: '14.x' })
});
```

## Limitations

Expand Down
9 changes: 6 additions & 3 deletions packages/integrations/vercel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
"bugs": "https://github.com/withastro/astro/issues",
"homepage": "https://astro.build",
"exports": {
".": "./dist/index.js",
"./server-entrypoint": "./dist/server-entrypoint.js",
"./edge": "./dist/edge/adapter.js",
"./edge/entrypoint": "./dist/edge/entrypoint.js",
"./serverless": "./dist/serverless/adapter.js",
"./serverless/entrypoint": "./dist/serverless/entrypoint.js",
"./static": "./dist/serverless/adapter.js",
"./package.json": "./package.json"
},
"scripts": {
Expand All @@ -25,7 +28,7 @@
},
"dependencies": {
"@astrojs/webapi": "^0.11.1",
"esbuild": "^0.14.38"
"@vercel/nft": "^0.18.2"
},
"devDependencies": {
"astro": "workspace:*",
Expand Down
74 changes: 74 additions & 0 deletions packages/integrations/vercel/src/edge/adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import type { AstroAdapter, AstroConfig, AstroIntegration } from 'astro';

import { writeJson, getVercelOutput } from '../lib/fs.js';
import { getRedirects } from '../lib/redirects.js';

const PACKAGE_NAME = '@astrojs/vercel/edge';

function getAdapter(): AstroAdapter {
return {
name: PACKAGE_NAME,
serverEntrypoint: `${PACKAGE_NAME}/entrypoint`,
exports: ['default'],
};
}

export default function vercelEdge(): AstroIntegration {
let _config: AstroConfig;
let functionFolder: URL;
let serverEntry: string;

return {
name: PACKAGE_NAME,
hooks: {
'astro:config:setup': ({ config }) => {
config.outDir = getVercelOutput(config.root);
},
'astro:config:done': ({ setAdapter, config }) => {
setAdapter(getAdapter());
_config = config;
},
'astro:build:setup': ({ vite, target }) => {
if (target === 'server') {
vite.resolve ||= {};
vite.resolve.alias ||= {};
const alias = vite.resolve.alias as Record<string, string>;
alias['react-dom/server'] = 'react-dom/server.browser';
vite.ssr = {
noExternal: true,
};
}
},
'astro:build:start': async ({ buildConfig }) => {
if (String(process.env.ENABLE_VC_BUILD) !== '1') {
throw new Error(
`The enviroment variable "ENABLE_VC_BUILD" was not found. Make sure you have it set to "1" in your Vercel project.\nLearn how to set enviroment variables here: https://vercel.com/docs/concepts/projects/environment-variables`
);
}

buildConfig.serverEntry = serverEntry = 'entry.mjs';
buildConfig.client = new URL('./static/', _config.outDir);
buildConfig.server = functionFolder = new URL('./functions/render.func/', _config.outDir);
},
'astro:build:done': async ({ routes }) => {
// Edge function config
// https://vercel.com/docs/build-output-api/v3#vercel-primitives/edge-functions/configuration
await writeJson(new URL(`./.vc-config.json`, functionFolder), {
runtime: 'edge',
entrypoint: serverEntry,
});

// Output configuration
// https://vercel.com/docs/build-output-api/v3#build-output-configuration
await writeJson(new URL(`./config.json`, _config.outDir), {
version: 3,
routes: [
...getRedirects(routes, _config),
{ handle: 'filesystem' },
{ src: '/.*', middlewarePath: 'render' },
],
});
},
},
};
}
Loading