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:
parent
c6e7355468
commit
4a23b5aaed
4 changed files with 14 additions and 1 deletions
5
.changeset/nice-cougars-hear.md
Normal file
5
.changeset/nice-cougars-hear.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixes use of import.meta.env.SITE
|
|
@ -78,6 +78,7 @@ export default function envVitePlugin({
|
||||||
if (typeof privateEnv === 'undefined') {
|
if (typeof privateEnv === 'undefined') {
|
||||||
privateEnv = getPrivateEnv(config, astroConfig);
|
privateEnv = getPrivateEnv(config, astroConfig);
|
||||||
if (privateEnv) {
|
if (privateEnv) {
|
||||||
|
privateEnv.SITE = astroConfig.site ? `'${astroConfig.site}'` : 'undefined';
|
||||||
const entries = Object.entries(privateEnv).map(([key, value]) => [
|
const entries = Object.entries(privateEnv).map(([key, value]) => [
|
||||||
`import.meta.env.${key}`,
|
`import.meta.env.${key}`,
|
||||||
value,
|
value,
|
||||||
|
|
|
@ -35,6 +35,12 @@ describe('Environment Variables', () => {
|
||||||
expect(indexHtml).to.include('http://example.com');
|
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 () => {
|
it('includes public env in client-side JS', async () => {
|
||||||
let dirs = await fixture.readdir('/');
|
let dirs = await fixture.readdir('/');
|
||||||
let found = false;
|
let found = false;
|
||||||
|
|
|
@ -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>{PUBLIC_PLACE}</environment-variable>
|
||||||
<environment-variable>{SECRET_PLACE}</environment-variable>
|
<environment-variable>{SECRET_PLACE}</environment-variable>
|
||||||
|
<environment-variable>{SITE}</environment-variable>
|
||||||
|
|
Loading…
Add table
Reference in a new issue