Skip to content

Commit a1d0441

Browse files
authored
Merge pull request #2 from OpenSourceRaidGuild/#1
fix: replace unknown import.meta.env lookups with full env object
2 parents 3f23671 + 9a96ff5 commit a1d0441

File tree

3 files changed

+124
-12
lines changed

3 files changed

+124
-12
lines changed

packages/babel-plugin-transform-vite-meta-env/src/__tests__/__snapshots__/index.ts.snap

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`vite-meta-env not import.meta lookup: not import.meta lookup 1`] = `
4+
5+
const x = import.meta()
6+
7+
↓ ↓ ↓ ↓ ↓ ↓
8+
9+
const x = import.meta()
10+
11+
12+
`;
13+
314
exports[`vite-meta-env not import.meta.env: not import.meta.env 1`] = `
415
16+
const x = import.meta.other
17+
18+
↓ ↓ ↓ ↓ ↓ ↓
19+
20+
const x = import.meta.other
21+
22+
23+
`;
24+
25+
exports[`vite-meta-env not import.meta: not import.meta 1`] = `
26+
527
const x = process.env.MODE
628
729
↓ ↓ ↓ ↓ ↓ ↓
@@ -17,7 +39,14 @@ const x = import.meta.env.OTHER
1739
1840
↓ ↓ ↓ ↓ ↓ ↓
1941
20-
const x = undefined
42+
const x = {
43+
...Object.fromEntries(Object.entries(process.env).filter(([k]) => /^VITE_/.test(k))),
44+
NODE_ENV: process.env.NODE_ENV || 'test',
45+
MODE: process.env.NODE_ENV || 'test',
46+
BASE_URL: '/',
47+
DEV: process.env.NODE_ENV !== 'production',
48+
PROD: process.env.NODE_ENV === 'production'
49+
}.OTHER
2150
2251
2352
`;
@@ -86,4 +115,59 @@ const x = import.meta.env.VITE_VAR
86115
const x = process.env.VITE_VAR
87116
88117
118+
`;
119+
120+
exports[`vite-meta-env replace env object: replace env object 1`] = `
121+
122+
const env = import.meta.env
123+
124+
↓ ↓ ↓ ↓ ↓ ↓
125+
126+
const env = {
127+
...Object.fromEntries(Object.entries(process.env).filter(([k]) => /^VITE_/.test(k))),
128+
NODE_ENV: process.env.NODE_ENV || 'test',
129+
MODE: process.env.NODE_ENV || 'test',
130+
BASE_URL: '/',
131+
DEV: process.env.NODE_ENV !== 'production',
132+
PROD: process.env.NODE_ENV === 'production'
133+
}
134+
135+
136+
`;
137+
138+
exports[`vite-meta-env replace key access: replace key access 1`] = `
139+
140+
const key = "VITE_VAR"; const x = import.meta.env[key]
141+
142+
↓ ↓ ↓ ↓ ↓ ↓
143+
144+
const key = 'VITE_VAR'
145+
const x = {
146+
...Object.fromEntries(Object.entries(process.env).filter(([k]) => /^VITE_/.test(k))),
147+
NODE_ENV: process.env.NODE_ENV || 'test',
148+
MODE: process.env.NODE_ENV || 'test',
149+
BASE_URL: '/',
150+
DEV: process.env.NODE_ENV !== 'production',
151+
PROD: process.env.NODE_ENV === 'production'
152+
}[key]
153+
154+
155+
`;
156+
157+
exports[`vite-meta-env replace string access: replace string access 1`] = `
158+
159+
const x = import.meta.env["VITE_VAR"]
160+
161+
↓ ↓ ↓ ↓ ↓ ↓
162+
163+
const x = {
164+
...Object.fromEntries(Object.entries(process.env).filter(([k]) => /^VITE_/.test(k))),
165+
NODE_ENV: process.env.NODE_ENV || 'test',
166+
MODE: process.env.NODE_ENV || 'test',
167+
BASE_URL: '/',
168+
DEV: process.env.NODE_ENV !== 'production',
169+
PROD: process.env.NODE_ENV === 'production'
170+
}['VITE_VAR']
171+
172+
89173
`;

packages/babel-plugin-transform-vite-meta-env/src/__tests__/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ pluginTester({
1212
'replace DEV': 'const x = import.meta.env.DEV',
1313
'replace PROD': 'const x = import.meta.env.PROD',
1414
'replace VITE_* variables': 'const x = import.meta.env.VITE_VAR',
15+
'replace string access': 'const x = import.meta.env["VITE_VAR"]',
16+
'replace key access': 'const key = "VITE_VAR"; const x = import.meta.env[key]',
17+
'replace env object': 'const env = import.meta.env',
1518
'not replaceable': 'const x = import.meta.env.OTHER',
16-
'not import.meta.env': 'const x = process.env.MODE'
19+
'not import.meta.env': 'const x = import.meta.other',
20+
'not import.meta': 'const x = process.env.MODE',
21+
'not import.meta lookup': 'const x = import.meta()'
1722
}
1823
})

packages/babel-plugin-transform-vite-meta-env/src/index.ts

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type babelCore from '@babel/core'
22

3-
const REPLACE_VARS = [
3+
const replaceVars = [
44
{
55
regex: /^VITE_/,
66
replacement: (template: typeof babelCore.template, variableName: string) =>
@@ -24,20 +24,26 @@ const REPLACE_VARS = [
2424
regex: /^PROD$/,
2525
replacement: (template: typeof babelCore.template) =>
2626
template.expression.ast("process.env.NODE_ENV === 'production'")
27-
},
28-
{
29-
regex: /.*/,
30-
replacement: (template: typeof babelCore.template) => template.expression.ast('undefined')
3127
}
3228
]
3329

30+
const replaceEnv = (template: typeof babelCore.template) =>
31+
template.expression.ast(`{
32+
...Object.fromEntries(Object.entries(process.env).filter(([k]) => /^VITE_/.test(k))),
33+
NODE_ENV: process.env.NODE_ENV || 'test',
34+
MODE: process.env.NODE_ENV || 'test',
35+
BASE_URL: '/',
36+
DEV: process.env.NODE_ENV !== 'production',
37+
PROD: process.env.NODE_ENV === 'production'
38+
}`)
39+
3440
function getReplacement(
3541
variableName: string,
3642
template: typeof babelCore.template
37-
): babelCore.types.Expression {
38-
return REPLACE_VARS.filter(({ regex }) => regex.test(variableName)).map(({ replacement }) =>
39-
replacement(template, variableName)
40-
)[0]
43+
): babelCore.types.Expression | undefined {
44+
return replaceVars
45+
.filter(({ regex }) => regex.test(variableName))
46+
.map(({ replacement }) => replacement(template, variableName))[0]
4147
}
4248

4349
export default function viteMetaEnvBabelPlugin({
@@ -64,7 +70,24 @@ export default function viteMetaEnvBabelPlugin({
6470

6571
const replacement = getReplacement(variableName, template)
6672

67-
path.replaceWith(replacement)
73+
if (replacement) {
74+
path.replaceWith(replacement)
75+
}
76+
},
77+
MetaProperty(path) {
78+
const envNode = t.isMemberExpression(path.parentPath.node) && path.parentPath.node
79+
80+
if (!envNode) {
81+
return
82+
}
83+
84+
const isEnvVar = t.isIdentifier(envNode.property) && envNode.property.name === 'env'
85+
86+
if (!isEnvVar) {
87+
return
88+
}
89+
90+
path.parentPath.replaceWith(replaceEnv(template))
6891
}
6992
}
7093
}

0 commit comments

Comments
 (0)