2022-04-19 15:22:15 +00:00
|
|
|
import type { AstroAdapter, AstroIntegration, AstroConfig } from 'astro';
|
2022-05-11 15:13:23 +00:00
|
|
|
import { createRedirects } from './shared.js';
|
2022-04-19 15:22:15 +00:00
|
|
|
|
|
|
|
export function getAdapter(): AstroAdapter {
|
|
|
|
return {
|
|
|
|
name: '@astrojs/netlify/functions',
|
|
|
|
serverEntrypoint: '@astrojs/netlify/netlify-functions.js',
|
|
|
|
exports: ['handler'],
|
|
|
|
args: {},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
interface NetlifyFunctionsOptions {
|
|
|
|
dist?: URL;
|
|
|
|
}
|
|
|
|
|
|
|
|
function netlifyFunctions({ dist }: NetlifyFunctionsOptions = {}): AstroIntegration {
|
|
|
|
let _config: AstroConfig;
|
|
|
|
let entryFile: string;
|
|
|
|
return {
|
|
|
|
name: '@astrojs/netlify',
|
|
|
|
hooks: {
|
|
|
|
'astro:config:setup': ({ config }) => {
|
|
|
|
if (dist) {
|
|
|
|
config.outDir = dist;
|
|
|
|
} else {
|
|
|
|
config.outDir = new URL('./netlify/', config.root);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'astro:config:done': ({ config, setAdapter }) => {
|
|
|
|
setAdapter(getAdapter());
|
|
|
|
_config = config;
|
|
|
|
},
|
|
|
|
'astro:build:start': async ({ buildConfig }) => {
|
|
|
|
entryFile = buildConfig.serverEntry.replace(/\.m?js/, '');
|
|
|
|
buildConfig.client = _config.outDir;
|
|
|
|
buildConfig.server = new URL('./functions/', _config.outDir);
|
|
|
|
},
|
|
|
|
'astro:build:done': async ({ routes, dir }) => {
|
2022-05-11 15:14:43 +00:00
|
|
|
await createRedirects(routes, dir, entryFile, false);
|
2022-04-19 15:22:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export { netlifyFunctions, netlifyFunctions as default };
|