From b0e6f8627b4025ad3116dc933c7d2b6cd1367a4e Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Thu, 20 Jun 2024 15:28:17 +0200 Subject: [PATCH 1/2] fix: astro:env getSecret --- .changeset/small-sloths-yawn.md | 6 ++++++ packages/integrations/node/src/index.ts | 5 +++++ packages/integrations/node/src/server.ts | 7 ++----- packages/integrations/vercel/src/serverless/adapter.ts | 5 +++++ packages/integrations/vercel/src/serverless/entrypoint.ts | 7 ++----- 5 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 .changeset/small-sloths-yawn.md diff --git a/.changeset/small-sloths-yawn.md b/.changeset/small-sloths-yawn.md new file mode 100644 index 000000000000..652bce3fd8fc --- /dev/null +++ b/.changeset/small-sloths-yawn.md @@ -0,0 +1,6 @@ +--- +'@astrojs/vercel': patch +'@astrojs/node': patch +--- + +Fixes `astro:env` getSecret compatibility diff --git a/packages/integrations/node/src/index.ts b/packages/integrations/node/src/index.ts index e11576990e98..9df22c44ae54 100644 --- a/packages/integrations/node/src/index.ts +++ b/packages/integrations/node/src/index.ts @@ -42,6 +42,11 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr ssr: { noExternal: ['@astrojs/node'], }, + build: { + rollupOptions: { + external: ['astro/env/setup'], + }, + }, }, }); }, diff --git a/packages/integrations/node/src/server.ts b/packages/integrations/node/src/server.ts index febace938f77..e5b503292d40 100644 --- a/packages/integrations/node/src/server.ts +++ b/packages/integrations/node/src/server.ts @@ -5,13 +5,10 @@ import { createStandaloneHandler } from './standalone.js'; import startServer from './standalone.js'; import type { Options } from './types.js'; -type EnvSetupModule = typeof import('astro/env/setup'); - // Won't throw if the virtual module is not available because it's not supported in // the users's astro version or if astro:env is not enabled in the project -const setupModule = 'astro/env/setup'; -await import(/* @vite-ignore */ setupModule) - .then((mod: EnvSetupModule) => mod.setGetEnv((key) => process.env[key])) +await import('astro/env/setup') + .then((mod) => mod.setGetEnv((key) => process.env[key])) .catch(() => {}); applyPolyfills(); diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts index 4481275129ff..f200254dc497 100644 --- a/packages/integrations/vercel/src/serverless/adapter.ts +++ b/packages/integrations/vercel/src/serverless/adapter.ts @@ -265,6 +265,11 @@ export default function vercelServerless({ ssr: { external: ['@vercel/nft'], }, + build: { + rollupOptions: { + external: ['astro/env/setup'], + }, + }, }, ...getAstroImageConfig( imageService, diff --git a/packages/integrations/vercel/src/serverless/entrypoint.ts b/packages/integrations/vercel/src/serverless/entrypoint.ts index 1e0b75d649ad..11d3c3a91e1d 100644 --- a/packages/integrations/vercel/src/serverless/entrypoint.ts +++ b/packages/integrations/vercel/src/serverless/entrypoint.ts @@ -8,13 +8,10 @@ import { ASTRO_PATH_PARAM, } from './adapter.js'; -type EnvSetupModule = typeof import('astro/env/setup'); - // Won't throw if the virtual module is not available because it's not supported in // the users's astro version or if astro:env is not enabled in the project -const setupModule = 'astro/env/setup'; -await import(/* @vite-ignore */ setupModule) - .then((mod: EnvSetupModule) => mod.setGetEnv((key) => process.env[key])) +await import('astro/env/setup') + .then((mod) => mod.setGetEnv((key) => process.env[key])) .catch(() => {}); applyPolyfills(); From 381cbfabe5120fae36444d325c1d904e9a29e221 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Thu, 20 Jun 2024 15:39:28 +0200 Subject: [PATCH 2/2] feat: address reviews --- packages/integrations/node/src/index.ts | 22 ++++++++++++++----- .../vercel/src/serverless/adapter.ts | 20 ++++++++++++----- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/packages/integrations/node/src/index.ts b/packages/integrations/node/src/index.ts index 9df22c44ae54..eb3c98a9b716 100644 --- a/packages/integrations/node/src/index.ts +++ b/packages/integrations/node/src/index.ts @@ -24,6 +24,16 @@ export function getAdapter(options: Options): AstroAdapter { }; } +// 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.`); @@ -33,7 +43,7 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr return { name: '@astrojs/node', hooks: { - 'astro:config:setup': ({ updateConfig, config }) => { + 'astro:config:setup': async ({ updateConfig, config }) => { updateConfig({ image: { endpoint: config.image.endpoint ?? 'astro/assets/endpoint/node', @@ -41,11 +51,11 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr vite: { ssr: { noExternal: ['@astrojs/node'], - }, - build: { - rollupOptions: { - external: ['astro/env/setup'], - }, + ...((await shouldExternalizeAstroEnvSetup()) + ? { + external: ['astro/env/setup'], + } + : {}), }, }, }); diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts index f200254dc497..aade4715081a 100644 --- a/packages/integrations/vercel/src/serverless/adapter.ts +++ b/packages/integrations/vercel/src/serverless/adapter.ts @@ -263,12 +263,10 @@ export default function vercelServerless({ vite: { ...getSpeedInsightsViteConfig(speedInsights?.enabled), ssr: { - external: ['@vercel/nft'], - }, - build: { - rollupOptions: { - external: ['astro/env/setup'], - }, + external: [ + '@vercel/nft', + ...((await shouldExternalizeAstroEnvSetup()) ? ['astro/env/setup'] : []), + ], }, }, ...getAstroImageConfig( @@ -447,6 +445,16 @@ export default function vercelServerless({ type Runtime = `nodejs${string}.x`; +// TODO: remove once we don't use a TLA anymore +async function shouldExternalizeAstroEnvSetup() { + try { + await import('astro/env/setup'); + return false; + } catch { + return true; + } +} + class VercelBuilder { readonly NTF_CACHE = {};