2022-06-06 16:49:53 +00:00
|
|
|
import type { AstroAdapter, AstroConfig, AstroIntegration } from 'astro';
|
2022-06-15 19:49:09 +00:00
|
|
|
import type { Args } from './netlify-functions.js';
|
2022-06-15 19:50:36 +00:00
|
|
|
import { createRedirects } from './shared.js';
|
2022-04-19 15:22:15 +00:00
|
|
|
|
2022-06-15 19:49:09 +00:00
|
|
|
export function getAdapter(args: Args = {}): AstroAdapter {
|
2022-04-19 15:22:15 +00:00
|
|
|
return {
|
|
|
|
name: '@astrojs/netlify/functions',
|
|
|
|
serverEntrypoint: '@astrojs/netlify/netlify-functions.js',
|
|
|
|
exports: ['handler'],
|
2022-06-15 19:49:09 +00:00
|
|
|
args,
|
2022-04-19 15:22:15 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
interface NetlifyFunctionsOptions {
|
|
|
|
dist?: URL;
|
2022-06-15 19:49:09 +00:00
|
|
|
binaryMediaTypes?: string[];
|
2022-04-19 15:22:15 +00:00
|
|
|
}
|
|
|
|
|
2022-06-15 19:50:36 +00:00
|
|
|
function netlifyFunctions({
|
|
|
|
dist,
|
|
|
|
binaryMediaTypes,
|
|
|
|
}: NetlifyFunctionsOptions = {}): AstroIntegration {
|
2022-04-19 15:22:15 +00:00
|
|
|
let _config: AstroConfig;
|
|
|
|
let entryFile: string;
|
|
|
|
return {
|
|
|
|
name: '@astrojs/netlify',
|
|
|
|
hooks: {
|
|
|
|
'astro:config:setup': ({ config }) => {
|
|
|
|
if (dist) {
|
|
|
|
config.outDir = dist;
|
|
|
|
} else {
|
2022-05-16 18:44:21 +00:00
|
|
|
config.outDir = new URL('./dist/', config.root);
|
2022-04-19 15:22:15 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'astro:config:done': ({ config, setAdapter }) => {
|
2022-06-15 19:49:09 +00:00
|
|
|
setAdapter(getAdapter({ binaryMediaTypes }));
|
2022-04-19 15:22:15 +00:00
|
|
|
_config = config;
|
2022-07-25 04:18:02 +00:00
|
|
|
|
2022-07-25 04:20:38 +00:00
|
|
|
if (config.output === 'static') {
|
2022-07-25 04:18:02 +00:00
|
|
|
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.`
|
|
|
|
);
|
2022-07-25 04:18:02 +00:00
|
|
|
}
|
2022-04-19 15:22:15 +00:00
|
|
|
},
|
|
|
|
'astro:build:start': async ({ buildConfig }) => {
|
|
|
|
entryFile = buildConfig.serverEntry.replace(/\.m?js/, '');
|
|
|
|
buildConfig.client = _config.outDir;
|
2022-05-16 18:44:21 +00:00
|
|
|
buildConfig.server = new URL('./.netlify/functions-internal/', _config.root);
|
2022-04-19 15:22:15 +00:00
|
|
|
},
|
|
|
|
'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 };
|