Skip to content

Commit b5ed099

Browse files
authored
fix: @astrojs/vercel bug fixes (#3000)
* Fixed outDir * Updated server out * changeset * Renamed out to tmp
1 parent e8aaedc commit b5ed099

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

.changeset/sweet-ways-tan.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/vercel': patch
3+
---
4+
5+
Fixed build directory and clean-up

packages/integrations/vercel/src/index.ts

+24-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default function vercel(): AstroIntegration {
2323
name: '@astrojs/vercel',
2424
hooks: {
2525
'astro:config:setup': ({ config }) => {
26-
config.outDir = new URL('./.output/', config.outDir);
26+
config.outDir = new URL('./.output/', config.root);
2727
config.build.format = 'directory';
2828
},
2929
'astro:config:done': ({ setAdapter, config }) => {
@@ -33,21 +33,39 @@ export default function vercel(): AstroIntegration {
3333
'astro:build:start': async ({ buildConfig }) => {
3434
buildConfig.serverEntry = `${ENTRYFILE}.mjs`;
3535
buildConfig.client = new URL('./static/', _config.outDir);
36-
buildConfig.server = new URL('./server/pages/', _config.outDir);
36+
buildConfig.server = new URL('./server/tmp/', _config.outDir);
3737
},
3838
'astro:build:done': async ({ dir, routes }) => {
39-
const pagesDir = new URL('./server/pages/', dir);
39+
/*
40+
Why do we need two folders? Why don't we just generate all inside `server/pages/`?
41+
When the app builds, it throws some metadata inside a `chunks/` folder.
42+
43+
./server/
44+
pages/
45+
__astro_entry.mjs
46+
chunks/
47+
(lots of js files)
48+
49+
Those chunks will count as serverless functions (which cost money), so we
50+
need to bundle as much as possible in one file. Hence, the following code
51+
*/
52+
53+
const tmpDir = new URL('./server/tmp/', dir);
54+
const bundleDir = new URL('./server/pages/', dir);
55+
56+
await fs.mkdir(bundleDir, { recursive: true });
4057

4158
// Convert server entry to CommonJS
4259
await esbuild.build({
43-
entryPoints: [fileURLToPath(new URL(`./${ENTRYFILE}.mjs`, pagesDir))],
44-
outfile: fileURLToPath(new URL(`./${ENTRYFILE}.js`, pagesDir)),
60+
entryPoints: [fileURLToPath(new URL(`./${ENTRYFILE}.mjs`, tmpDir))],
61+
outfile: fileURLToPath(new URL(`./${ENTRYFILE}.js`, bundleDir)),
4562
bundle: true,
4663
format: 'cjs',
4764
platform: 'node',
4865
target: 'node14',
4966
});
50-
await fs.rm(new URL(`./${ENTRYFILE}.mjs`, pagesDir));
67+
68+
await fs.rm(tmpDir, { recursive: true });
5169

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

0 commit comments

Comments
 (0)