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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

59 lines
1.6 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;
binaryMediaTypes?: string[];
}
2022-06-15 19:50:36 +00:00
function netlifyFunctions({
dist,
binaryMediaTypes,
}: 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('./dist/', config.root);
}
},
'astro:config:done': ({ config, setAdapter }) => {
setAdapter(getAdapter({ binaryMediaTypes }));
_config = config;
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:start': async ({ buildConfig }) => {
entryFile = buildConfig.serverEntry.replace(/\.m?js/, '');
buildConfig.client = _config.outDir;
buildConfig.server = new URL('./.netlify/functions-internal/', _config.root);
},
'astro:build:done': async ({ routes, dir }) => {
2022-05-11 15:14:43 +00:00
await createRedirects(routes, dir, entryFile, false);
},
},
};
}
export { netlifyFunctions, netlifyFunctions as default };