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: {
|
2022-10-12 21:25:51 +00:00
|
|
|
'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
|
|
|
},
|
2022-10-12 21:25:51 +00:00
|
|
|
});
|
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-10-12 21:25:51 +00:00
|
|
|
entryFile = config.build.serverEntry.replace(/\.m?js/, '');
|
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: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 };
|