diff --git a/.changeset/sixty-trainers-shout.md b/.changeset/sixty-trainers-shout.md new file mode 100644 index 000000000..8838fdede --- /dev/null +++ b/.changeset/sixty-trainers-shout.md @@ -0,0 +1,7 @@ +--- +'@astrojs/netlify': minor +'@astrojs/vercel': minor +'@astrojs/node': minor +--- + +Cleans up `astro:env` support diff --git a/packages/netlify/src/index.ts b/packages/netlify/src/index.ts index 11dfafe51..c48b952af 100644 --- a/packages/netlify/src/index.ts +++ b/packages/netlify/src/index.ts @@ -141,16 +141,6 @@ async function writeNetlifyFrameworkConfig(config: AstroConfig, logger: AstroInt ); } -// 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 interface NetlifyIntegrationConfig { /** * If enabled, On-Demand-Rendered pages are cached for up to a year. @@ -435,11 +425,6 @@ export default function netlifyIntegration( ignored: [fileURLToPath(new URL('./.netlify/**', rootDir))], }, }, - ...((await shouldExternalizeAstroEnvSetup()) - ? { - ssr: { external: ['astro/env/setup'] }, - } - : {}), }, image: { service: { diff --git a/packages/netlify/src/polyfill.ts b/packages/netlify/src/polyfill.ts new file mode 100644 index 000000000..dc00f45d7 --- /dev/null +++ b/packages/netlify/src/polyfill.ts @@ -0,0 +1,3 @@ +import { applyPolyfills } from 'astro/app/node'; + +applyPolyfills(); diff --git a/packages/netlify/src/ssr-function.ts b/packages/netlify/src/ssr-function.ts index 22925ddaa..5ea2e97f1 100644 --- a/packages/netlify/src/ssr-function.ts +++ b/packages/netlify/src/ssr-function.ts @@ -1,15 +1,12 @@ +// Keep at the top +import './polyfill.js'; + import type { Context } from '@netlify/functions'; import type { SSRManifest } from 'astro'; import { App } from 'astro/app'; -import { applyPolyfills } from 'astro/app/node'; - -applyPolyfills(); +import { setGetEnv } from '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 -await import('astro/env/setup') - .then((mod) => mod.setGetEnv((key) => process.env[key])) - .catch(() => {}); +setGetEnv((key) => process.env[key]); export interface Args { middlewareSecret: string; diff --git a/packages/node/src/index.ts b/packages/node/src/index.ts index dbd39e6ea..999033a97 100644 --- a/packages/node/src/index.ts +++ b/packages/node/src/index.ts @@ -27,16 +27,6 @@ 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.`); @@ -54,11 +44,6 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr vite: { ssr: { noExternal: ['@astrojs/node'], - ...((await shouldExternalizeAstroEnvSetup()) - ? { - external: ['astro/env/setup'], - } - : {}), }, }, }); diff --git a/packages/node/src/polyfill.ts b/packages/node/src/polyfill.ts new file mode 100644 index 000000000..dc00f45d7 --- /dev/null +++ b/packages/node/src/polyfill.ts @@ -0,0 +1,3 @@ +import { applyPolyfills } from 'astro/app/node'; + +applyPolyfills(); diff --git a/packages/node/src/server.ts b/packages/node/src/server.ts index 93d75d360..cef262b47 100644 --- a/packages/node/src/server.ts +++ b/packages/node/src/server.ts @@ -1,13 +1,14 @@ +// Keep at the top +import './polyfill.js'; + import type { SSRManifest } from 'astro'; -import { NodeApp, applyPolyfills } from 'astro/app/node'; +import { NodeApp } from 'astro/app/node'; import { setGetEnv } from 'astro/env/setup'; import createMiddleware from './middleware.js'; import { createStandaloneHandler } from './standalone.js'; import startServer from './standalone.js'; import type { Options } from './types.js'; -// This needs to run first because some internals depend on `crypto` -applyPolyfills(); setGetEnv((key) => process.env[key]); export function createExports(manifest: SSRManifest, options: Options) { diff --git a/packages/vercel/src/serverless/adapter.ts b/packages/vercel/src/serverless/adapter.ts index b8cf1f2bc..df4a02357 100644 --- a/packages/vercel/src/serverless/adapter.ts +++ b/packages/vercel/src/serverless/adapter.ts @@ -260,10 +260,7 @@ export default function vercelServerless({ vite: { ...getSpeedInsightsViteConfig(speedInsights?.enabled), ssr: { - external: [ - '@vercel/nft', - ...((await shouldExternalizeAstroEnvSetup()) ? ['astro/env/setup'] : []), - ], + external: ['@vercel/nft'], }, }, ...getAstroImageConfig( @@ -426,16 +423,6 @@ 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 = {}; diff --git a/packages/vercel/src/serverless/entrypoint.ts b/packages/vercel/src/serverless/entrypoint.ts index e2c146ed8..01279a2b4 100644 --- a/packages/vercel/src/serverless/entrypoint.ts +++ b/packages/vercel/src/serverless/entrypoint.ts @@ -1,6 +1,9 @@ +// Keep at the top +import './polyfill.js'; + import type { IncomingMessage, ServerResponse } from 'node:http'; import type { SSRManifest } from 'astro'; -import { NodeApp, applyPolyfills } from 'astro/app/node'; +import { NodeApp } from 'astro/app/node'; import { setGetEnv } from 'astro/env/setup'; import { ASTRO_LOCALS_HEADER, @@ -9,8 +12,6 @@ import { ASTRO_PATH_PARAM, } from './adapter.js'; -// Run polyfills immediately so any dependent code can use the globals -applyPolyfills(); setGetEnv((key) => process.env[key]); export const createExports = ( diff --git a/packages/vercel/src/serverless/polyfill.ts b/packages/vercel/src/serverless/polyfill.ts new file mode 100644 index 000000000..dc00f45d7 --- /dev/null +++ b/packages/vercel/src/serverless/polyfill.ts @@ -0,0 +1,3 @@ +import { applyPolyfills } from 'astro/app/node'; + +applyPolyfills();