mirror of
https://github.com/withastro/astro.git
synced 2025-01-06 22:10:10 -05:00
Build to CommonJS
This commit is contained in:
parent
09e8b8e502
commit
e890b528c4
3 changed files with 26 additions and 20 deletions
|
@ -23,7 +23,8 @@
|
||||||
"dev": "astro-scripts dev \"src/**/*.ts\""
|
"dev": "astro-scripts dev \"src/**/*.ts\""
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@astrojs/webapi": "^0.11.0"
|
"@astrojs/webapi": "^0.11.0",
|
||||||
|
"esbuild": "0.14.25"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"astro": "workspace:*",
|
"astro": "workspace:*",
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
import type { AstroAdapter, AstroIntegration } from 'astro';
|
import type { AstroAdapter, AstroIntegration } from 'astro';
|
||||||
import type { PathLike } from 'fs';
|
import type { PathLike } from 'fs';
|
||||||
import fs from 'fs/promises';
|
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 writeJson = (path: PathLike, data: any) => fs.writeFile(path, JSON.stringify(data), { encoding: 'utf-8' });
|
||||||
|
|
||||||
|
const ENTRYFILE = '__astro_entry';
|
||||||
|
|
||||||
export function getAdapter(): AstroAdapter {
|
export function getAdapter(): AstroAdapter {
|
||||||
return {
|
return {
|
||||||
name: '@astrojs/vercel',
|
name: '@astrojs/vercel',
|
||||||
|
@ -13,8 +17,6 @@ export function getAdapter(): AstroAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function vercel(): AstroIntegration {
|
export default function vercel(): AstroIntegration {
|
||||||
let entryFile: string;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: '@astrojs/vercel',
|
name: '@astrojs/vercel',
|
||||||
hooks: {
|
hooks: {
|
||||||
|
@ -26,15 +28,28 @@ export default function vercel(): AstroIntegration {
|
||||||
setAdapter(getAdapter());
|
setAdapter(getAdapter());
|
||||||
},
|
},
|
||||||
'astro:build:start': async ({ buildConfig, config }) => {
|
'astro:build:start': async ({ buildConfig, config }) => {
|
||||||
entryFile = buildConfig.serverEntry;
|
buildConfig.serverEntry = `${ENTRYFILE}.mjs`;
|
||||||
buildConfig.client = new URL('./static/', config.dist);
|
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 }) => {
|
'astro:build:done': async ({ dir, routes }) => {
|
||||||
await writeJson(new URL(`./functions/package.json`, dir), {
|
const pagesDir = new URL('./server/pages/', dir);
|
||||||
type: 'commonjs',
|
|
||||||
|
// 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
|
// Routes Manifest
|
||||||
// https://vercel.com/docs/file-system-api#configuration/routes
|
// https://vercel.com/docs/file-system-api#configuration/routes
|
||||||
await writeJson(new URL(`./routes-manifest.json`, dir), {
|
await writeJson(new URL(`./routes-manifest.json`, dir), {
|
||||||
|
@ -51,21 +66,9 @@ export default function vercel(): AstroIntegration {
|
||||||
// ],
|
// ],
|
||||||
rewrites: routes.map((route) => ({
|
rewrites: routes.map((route) => ({
|
||||||
source: route.pathname,
|
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}`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1364,8 +1364,10 @@ importers:
|
||||||
'@astrojs/webapi': ^0.11.0
|
'@astrojs/webapi': ^0.11.0
|
||||||
astro: workspace:*
|
astro: workspace:*
|
||||||
astro-scripts: workspace:*
|
astro-scripts: workspace:*
|
||||||
|
esbuild: 0.14.25
|
||||||
dependencies:
|
dependencies:
|
||||||
'@astrojs/webapi': link:../../webapi
|
'@astrojs/webapi': link:../../webapi
|
||||||
|
esbuild: 0.14.25
|
||||||
devDependencies:
|
devDependencies:
|
||||||
astro: link:../../astro
|
astro: link:../../astro
|
||||||
astro-scripts: link:../../../scripts
|
astro-scripts: link:../../../scripts
|
||||||
|
|
Loading…
Reference in a new issue