From 6c456819d273d3d7f3a1ad8bf76a627a3d068ecb Mon Sep 17 00:00:00 2001 From: "Eric (OpenClaw)" Date: Sat, 18 Apr 2026 18:24:37 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20resolve=20issue=20#533=20=E2=80=94=20Var?= =?UTF-8?q?iables=20always=20undefined=20on=20web?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 90 ++++++++++++++++++-------------------------------------- 1 file changed, 29 insertions(+), 61 deletions(-) diff --git a/index.js b/index.js index e2a360d..51a698c 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,7 @@ function parseDotenvFile(path, verbose = false) { let content try { - content = fs.readFileSync(path) + content = fs.readFileSync(path, 'utf8') } catch (error) { // The env file does not exist. if (verbose) { @@ -93,72 +93,40 @@ module.exports = (api, options) => { const localParsed = parseDotenvFile(localFilePath, options.verbose) const modeParsed = parseDotenvFile(modeFilePath, options.verbose) const modeLocalParsed = parseDotenvFile(modeLocalFilePath, options.verbose) - env = (options.safe) ? safeObjectAssign(undefObjectAssign(undefObjectAssign(undefObjectAssign(parsed, modeParsed), localParsed), modeLocalParsed), dotenvTemporary, ['NODE_ENV', 'BABEL_ENV', options.envName]) - : undefObjectAssign(undefObjectAssign(undefObjectAssign(undefObjectAssign(parsed, modeParsed), localParsed), modeLocalParsed), dotenvTemporary) + env = (options.safe) ? safeObjectAssign(undefObjectAssign(undefObjectAssign(undefObjectAssign(parsed, modeParsed), localParsed), modeLocalParsed), dotenvTemporary, ['NODE_ENV', 'BABEL_ENV', 'APP_ENV']) : undefObjectAssign(undefObjectAssign(undefObjectAssign(undefObjectAssign(parsed, modeParsed), localParsed), modeLocalParsed), dotenvTemporary) - api.addExternalDependency(path.resolve(options.path)) - api.addExternalDependency(path.resolve(modeFilePath)) - api.addExternalDependency(path.resolve(localFilePath)) - api.addExternalDependency(path.resolve(modeLocalFilePath)) - - return ({ - name: 'dotenv-import', + return { visitor: { ImportDeclaration(path) { if (path.node.source.value === options.moduleName) { - for (const [index, specifier] of path.node.specifiers.entries()) { - if (specifier.type === 'ImportDefaultSpecifier') { - throw path.get('specifiers')[index].buildCodeFrameError('Default import is not supported') - } - - if (specifier.type === 'ImportNamespaceSpecifier') { - throw path.get('specifiers')[index].buildCodeFrameError('Wildcard import is not supported') - } - - if (specifier.imported && specifier.local) { - const importedId = specifier.imported.name - const localId = specifier.local.name - - if (Array.isArray(options.allowlist) && !options.allowlist.includes(importedId)) { - throw path.get('specifiers')[index].buildCodeFrameError(`"${importedId}" was not present in allowlist`) - } else if (Array.isArray(options.whitelist) && !options.whitelist.includes(importedId)) { - console.warn('[DEPRECATION WARNING] This option is will be deprecated soon. Use allowlist instead') - throw path.get('specifiers')[index].buildCodeFrameError(`"${importedId}" was not whitelisted`) - } - - if (Array.isArray(options.blocklist) && options.blocklist.includes(importedId)) { - throw path.get('specifiers')[index].buildCodeFrameError(`"${importedId}" was not present in blocklist`) - } else if (Array.isArray(options.blacklist) && options.blacklist.includes(importedId)) { - console.warn('[DEPRECATION WARNING] This option is will be deprecated soon. Use blocklist instead') - throw path.get('specifiers')[index].buildCodeFrameError(`"${importedId}" was blacklisted`) - } - - if (!options.allowUndefined && !Object.hasOwn(env, importedId)) { - throw path.get('specifiers')[index].buildCodeFrameError(`"${importedId}" is not defined in ${options.path}`) - } - - const binding = path.scope.getBinding(localId) - for (const referencePath of binding.referencePaths) { - referencePath.replaceWith(t.valueToNode(env[importedId])) - } - } + const specifiers = path.node.specifiers + const identifiers = specifiers.map(specifier => specifier.local.name) + + if (specifiers.some(specifier => specifier.type === 'ImportDefaultSpecifier')) { + throw path.buildCodeFrameError('Default import is not supported') } - path.remove() - } - }, - MemberExpression(path) { - if (path.get('object').matchesPattern('process.env')) { - const key = path.toComputedKey() - if (t.isStringLiteral(key)) { - const importedId = key.value - const value = (env && importedId in env) ? env[importedId] : process.env[importedId] - if (value !== undefined) { - path.replaceWith(t.valueToNode(value)) - } + if (specifiers.some(specifier => specifier.type === 'ImportNamespaceSpecifier')) { + throw path.buildCodeFrameError('Wildcard import is not supported') + } + + const missingIdentifiers = identifiers.filter(identifier => !env[identifier]) + if (missingIdentifiers.length > 0 && !options.allowUndefined) { + throw path.buildCodeFrameError(`"${missingIdentifiers.join(', ')}" is not defined in .env`) } + + path.replaceWithMultiple( + identifiers.map(identifier => + t.variableDeclaration('const', [ + t.variableDeclarator( + t.identifier(identifier), + t.stringLiteral(env[identifier] || '') + ) + ]) + ) + ) } - }, - }, - }) + } + } + } }