From d8b32142edd335df11008c5899331c3fcb0ccb50 Mon Sep 17 00:00:00 2001 From: Eric Corson Date: Tue, 5 Dec 2023 14:23:13 +0900 Subject: [PATCH] fix: only add specified env vars to bundle (#493) --- apps/airdrop/webpack.config.js | 6 +++++- apps/extension/webpack.config.js | 6 +++++- apps/faucet/webpack.config.js | 6 +++++- apps/namada-interface/webpack.config.js | 6 +++++- packages/config/webpack.js | 22 ++++++++++++++++++++++ 5 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 packages/config/webpack.js diff --git a/apps/airdrop/webpack.config.js b/apps/airdrop/webpack.config.js index 12d6d6f1d60..69974703161 100644 --- a/apps/airdrop/webpack.config.js +++ b/apps/airdrop/webpack.config.js @@ -2,6 +2,7 @@ const webpack = require("webpack"); const { resolve, join } = require("path"); const HtmlWebpackPlugin = require("html-webpack-plugin"); const CopyPlugin = require("copy-webpack-plugin"); +const { getProcessEnv } = require("@namada/config/webpack.js"); // Load environment variables require("dotenv").config({ path: resolve(__dirname, ".env") }); @@ -43,7 +44,10 @@ const plugins = [ // Provide environment variables to interface: new webpack.DefinePlugin({ process: { - env: JSON.stringify(process.env), + env: JSON.stringify(getProcessEnv( + "NAMADA_INTERFACE", + ["TARGET", "NODE_ENV", "npm_package_version"] + )), }, }), ]; diff --git a/apps/extension/webpack.config.js b/apps/extension/webpack.config.js index 9d21c46f0d0..9ba3b50f911 100644 --- a/apps/extension/webpack.config.js +++ b/apps/extension/webpack.config.js @@ -7,6 +7,7 @@ const RemovePlugin = require('remove-files-webpack-plugin'); const createStyledComponentsTransformer = require("typescript-plugin-styled-components").default; const packageJson = require("./package.json"); +const { getProcessEnv } = require("@namada/config/webpack.js"); // Load .env from namada-interface: require("dotenv").config({ path: "../namada-interface/.env" }); @@ -91,7 +92,10 @@ const plugins = [ // Provide environment variables to extension: new webpack.DefinePlugin({ process: { - env: JSON.stringify(process.env), + env: JSON.stringify(getProcessEnv( + "NAMADA_INTERFACE", + ["TARGET", "NODE_ENV", "npm_package_version"] + )), }, }), new RemovePlugin({ diff --git a/apps/faucet/webpack.config.js b/apps/faucet/webpack.config.js index 0bd468a13b6..c3d42994a7c 100644 --- a/apps/faucet/webpack.config.js +++ b/apps/faucet/webpack.config.js @@ -2,6 +2,7 @@ const webpack = require("webpack"); const { resolve, join } = require("path"); const HtmlWebpackPlugin = require("html-webpack-plugin"); const CopyPlugin = require("copy-webpack-plugin"); +const { getProcessEnv } = require("@namada/config/webpack.js"); // Load environment variables require("dotenv").config({ path: resolve(__dirname, ".env") }); @@ -39,7 +40,10 @@ const plugins = [ // Provide environment variables to interface: new webpack.DefinePlugin({ process: { - env: JSON.stringify(process.env), + env: JSON.stringify(getProcessEnv( + "NAMADA_INTERFACE", + ["TARGET", "NODE_ENV", "npm_package_version"] + )), }, }), ]; diff --git a/apps/namada-interface/webpack.config.js b/apps/namada-interface/webpack.config.js index 4a06c571a0c..156305d811e 100644 --- a/apps/namada-interface/webpack.config.js +++ b/apps/namada-interface/webpack.config.js @@ -2,6 +2,7 @@ const webpack = require("webpack"); const { resolve, join } = require("path"); const HtmlWebpackPlugin = require("html-webpack-plugin"); const CopyPlugin = require("copy-webpack-plugin"); +const { getProcessEnv } = require("@namada/config/webpack.js"); // Load environment variables require("dotenv").config({ path: resolve(__dirname, ".env") }); @@ -39,7 +40,10 @@ const plugins = [ // Provide environment variables to interface: new webpack.DefinePlugin({ process: { - env: JSON.stringify(process.env), + env: JSON.stringify(getProcessEnv( + "NAMADA_INTERFACE", + ["TARGET", "NODE_ENV", "npm_package_version"] + )), }, }), ]; diff --git a/packages/config/webpack.js b/packages/config/webpack.js new file mode 100644 index 00000000000..d7d81e5f969 --- /dev/null +++ b/packages/config/webpack.js @@ -0,0 +1,22 @@ +/** + * Selects a subset of environment variables from process.env. + * + * @param {string} prefix - All env vars starting with this prefix are added + * @param {string[]} extras - Other env vars that should be added + * @returns {object} An object containing only the allowed env vars + */ +const getProcessEnv = (prefix, extras) => { + const prefixedEntries = Object.entries(process.env) + .filter(([key, value]) => key.startsWith(`${prefix}_`)); + + const extraEntries = extras.map(key => [key, process.env[key]]); + + return prefixedEntries.concat(extraEntries).reduce( + (acc, [key, value]) => ({ [key]: value, ...acc }), + {} + ); +}; + +module.exports = { + getProcessEnv, +};