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

fix: @astrojs/vercel bug fixes #3000

Merged
merged 4 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all 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/sweet-ways-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/vercel': patch
---

Fixed build directory and clean-up
30 changes: 24 additions & 6 deletions packages/integrations/vercel/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function vercel(): AstroIntegration {
name: '@astrojs/vercel',
hooks: {
'astro:config:setup': ({ config }) => {
config.outDir = new URL('./.output/', config.outDir);
config.outDir = new URL('./.output/', config.root);
config.build.format = 'directory';
},
'astro:config:done': ({ setAdapter, config }) => {
Expand All @@ -33,21 +33,39 @@ export default function vercel(): AstroIntegration {
'astro:build:start': async ({ buildConfig }) => {
buildConfig.serverEntry = `${ENTRYFILE}.mjs`;
buildConfig.client = new URL('./static/', _config.outDir);
buildConfig.server = new URL('./server/pages/', _config.outDir);
buildConfig.server = new URL('./server/tmp/', _config.outDir);
},
'astro:build:done': async ({ dir, routes }) => {
const pagesDir = new URL('./server/pages/', dir);
/*
Why do we need two folders? Why don't we just generate all inside `server/pages/`?
When the app builds, it throws some metadata inside a `chunks/` folder.

./server/
pages/
__astro_entry.mjs
chunks/
(lots of js files)

Those chunks will count as serverless functions (which cost money), so we
need to bundle as much as possible in one file. Hence, the following code
*/

const tmpDir = new URL('./server/tmp/', dir);
const bundleDir = new URL('./server/pages/', dir);

await fs.mkdir(bundleDir, { recursive: true });

// Convert server entry to CommonJS
await esbuild.build({
entryPoints: [fileURLToPath(new URL(`./${ENTRYFILE}.mjs`, pagesDir))],
outfile: fileURLToPath(new URL(`./${ENTRYFILE}.js`, pagesDir)),
entryPoints: [fileURLToPath(new URL(`./${ENTRYFILE}.mjs`, tmpDir))],
outfile: fileURLToPath(new URL(`./${ENTRYFILE}.js`, bundleDir)),
bundle: true,
format: 'cjs',
platform: 'node',
target: 'node14',
});
await fs.rm(new URL(`./${ENTRYFILE}.mjs`, pagesDir));

await fs.rm(tmpDir, { recursive: true });

// Routes Manifest
// https://vercel.com/docs/file-system-api#configuration/routes
Expand Down