diff options
author | 2024-08-29 19:58:06 +0200 | |
---|---|---|
committer | 2024-08-29 19:58:06 +0200 | |
commit | b2d097b51e1d8845d955cee4d1e8838f96975638 (patch) | |
tree | 1593bbc71f60058579ed35219adf53b68ee3a24b /packages/integrations/node/src/index.ts | |
parent | 93a1db68cd9cf3bb2a4d9f7a8af13cbd881eb701 (diff) | |
parent | 7897044c1d95ef905a4835dafe75d5b5b323b5bf (diff) | |
download | astro-b2d097b51e1d8845d955cee4d1e8838f96975638.tar.gz astro-b2d097b51e1d8845d955cee4d1e8838f96975638.tar.zst astro-b2d097b51e1d8845d955cee4d1e8838f96975638.zip |
Merge `vercel` and `node` into main #366
Diffstat (limited to 'packages/integrations/node/src/index.ts')
-rw-r--r-- | packages/integrations/node/src/index.ts | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/packages/integrations/node/src/index.ts b/packages/integrations/node/src/index.ts new file mode 100644 index 000000000..eb3c98a9b --- /dev/null +++ b/packages/integrations/node/src/index.ts @@ -0,0 +1,82 @@ +import type { AstroAdapter, AstroIntegration } from 'astro'; +import { AstroError } from 'astro/errors'; +import type { Options, UserOptions } from './types.js'; + +export function getAdapter(options: Options): AstroAdapter { + return { + name: '@astrojs/node', + serverEntrypoint: '@astrojs/node/server.js', + previewEntrypoint: '@astrojs/node/preview.js', + exports: ['handler', 'startServer', 'options'], + args: options, + supportedAstroFeatures: { + hybridOutput: 'stable', + staticOutput: 'stable', + serverOutput: 'stable', + assets: { + supportKind: 'stable', + isSharpCompatible: true, + isSquooshCompatible: true, + }, + i18nDomains: 'experimental', + envGetSecret: 'experimental', + }, + }; +} + +// TODO: remove once we don't use a TLA anymore +async function shouldExternalizeAstroEnvSetup() { + try { + await import('astro/env/setup'); + return false; + } catch { + return true; + } +} + +export default function createIntegration(userOptions: UserOptions): AstroIntegration { + if (!userOptions?.mode) { + throw new AstroError(`Setting the 'mode' option is required.`); + } + + let _options: Options; + return { + name: '@astrojs/node', + hooks: { + 'astro:config:setup': async ({ updateConfig, config }) => { + updateConfig({ + image: { + endpoint: config.image.endpoint ?? 'astro/assets/endpoint/node', + }, + vite: { + ssr: { + noExternal: ['@astrojs/node'], + ...((await shouldExternalizeAstroEnvSetup()) + ? { + external: ['astro/env/setup'], + } + : {}), + }, + }, + }); + }, + 'astro:config:done': ({ setAdapter, config, logger }) => { + _options = { + ...userOptions, + client: config.build.client?.toString(), + server: config.build.server?.toString(), + host: config.server.host, + port: config.server.port, + assets: config.build.assets, + }; + setAdapter(getAdapter(_options)); + + if (config.output === 'static') { + logger.warn( + `\`output: "server"\` or \`output: "hybrid"\` is required to use this adapter.` + ); + } + }, + }, + }; +} |