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 1 commit
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
Prev Previous commit
Next Next commit
More changes
  • Loading branch information
JuanM04 committed Apr 26, 2022
commit 8a9d292737ecc3bb938be22c0d3ef69113adb84b
3 changes: 2 additions & 1 deletion packages/integrations/vercel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
},
"dependencies": {
"@astrojs/webapi": "^0.11.1",
"@vercel/routing-utils": "^1.13.1"
"@vercel/routing-utils": "^1.13.1",
"esbuild": "^0.14.38"
},
"devDependencies": {
"astro": "workspace:*",
Expand Down
103 changes: 62 additions & 41 deletions packages/integrations/vercel/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { AstroAdapter, AstroConfig, AstroIntegration } from 'astro';
import type { PathLike } from 'fs';

import fs from 'fs/promises';
import { fileURLToPath } from 'url';
import esbuild from 'esbuild';
import { getTransformedRoutes, Redirect, Rewrite } from '@vercel/routing-utils';

const writeJson = (path: PathLike, data: any) =>
Expand Down Expand Up @@ -38,13 +41,13 @@ export default function vercel({ edge = false }: Options = {}): AstroIntegration
_config = config;
},
'astro:build:setup': ({ vite, target }) => {
if (target === 'server') {
if (!edge && target === 'server') {
vite.build = {
...(vite.build || {}),
rollupOptions: {
...(vite.build?.rollupOptions || {}),
output: {
...(vite.build?.rollupOptions || {}),
...(vite.build?.rollupOptions?.output || {}),
format: 'cjs',
},
},
Expand All @@ -63,6 +66,18 @@ export default function vercel({ edge = false }: Options = {}): AstroIntegration
}
},
'astro:build:done': async ({ routes }) => {
const entryPath = fileURLToPath(new URL(_serverEntry, _serverOut));

// Bundle dependencies
await esbuild.build({
entryPoints: [entryPath],
outfile: entryPath,
bundle: true,
target: 'node14',
allowOverwrite: true,
...(edge ? { format: 'esm', platform: 'browser' } : { format: 'cjs', platform: 'node' }),
});

if (edge) {
// Edge function config
// https://vercel.com/docs/build-output-api/v3#vercel-primitives/edge-functions/configuration
Expand Down Expand Up @@ -118,49 +133,55 @@ export default function vercel({ edge = false }: Options = {}): AstroIntegration
}
}

const transformedRoutes = getTransformedRoutes({
nowConfig: {
rewrites: [],
redirects:
_config.trailingSlash !== 'ignore'
? routes
.filter((route) => route.type === 'page' && !route.pathname?.endsWith('/'))
.map((route) => {
const path =
'/' +
route.segments
.map((segments) =>
segments
.map((part) =>
part.spread
? `:${part.content}*`
: part.dynamic
? `:${part.content}`
: part.content
)
.join('')
)
.join('/');

let source, destination;

if (_config.trailingSlash === 'always') {
source = path;
destination = path + '/';
} else {
source = path + '/';
destination = path;
}

return { source, destination, statusCode: 308 };
})
: [],
},
});

if (transformedRoutes.error) {
throw new Error(JSON.stringify(transformedRoutes.error, null, 2));
}

// Output configuration
// https://vercel.com/docs/build-output-api/v3#build-output-configuration
await writeJson(new URL(`./config.json`, _config.outDir), {
version: 3,
routes: getTransformedRoutes({
nowConfig: {
rewrites: [],
redirects:
_config.trailingSlash !== 'ignore'
? routes
.filter((route) => route.type === 'page' && !route.pathname?.endsWith('/'))
.map((route) => {
const path =
'/' +
route.segments
.map((segments) =>
segments
.map((part) =>
part.spread
? `:${part.content}*`
: part.dynamic
? `:${part.content}`
: part.content
)
.join('')
)
.join('/');

let source, destination;

if (_config.trailingSlash === 'always') {
source = path;
destination = path + '/';
} else {
source = path + '/';
destination = path;
}

return { source, destination, statusCode: 308 };
})
: [],
},
}),
routes: transformedRoutes.routes,
});
},
},
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

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