diff --git a/src/plugins/import-meta-env.ts b/src/plugins/import-meta-env.ts index 552bd4c6..c8754d63 100644 --- a/src/plugins/import-meta-env.ts +++ b/src/plugins/import-meta-env.ts @@ -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 { @@ -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")); + } }, }, }; diff --git a/test/__snapshots__/fixtures.test.ts.snap b/test/__snapshots__/fixtures.test.ts.snap index f970ded6..480184ac 100644 --- a/test/__snapshots__/fixtures.test.ts.snap +++ b/test/__snapshots__/fixtures.test.ts.snap @@ -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`] = ` diff --git a/test/fixtures/env/index.ts b/test/fixtures/env/index.ts index 6a8d05af..8b834a12 100644 --- a/test/fixtures/env/index.ts +++ b/test/fixtures/env/index.ts @@ -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);