2022-03-24 11:26:25 +00:00
|
|
|
import type { AstroAdapter, AstroIntegration } from 'astro';
|
2022-10-12 21:25:51 +00:00
|
|
|
import type { Options, UserOptions } from './types';
|
2022-03-24 11:26:25 +00:00
|
|
|
|
2022-10-12 21:25:51 +00:00
|
|
|
export function getAdapter(options: Options): AstroAdapter {
|
2022-03-24 11:26:25 +00:00
|
|
|
return {
|
|
|
|
name: '@astrojs/node',
|
|
|
|
serverEntrypoint: '@astrojs/node/server.js',
|
2022-10-12 21:25:51 +00:00
|
|
|
previewEntrypoint: '@astrojs/node/preview.js',
|
2023-02-03 00:10:16 +00:00
|
|
|
exports: ['handler', 'startServer'],
|
2022-10-12 21:27:56 +00:00
|
|
|
args: options,
|
2022-03-24 11:26:25 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-12 21:25:51 +00:00
|
|
|
export default function createIntegration(userOptions: UserOptions): AstroIntegration {
|
2022-10-12 21:27:56 +00:00
|
|
|
if (!userOptions?.mode) {
|
|
|
|
throw new Error(`[@astrojs/node] Setting the 'mode' option is required.`);
|
2022-10-12 21:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let _options: Options;
|
2022-03-24 11:26:25 +00:00
|
|
|
return {
|
|
|
|
name: '@astrojs/node',
|
|
|
|
hooks: {
|
2022-10-18 15:47:18 +00:00
|
|
|
'astro:config:setup': ({ updateConfig }) => {
|
2022-10-18 15:49:01 +00:00
|
|
|
updateConfig({
|
|
|
|
vite: {
|
|
|
|
ssr: {
|
|
|
|
noExternal: ['@astrojs/node'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2022-07-25 04:18:02 +00:00
|
|
|
'astro:config:done': ({ setAdapter, config }) => {
|
2022-10-12 21:25:51 +00:00
|
|
|
_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:18:02 +00:00
|
|
|
|
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-07-25 04:18:02 +00:00
|
|
|
}
|
2022-04-14 13:52:56 +00:00
|
|
|
},
|
2022-03-24 11:27:15 +00:00
|
|
|
},
|
2022-03-24 11:26:25 +00:00
|
|
|
};
|
|
|
|
}
|