astro/packages/integrations/netlify/src/integration-functions.ts

61 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-06-06 16:49:53 +00:00
import type { AstroAdapter, AstroConfig, AstroIntegration } from 'astro';
import type { Args } from './netlify-functions.js';
2022-06-15 19:50:36 +00:00
import { createRedirects } from './shared.js';
export function getAdapter(args: Args = {}): AstroAdapter {
return {
name: '@astrojs/netlify/functions',
serverEntrypoint: '@astrojs/netlify/netlify-functions.js',
exports: ['handler'],
args,
};
}
interface NetlifyFunctionsOptions {
dist?: URL;
builders?: boolean;
binaryMediaTypes?: string[];
}
2022-06-15 19:50:36 +00:00
function netlifyFunctions({
dist,
builders,
2022-06-15 19:50:36 +00:00
binaryMediaTypes,
}: NetlifyFunctionsOptions = {}): AstroIntegration {
let _config: AstroConfig;
let entryFile: string;
return {
name: '@astrojs/netlify',
hooks: {
'astro:config:setup': ({ config, updateConfig }) => {
const outDir = dist ?? new URL('./dist/', config.root);
updateConfig({
outDir,
build: {
client: outDir,
server: new URL('./.netlify/functions-internal/', config.root),
2022-10-12 21:27:56 +00:00
},
});
},
'astro:config:done': ({ config, setAdapter }) => {
setAdapter(getAdapter({ binaryMediaTypes, builders }));
_config = config;
entryFile = config.build.serverEntry.replace(/\.m?js/, '');
2022-07-25 04:20:38 +00:00
if (config.output === 'static') {
console.warn(`[@astrojs/netlify] \`output: "server"\` is required to use this adapter.`);
2022-07-25 04:20:38 +00:00
console.warn(
`[@astrojs/netlify] Otherwise, this adapter is not required to deploy a static site to Netlify.`
);
}
},
'astro:build:done': async ({ routes, dir }) => {
const type = builders ? 'builders' : 'functions'
await createRedirects(_config, routes, dir, entryFile, type);
},
},
};
}
export { netlifyFunctions, netlifyFunctions as default };