0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Build to CommonJS

This commit is contained in:
JuanM04 2022-03-30 23:08:39 -03:00
parent 09e8b8e502
commit e890b528c4
No known key found for this signature in database
GPG key ID: 0171B712E406271A
3 changed files with 26 additions and 20 deletions

View file

@ -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:*",

View file

@ -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}`,
},
},
});
},
},
};

View file

@ -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