Skip to content

Commit

Permalink
provide import.meta.env.SITE when there are private envs
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp committed Jun 1, 2022
1 parent 9d3cab4 commit c664bc9
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/astro/src/vite-plugin-env/index.ts
Expand Up @@ -85,6 +85,7 @@ export default function envVitePlugin({
replacements = Object.fromEntries(entries);
// These additional replacements are needed to match Vite
replacements = Object.assign(replacements, {
'import.meta.env.SITE': astroConfig.site ? `'${astroConfig.site}'` : 'undefined',
// This catches destructed `import.meta.env` calls,
// BUT we only want to inject private keys referenced in the file.
// We overwrite this value on a per-file basis.
Expand Down
5 changes: 5 additions & 0 deletions packages/astro/test/astro-envs.test.js
Expand Up @@ -30,6 +30,11 @@ describe('Environment Variables', () => {
expect(indexHtml).to.include('BLUE_BAYOU');
});

it('does render builtin SITE env', async () => {
let indexHtml = await fixture.readFile('/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;
Expand Down
1 change: 1 addition & 0 deletions packages/astro/test/fixtures/astro-envs/astro.config.mjs
Expand Up @@ -3,5 +3,6 @@ import vue from '@astrojs/vue';

// https://astro.build/config
export default defineConfig({
site: 'http://example.com',
integrations: [vue()],
});
Expand Up @@ -3,4 +3,5 @@ import Client from '../components/Client.vue';
---
<environment-variable>{import.meta.env.PUBLIC_PLACE}</environment-variable>
<environment-variable>{import.meta.env.SECRET_PLACE}</environment-variable>
<environment-variable>{import.meta.env.SITE}</environment-variable>
<Client client:load />

0 comments on commit c664bc9

Please sign in to comment.