Skip to content

Commit

Permalink
Temporal Fix to Vercel Adapter Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TeenBiscuits committed Dec 31, 2024
1 parent a721869 commit 7c21bae
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
4 changes: 4 additions & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import rehypeMathJax from "rehype-mathjax";
// Vercel Adapter
import vercel from "@astrojs/vercel";

// Temporal Script to fix https://github.com/withastro/adapters/issues/445
import { CopyFilesPlugin } from "./scripts/copy-files.ts";

// Partytown Integration
import partytown from "@astrojs/partytown";

Expand Down Expand Up @@ -242,6 +245,7 @@ export default defineConfig({
tailwind({
applyBaseStyles: false,
}),
CopyFilesPlugin(),
],
markdown: {
remarkPlugins: [remarkMath],
Expand Down
62 changes: 62 additions & 0 deletions scripts/copy-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// mohdlatif
//
// https://github.com/withastro/adapters/issues/445#issuecomment-2564233900

import type { AstroIntegration } from "astro";
import { promises as fs } from "fs";
import path from "path";
import { fileURLToPath } from "url";

// Utility function for formatted logging
function formatLog(tag: string, message: string) {
const timestamp = new Date().toLocaleTimeString("en-US", {
hour: "2-digit",
hour12: false,
minute: "2-digit",
second: "2-digit",
});

// eslint-disable-next-line no-console
console.log(
"\n" + // Add space above
`\x1b[90m${timestamp}\x1b[0m ` + // Gray timestamp
`[\x1b[36m${tag}\x1b[0m] ` + // Cyan colored tag
`${message}` + // Message
"\n", // Add space below
);
}

async function copyFiles(srcDir: string, destDir: string) {
const files = await fs.readdir(srcDir);

for (const file of files) {
const srcPath = path.join(srcDir, file);
const destPath = path.join(destDir, file);

const stat = await fs.stat(srcPath);

if (stat.isDirectory()) {
await fs.mkdir(destPath, { recursive: true });
await copyFiles(srcPath, destPath);
} else {
await fs.copyFile(srcPath, destPath);
}
}
}

export function CopyFilesPlugin(): AstroIntegration {
return {
hooks: {
"astro:build:done": async ({ dir }) => {
formatLog("copy-files", "Copying files to .vercel/output/static");

const distDir = fileURLToPath(dir.href);
const staticDir = path.resolve(".vercel/output/static");

await fs.mkdir(staticDir, { recursive: true });
await copyFiles(distDir, staticDir);
},
},
name: "copy-files",
};
}

0 comments on commit 7c21bae

Please sign in to comment.