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.

67 lines
1.9 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;
let needsBuildConfig = false;
return {
name: '@astrojs/netlify',
hooks: {
'astro:config:setup': ({ config, updateConfig }) => {
needsBuildConfig = !config.build.client;
const outDir = dist ?? new URL('./dist/', config.root);
updateConfig({
outDir,
build: {
client: outDir,
server: new URL('./.netlify/functions-internal/', config.root),
}
});
},
'astro:config:done': ({ config, setAdapter }) => {
setAdapter(getAdapter({ binaryMediaTypes }));
_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:start': ({ buildConfig }) => {
if(needsBuildConfig) {
buildConfig.client = _config.outDir;
buildConfig.server = new URL('./.netlify/functions-internal/', _config.root);
entryFile = buildConfig.serverEntry.replace(/\.m?js/, '');
}
},
'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 };