From c664bc923cb32772e463f621005481cc9d6a8156 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 1 Jun 2022 11:51:40 -0400 Subject: [PATCH] provide import.meta.env.SITE when there are private envs --- packages/astro/src/vite-plugin-env/index.ts | 1 + packages/astro/test/astro-envs.test.js | 5 +++++ packages/astro/test/fixtures/astro-envs/astro.config.mjs | 1 + .../astro/test/fixtures/astro-envs/src/pages/index.astro | 1 + 4 files changed, 8 insertions(+) diff --git a/packages/astro/src/vite-plugin-env/index.ts b/packages/astro/src/vite-plugin-env/index.ts index a510f2eee8ca..672fe5a0f0da 100644 --- a/packages/astro/src/vite-plugin-env/index.ts +++ b/packages/astro/src/vite-plugin-env/index.ts @@ -85,6 +85,7 @@ export default function envVitePlugin({ replacements = Object.fromEntries(entries); // These additional replacements are needed to match Vite replacements = Object.assign(replacements, { + 'import.meta.env.SITE': astroConfig.site ? `'${astroConfig.site}'` : 'undefined', // This catches destructed `import.meta.env` calls, // BUT we only want to inject private keys referenced in the file. // We overwrite this value on a per-file basis. diff --git a/packages/astro/test/astro-envs.test.js b/packages/astro/test/astro-envs.test.js index f44b007c8592..da2332c9eb07 100644 --- a/packages/astro/test/astro-envs.test.js +++ b/packages/astro/test/astro-envs.test.js @@ -30,6 +30,11 @@ describe('Environment Variables', () => { expect(indexHtml).to.include('BLUE_BAYOU'); }); + it('does render builtin SITE env', async () => { + let indexHtml = await fixture.readFile('/index.html'); + expect(indexHtml).to.include('http://example.com'); + }); + it('includes public env in client-side JS', async () => { let dirs = await fixture.readdir('/'); let found = false; diff --git a/packages/astro/test/fixtures/astro-envs/astro.config.mjs b/packages/astro/test/fixtures/astro-envs/astro.config.mjs index 88193061246b..7bb1b85f1d15 100644 --- a/packages/astro/test/fixtures/astro-envs/astro.config.mjs +++ b/packages/astro/test/fixtures/astro-envs/astro.config.mjs @@ -3,5 +3,6 @@ import vue from '@astrojs/vue'; // https://astro.build/config export default defineConfig({ + site: 'http://example.com', integrations: [vue()], }); diff --git a/packages/astro/test/fixtures/astro-envs/src/pages/index.astro b/packages/astro/test/fixtures/astro-envs/src/pages/index.astro index f71c11db75dc..3c265dbef736 100644 --- a/packages/astro/test/fixtures/astro-envs/src/pages/index.astro +++ b/packages/astro/test/fixtures/astro-envs/src/pages/index.astro @@ -3,4 +3,5 @@ import Client from '../components/Client.vue'; --- {import.meta.env.PUBLIC_PLACE} {import.meta.env.SECRET_PLACE} +{import.meta.env.SITE}