From c6895d6e17f6c44fff979759c3da971267a1829c Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 18 Aug 2023 14:00:47 +0200 Subject: [PATCH] fix: only replace `import.meta.env` --- src/plugins/import-meta-env.ts | 7 ++++--- test/fixtures/env/{index.ts => index.mjs} | 3 --- 2 files changed, 4 insertions(+), 6 deletions(-) rename test/fixtures/env/{index.ts => index.mjs} (89%) diff --git a/src/plugins/import-meta-env.ts b/src/plugins/import-meta-env.ts index c8754d63..b5bab8ab 100644 --- a/src/plugins/import-meta-env.ts +++ b/src/plugins/import-meta-env.ts @@ -3,7 +3,7 @@ */ import type { PluginObj } from "@babel/core"; -import type { MemberExpression, MetaProperty } from "@babel/types"; +import type { Identifier, MemberExpression, MetaProperty } from "@babel/types"; export function importMetaEnvPlugin({ template, types }: any) { return { @@ -36,8 +36,9 @@ export function importMetaEnvPlugin({ template, types }: any) { const parentNodeObjMeta = parentNode.object as MetaProperty; if ( - parentNodeObjMeta.meta.name === "import" || - parentNodeObjMeta.property.name === "meta" + parentNodeObjMeta.meta.name === "import" && + parentNodeObjMeta.property.name === "meta" && + (parentNode.property as Identifier).name === "env" ) { path.parentPath.replaceWith(template.expression.ast("process.env")); } diff --git a/test/fixtures/env/index.ts b/test/fixtures/env/index.mjs similarity index 89% rename from test/fixtures/env/index.ts rename to test/fixtures/env/index.mjs index 8b834a12..99d7bea6 100644 --- a/test/fixtures/env/index.ts +++ b/test/fixtures/env/index.mjs @@ -2,9 +2,6 @@ console.log("process.env", Object.keys(process.env).includes("TEST")); console.log("process.env.TEST", process.env.TEST); console.log("process.env?.TEST", process.env?.TEST); -// @ts-ignore console.log("import.meta", Object.keys(import.meta.env).includes("TEST")); -// @ts-ignore console.log("import.meta.env.TEST", import.meta.env.TEST); -// @ts-ignore console.log("import.meta.env?.TEST", import.meta.env?.TEST);