From 4a23b5aaedd69043635711dfdd60dd0d69dd69a8 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 2 Jun 2022 14:21:10 -0400 Subject: [PATCH] provide import.meta.env.SITE when there are private envs (#3498) * provide import.meta.env.SITE when there are private envs * Adds a changeset * Handle destructing of import.meta.env.SITE --- .changeset/nice-cougars-hear.md | 5 +++++ packages/astro/src/vite-plugin-env/index.ts | 1 + packages/astro/test/astro-envs.test.js | 6 ++++++ .../test/fixtures/astro-envs/src/pages/destructured.astro | 3 ++- 4 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 .changeset/nice-cougars-hear.md diff --git a/.changeset/nice-cougars-hear.md b/.changeset/nice-cougars-hear.md new file mode 100644 index 000000000..6d14608db --- /dev/null +++ b/.changeset/nice-cougars-hear.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes use of import.meta.env.SITE diff --git a/packages/astro/src/vite-plugin-env/index.ts b/packages/astro/src/vite-plugin-env/index.ts index 672fe5a0f..71c086865 100644 --- a/packages/astro/src/vite-plugin-env/index.ts +++ b/packages/astro/src/vite-plugin-env/index.ts @@ -78,6 +78,7 @@ export default function envVitePlugin({ if (typeof privateEnv === 'undefined') { privateEnv = getPrivateEnv(config, astroConfig); if (privateEnv) { + privateEnv.SITE = astroConfig.site ? `'${astroConfig.site}'` : 'undefined'; const entries = Object.entries(privateEnv).map(([key, value]) => [ `import.meta.env.${key}`, value, diff --git a/packages/astro/test/astro-envs.test.js b/packages/astro/test/astro-envs.test.js index da2332c9e..5c5f410c4 100644 --- a/packages/astro/test/astro-envs.test.js +++ b/packages/astro/test/astro-envs.test.js @@ -35,6 +35,12 @@ describe('Environment Variables', () => { expect(indexHtml).to.include('http://example.com'); }); + it('does render destructured builtin SITE env', async () => { + let indexHtml = await fixture.readFile('/destructured/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/src/pages/destructured.astro b/packages/astro/test/fixtures/astro-envs/src/pages/destructured.astro index 25fb95954..e85b9bf81 100644 --- a/packages/astro/test/fixtures/astro-envs/src/pages/destructured.astro +++ b/packages/astro/test/fixtures/astro-envs/src/pages/destructured.astro @@ -1,5 +1,6 @@ --- -const { PUBLIC_PLACE, SECRET_PLACE } = import.meta.env; +const { PUBLIC_PLACE, SECRET_PLACE, SITE } = import.meta.env; --- {PUBLIC_PLACE} {SECRET_PLACE} +{SITE}