From e890b528c4c3045d4dc04fb74fc1a739d4f8f618 Mon Sep 17 00:00:00 2001 From: JuanM04 Date: Wed, 30 Mar 2022 23:08:39 -0300 Subject: [PATCH] Build to CommonJS --- packages/integrations/vercel/package.json | 3 +- packages/integrations/vercel/src/index.ts | 41 ++++++++++++----------- pnpm-lock.yaml | 2 ++ 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/packages/integrations/vercel/package.json b/packages/integrations/vercel/package.json index 53dc7327bf..229a48b8ec 100644 --- a/packages/integrations/vercel/package.json +++ b/packages/integrations/vercel/package.json @@ -23,7 +23,8 @@ "dev": "astro-scripts dev \"src/**/*.ts\"" }, "dependencies": { - "@astrojs/webapi": "^0.11.0" + "@astrojs/webapi": "^0.11.0", + "esbuild": "0.14.25" }, "devDependencies": { "astro": "workspace:*", diff --git a/packages/integrations/vercel/src/index.ts b/packages/integrations/vercel/src/index.ts index 0df7b9485f..3521dfc8d3 100644 --- a/packages/integrations/vercel/src/index.ts +++ b/packages/integrations/vercel/src/index.ts @@ -1,9 +1,13 @@ import type { AstroAdapter, AstroIntegration } from 'astro'; import type { PathLike } from 'fs'; import fs from 'fs/promises'; +import esbuild from 'esbuild'; +import { fileURLToPath } from 'url'; const writeJson = (path: PathLike, data: any) => fs.writeFile(path, JSON.stringify(data), { encoding: 'utf-8' }); +const ENTRYFILE = '__astro_entry'; + export function getAdapter(): AstroAdapter { return { name: '@astrojs/vercel', @@ -13,8 +17,6 @@ export function getAdapter(): AstroAdapter { } export default function vercel(): AstroIntegration { - let entryFile: string; - return { name: '@astrojs/vercel', hooks: { @@ -26,15 +28,28 @@ export default function vercel(): AstroIntegration { setAdapter(getAdapter()); }, 'astro:build:start': async ({ buildConfig, config }) => { - entryFile = buildConfig.serverEntry; + buildConfig.serverEntry = `${ENTRYFILE}.mjs`; buildConfig.client = new URL('./static/', config.dist); - buildConfig.server = new URL('./functions/', config.dist); + buildConfig.server = new URL('./server/pages/', config.dist); }, 'astro:build:done': async ({ dir, routes }) => { - await writeJson(new URL(`./functions/package.json`, dir), { - type: 'commonjs', + const pagesDir = new URL('./server/pages/', dir); + + // FIX: Remove these two line before merging + await fs.mkdir(pagesDir, { recursive: true }); + await fs.rename(new URL(`./${ENTRYFILE}.mjs`, dir), new URL(`./${ENTRYFILE}.mjs`, pagesDir)); + + await esbuild.build({ + entryPoints: [fileURLToPath(new URL(`./${ENTRYFILE}.mjs`, pagesDir))], + outfile: fileURLToPath(new URL(`./${ENTRYFILE}.js`, pagesDir)), + bundle: true, + format: 'cjs', + platform: 'node', + target: 'node14', }); + await fs.rm(new URL(`./${ENTRYFILE}.mjs`, pagesDir)); + // Routes Manifest // https://vercel.com/docs/file-system-api#configuration/routes await writeJson(new URL(`./routes-manifest.json`, dir), { @@ -51,21 +66,9 @@ export default function vercel(): AstroIntegration { // ], rewrites: routes.map((route) => ({ source: route.pathname, - destination: '/__astro_entry', + destination: `/${ENTRYFILE}`, })), }); - - // Functions Manifest - // https://vercel.com/docs/file-system-api#configuration/functions - await writeJson(new URL(`./functions-manifest.json`, dir), { - version: 1, - pages: { - __astro_entry: { - runtime: 'nodejs14', - handler: `functions/${entryFile}`, - }, - }, - }); }, }, }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b973649e5f..72b3b48ba6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1364,8 +1364,10 @@ importers: '@astrojs/webapi': ^0.11.0 astro: workspace:* astro-scripts: workspace:* + esbuild: 0.14.25 dependencies: '@astrojs/webapi': link:../../webapi + esbuild: 0.14.25 devDependencies: astro: link:../../astro astro-scripts: link:../../../scripts