Skip to content

Commit 627aec3

Browse files
authoredJan 17, 2025··
fix: do not inject env vars into non-source files (#13001)
1 parent 78fd73a commit 627aec3

File tree

10 files changed

+96
-2
lines changed

10 files changed

+96
-2
lines changed
 

‎.changeset/warm-pandas-lick.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes a bug that caused Astro to attempt to inject environment variables into non-source files, causing performance problems and broken builds

‎packages/astro/src/env/vite-plugin-import-meta-env.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { transform } from 'esbuild';
22
import MagicString from 'magic-string';
33
import type * as vite from 'vite';
4+
import { createFilter, isCSSRequest } from 'vite';
45
import type { EnvLoader } from './env-loader.js';
56

67
interface EnvPluginOptions {
@@ -71,6 +72,7 @@ export function importMetaEnv({ envLoader }: EnvPluginOptions): vite.Plugin {
7172
let isDev: boolean;
7273
let devImportMetaEnvPrepend: string;
7374
let viteConfig: vite.ResolvedConfig;
75+
const filter = createFilter(null, ['**/*.html', '**/*.htm', '**/*.json']);
7476
return {
7577
name: 'astro:vite-plugin-env',
7678
config(_, { command }) {
@@ -96,11 +98,17 @@ export function importMetaEnv({ envLoader }: EnvPluginOptions): vite.Plugin {
9698
}
9799
}
98100
},
101+
99102
transform(source, id, options) {
100-
if (!options?.ssr || !source.includes('import.meta.env')) {
103+
if (
104+
!options?.ssr ||
105+
!source.includes('import.meta.env') ||
106+
!filter(id) ||
107+
isCSSRequest(id) ||
108+
viteConfig.assetsInclude(id)
109+
) {
101110
return;
102111
}
103-
104112
// Find matches for *private* env and do our own replacement.
105113
// Env is retrieved before process.env is populated by astro:env
106114
// so that import.meta.env is first replaced by values, not process.env

‎packages/astro/test/astro-envs.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,14 @@ describe('Environment Variables', () => {
120120
let $ = cheerio.load(indexHtml);
121121
assert.equal($('#base-url').text(), '/blog');
122122
});
123+
124+
it('does not inject env into imported asset files', async () => {
125+
let res = await fixture.fetch('/blog/');
126+
assert.equal(res.status, 200);
127+
let indexHtml = await res.text();
128+
let $ = cheerio.load(indexHtml);
129+
assert.equal($('#env').text(), 'A MYSTERY');
130+
assert.equal($('#css').text(), 'good');
131+
});
123132
});
124133
});
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
SECRET_PLACE=CLUB_33
22
PUBLIC_PLACE=BLUE_BAYOU
3+
KITTY=CHESHIRE

‎packages/astro/test/fixtures/astro-envs/astro.config.mjs

+14
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,18 @@ export default defineConfig({
66
site: 'http://example.com',
77
base: '/blog',
88
integrations: [vue()],
9+
vite: {
10+
plugins: [
11+
{
12+
// Plugin so that we can see in the tests whether the env has been injected
13+
name: 'export-env-plugin',
14+
enforce: 'post',
15+
transform(code, id) {
16+
if (id.endsWith('.json')) {
17+
return `${code}\n export const env = ${JSON.stringify(code.includes('CHESHIRE') || code.includes('process.env.KITTY') ? 'CHESHIRE' : 'A MYSTERY')}`;
18+
}
19+
},
20+
},
21+
],
22+
},
923
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"tiddles": {
3+
"name": "Tiddles",
4+
"age": 3,
5+
"colour": "black"
6+
},
7+
"mittens": {
8+
"name": "Mittens",
9+
"age": 5,
10+
"colour": "white"
11+
},
12+
"fluffy": {
13+
"name": "Fluffy",
14+
"age": 2,
15+
"colour": "grey"
16+
},
17+
"whiskers": {
18+
"name": "Whiskers",
19+
"age": 4,
20+
"colour": "tabby"
21+
},
22+
"bobby-env": {
23+
"name": "import.meta.env.KITTY",
24+
"age": 1,
25+
"colour": "calico"
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Just mentioning import.meta.env is enough to trigger this */
2+
body {
3+
background-color: red;
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
---
3+
<h1>import.meta.env.KITTEN</h1>
4+
5+
```js
6+
console.log(import.meta.env.KITTEN)
7+
```
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
---
22
import Client from '../components/Client.vue';
3+
import css from '../data/hello.css?inline';
4+
const {env} = await import('../data/cats.json');
35
---
6+
<head />
47
<environment-variable>{import.meta.env.PUBLIC_PLACE}</environment-variable>
58
<environment-variable>{import.meta.env.SECRET_PLACE}</environment-variable>
69
<environment-variable>{import.meta.env.SITE}</environment-variable>
710
<environment-variable id="base-url">{import.meta.env.BASE_URL}</environment-variable>
11+
<environment-variable id="env">{env}</environment-variable>
12+
<environment-variable id="css">{css.includes('SECRET_PLACE') ? 'bad' : 'good' }</environment-variable>
813
<Client client:load />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
</head>
9+
10+
<body>
11+
Did you know import.meta.env is a magic word?
12+
</body>
13+
14+
</html>

0 commit comments

Comments
 (0)
Please sign in to comment.