Skip to content

Commit

Permalink
fix: support import.meta.env and import.meta.env?.prop (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Aug 18, 2023
1 parent f18508c commit 7155c00
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 78 deletions.
94 changes: 18 additions & 76 deletions src/plugins/import-meta-env.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,9 @@
/**
* Based on https://github.com/iendeavor/import-meta-env/tree/main/packages/babel 0.4.2 (MIT)
Modified to use runtime only without dotenv dependency
---
MIT License
Copyright (c) 2021 Ernest
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
* Forked from https://github.com/iendeavor/import-meta-env/tree/main/packages/babel 0.4.2 (MIT License - Copyright (c) 2021 Ernest)
*/

import type BabelCore from "@babel/core";
import type { PluginObj } from "@babel/core";

export const accessor = `process.env`;

const replaceEnvForRuntime = (
template: typeof BabelCore.template,
property: string,
) => template.expression.ast(`${accessor}.${property}`);
import type { MemberExpression, MetaProperty } from "@babel/types";

export function importMetaEnvPlugin({ template, types }: any) {
return <PluginObj>{
Expand All @@ -47,58 +14,33 @@ export function importMetaEnvPlugin({ template, types }: any) {
return;
}

// {}.{}
if (!types.isMemberExpression(path.parentPath)) {
// {}.{} or {}?.{} (meta.env or meta?.env)
if (
!types.isMemberExpression(path.parentPath) &&
!types.isOptionalMemberExpression(path.parentPath)
) {
return;
}
// {}.{}.{}

// {}.{}.{} (import.meta.env)
if (!types.isMemberExpression(path.parentPath.node)) {
return;
}
// {}.{}.{}.{}
// @ts-ignore
if (!types.isMemberExpression(path.parentPath.node.object)) {
return;
}

// {}.{}.{}.PROPERTY
// @ts-ignore
if (path.parentPath.computed) {
return;
}
// @ts-ignore
if (!types.isIdentifier(path.parentPath.node.property)) {
return;
}
const parentNode = path.parentPath.node as MemberExpression;

// {}.{}.env.PROPERTY
// @ts-ignore
if (!types.isIdentifier(path.parentPath.node.object.property)) {
return;
}
// @ts-ignore
if (path.parentPath.node.object.property.name !== "env") {
if (!types.isMetaProperty(parentNode.object)) {
return;
}

// import.meta.env.PROPERTY
// @ts-ignore
if (!types.isMetaProperty(path.parentPath.node.object.object)) {
return;
}
// @ts-ignore
if (path.parentPath.node.object.object.property.name !== "meta") {
return;
}
// @ts-ignore
if (path.parentPath.node.object.object.meta.name !== "import") {
return;
}
const parentNodeObjMeta = parentNode.object as MetaProperty;

path.parentPath.replaceWith(
// @ts-ignore
replaceEnvForRuntime(template, path.parentPath.node.property.name),
);
if (
parentNodeObjMeta.meta.name === "import" ||
parentNodeObjMeta.property.name === "meta"
) {
path.parentPath.replaceWith(template.expression.ast("process.env"));
}
},
},
};
Expand Down
8 changes: 6 additions & 2 deletions test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ exports[`fixtures > async > stdout 1`] = `"works"`;
exports[`fixtures > circular > stdout 1`] = `"a b c"`;

exports[`fixtures > env > stdout 1`] = `
"process.env.TEST true
import.meta.env.TEST true"
"process.env true
process.env.TEST true
process.env?.TEST true
import.meta true
import.meta.env.TEST true
import.meta.env?.TEST true"
`;

exports[`fixtures > error-parse > stderr 1`] = `
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/env/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
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 7155c00

Please sign in to comment.