Skip to content

Commit

Permalink
fix: only replace import.meta.env
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Aug 18, 2023
1 parent 035c879 commit c6895d6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/plugins/import-meta-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <PluginObj>{
Expand Down Expand Up @@ -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"));
}
Expand Down
3 changes: 0 additions & 3 deletions test/fixtures/env/index.ts → test/fixtures/env/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit c6895d6

Please sign in to comment.