Skip to content

Commit

Permalink
fix: only add specified env vars to bundle (anoma#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
emccorson committed Dec 11, 2023
1 parent 570f068 commit d8b3214
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
6 changes: 5 additions & 1 deletion apps/airdrop/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") });
Expand Down Expand Up @@ -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"]
)),
},
}),
];
Expand Down
6 changes: 5 additions & 1 deletion apps/extension/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down Expand Up @@ -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({
Expand Down
6 changes: 5 additions & 1 deletion apps/faucet/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") });
Expand Down Expand Up @@ -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"]
)),
},
}),
];
Expand Down
6 changes: 5 additions & 1 deletion apps/namada-interface/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") });
Expand Down Expand Up @@ -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"]
)),
},
}),
];
Expand Down
22 changes: 22 additions & 0 deletions packages/config/webpack.js
Original file line number Diff line number Diff line change
@@ -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,
};

0 comments on commit d8b3214

Please sign in to comment.