astro/packages/integrations/node/src/index.ts

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

49 lines
1.3 KiB
TypeScript
Raw Normal View History

import type { AstroAdapter, AstroIntegration } from 'astro';
import type { Options, UserOptions } from './types';
export function getAdapter(options: Options): AstroAdapter {
return {
name: '@astrojs/node',
serverEntrypoint: '@astrojs/node/server.js',
previewEntrypoint: '@astrojs/node/preview.js',
exports: ['handler'],
args: options
};
}
export default function createIntegration(userOptions: UserOptions): AstroIntegration {
if(!userOptions?.mode) {
throw new Error(`[@astrojs/node] Setting the 'mode' option is required.`)
}
let needsBuildConfig = false;
let _options: Options;
return {
name: '@astrojs/node',
hooks: {
'astro:config:done': ({ setAdapter, config }) => {
needsBuildConfig = !config.build?.server;
_options = {
...userOptions,
client: config.build.client?.toString(),
server: config.build.server?.toString(),
host: config.server.host,
port: config.server.port,
};
setAdapter(getAdapter(_options));
2022-07-25 04:20:38 +00:00
if (config.output === 'static') {
2022-08-29 09:02:44 +00:00
console.warn(`[@astrojs/node] \`output: "server"\` is required to use this adapter.`);
}
2022-04-14 13:52:56 +00:00
},
'astro:build:start': ({ buildConfig }) => {
// Backwards compat
if(needsBuildConfig) {
_options.client = buildConfig.client.toString();
_options.server = buildConfig.server.toString();
}
}
2022-03-24 11:27:15 +00:00
},
};
}