Support PUBLIC_ environment variables (#1750)

* Support PUBLIC_ environment variables

* Remove unnecessary Test component
This commit is contained in:
Jonathan Neal 2021-11-08 06:28:15 -05:00 committed by GitHub
parent a26f7a2088
commit 18172a6750
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 0 deletions

View file

@ -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

View file

@ -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');
});
});

View file

@ -0,0 +1,2 @@
SECRET_PLACE=CLUB_33
PUBLIC_PLACE=BLUE_BAYOU

View file

@ -0,0 +1 @@
!.env

View file

@ -0,0 +1,2 @@
<environment-variable>{import.meta.env.PUBLIC_PLACE}</environment-variable>
<environment-variable>{import.meta.env.SECRET_PLACE}</environment-variable>