From 18172a6750a55fde0123cbdd878e0b86bb6e9d51 Mon Sep 17 00:00:00 2001 From: Jonathan Neal Date: Mon, 8 Nov 2021 06:28:15 -0500 Subject: [PATCH] Support PUBLIC_ environment variables (#1750) * Support PUBLIC_ environment variables * Remove unnecessary Test component --- packages/astro/src/core/create-vite.ts | 1 + packages/astro/test/astro-envs.test.js | 23 +++++++++++++++++++ packages/astro/test/fixtures/astro-envs/.env | 2 ++ .../astro/test/fixtures/astro-envs/.gitignore | 1 + .../fixtures/astro-envs/src/pages/index.astro | 2 ++ 5 files changed, 29 insertions(+) create mode 100644 packages/astro/test/astro-envs.test.js create mode 100644 packages/astro/test/fixtures/astro-envs/.env create mode 100644 packages/astro/test/fixtures/astro-envs/.gitignore create mode 100644 packages/astro/test/fixtures/astro-envs/src/pages/index.astro diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 7b6653980..b49f664dc 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -55,6 +55,7 @@ export async function createVite(inlineConfig: ViteConfigWithSSR, { astroConfig, ], publicDir: fileURLToPath(astroConfig.public), root: fileURLToPath(astroConfig.projectRoot), + envPrefix: 'PUBLIC_', server: { force: true, // force dependency rebuild (TODO: enabled only while next is unstable; eventually only call in "production" mode?) hmr: process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'production' ? false : undefined, // disable HMR for test diff --git a/packages/astro/test/astro-envs.test.js b/packages/astro/test/astro-envs.test.js new file mode 100644 index 000000000..d8c7a3a8c --- /dev/null +++ b/packages/astro/test/astro-envs.test.js @@ -0,0 +1,23 @@ +import { expect } from 'chai'; +import { loadFixture } from './test-utils.js'; + +describe('Environment Variables', () => { + let fixture; + + before(async () => { + fixture = await loadFixture({ projectRoot: './fixtures/astro-envs/' }); + + await fixture.build(); + }); + + it('builds without throwing', async () => { + expect(true).to.equal(true); + }); + + it('does render public env, does not render private env', async () => { + let indexHtml = await fixture.readFile('/index.html'); + + expect(indexHtml).to.not.include('CLUB_33'); + expect(indexHtml).to.include('BLUE_BAYOU'); + }); +}); diff --git a/packages/astro/test/fixtures/astro-envs/.env b/packages/astro/test/fixtures/astro-envs/.env new file mode 100644 index 000000000..e0587fe3f --- /dev/null +++ b/packages/astro/test/fixtures/astro-envs/.env @@ -0,0 +1,2 @@ +SECRET_PLACE=CLUB_33 +PUBLIC_PLACE=BLUE_BAYOU diff --git a/packages/astro/test/fixtures/astro-envs/.gitignore b/packages/astro/test/fixtures/astro-envs/.gitignore new file mode 100644 index 000000000..8e0776e8d --- /dev/null +++ b/packages/astro/test/fixtures/astro-envs/.gitignore @@ -0,0 +1 @@ +!.env diff --git a/packages/astro/test/fixtures/astro-envs/src/pages/index.astro b/packages/astro/test/fixtures/astro-envs/src/pages/index.astro new file mode 100644 index 000000000..9dc6a380e --- /dev/null +++ b/packages/astro/test/fixtures/astro-envs/src/pages/index.astro @@ -0,0 +1,2 @@ +{import.meta.env.PUBLIC_PLACE} +{import.meta.env.SECRET_PLACE}