Skip to content

Commit c6895d6

Browse files
committed
fix: only replace import.meta.env
1 parent 035c879 commit c6895d6

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

src/plugins/import-meta-env.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import type { PluginObj } from "@babel/core";
6-
import type { MemberExpression, MetaProperty } from "@babel/types";
6+
import type { Identifier, MemberExpression, MetaProperty } from "@babel/types";
77

88
export function importMetaEnvPlugin({ template, types }: any) {
99
return <PluginObj>{
@@ -36,8 +36,9 @@ export function importMetaEnvPlugin({ template, types }: any) {
3636
const parentNodeObjMeta = parentNode.object as MetaProperty;
3737

3838
if (
39-
parentNodeObjMeta.meta.name === "import" ||
40-
parentNodeObjMeta.property.name === "meta"
39+
parentNodeObjMeta.meta.name === "import" &&
40+
parentNodeObjMeta.property.name === "meta" &&
41+
(parentNode.property as Identifier).name === "env"
4142
) {
4243
path.parentPath.replaceWith(template.expression.ast("process.env"));
4344
}

test/fixtures/env/index.ts renamed to test/fixtures/env/index.mjs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ console.log("process.env", Object.keys(process.env).includes("TEST"));
22
console.log("process.env.TEST", process.env.TEST);
33
console.log("process.env?.TEST", process.env?.TEST);
44

5-
// @ts-ignore
65
console.log("import.meta", Object.keys(import.meta.env).includes("TEST"));
7-
// @ts-ignore
86
console.log("import.meta.env.TEST", import.meta.env.TEST);
9-
// @ts-ignore
107
console.log("import.meta.env?.TEST", import.meta.env?.TEST);

0 commit comments

Comments
 (0)