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
This commit is contained in:
Matthew Phillips 2022-06-02 14:21:10 -04:00 committed by GitHub
parent c6e7355468
commit 4a23b5aaed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes use of import.meta.env.SITE

View file

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

View file

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

View file

@ -1,5 +1,6 @@
---
const { PUBLIC_PLACE, SECRET_PLACE } = import.meta.env;
const { PUBLIC_PLACE, SECRET_PLACE, SITE } = import.meta.env;
---
<environment-variable>{PUBLIC_PLACE}</environment-variable>
<environment-variable>{SECRET_PLACE}</environment-variable>
<environment-variable>{SITE}</environment-variable>