From b1d7649b17bca5e0e8be7a40a1b38099261eff83 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Tue, 24 May 2022 11:54:21 +0100 Subject: [PATCH] make env files work --- Accounts/webpack.config.js | 31 +++++++++++++++++++++---------- CommonUI/src/Config.ts | 5 ++--- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Accounts/webpack.config.js b/Accounts/webpack.config.js index 920d06d502..0f79ca0626 100644 --- a/Accounts/webpack.config.js +++ b/Accounts/webpack.config.js @@ -1,6 +1,17 @@ const path = require("path"); const webpack = require("webpack"); -const Dotenv = require('dotenv-webpack'); +const dotenv = require('dotenv'); + +const readEnvFile = (pathToFile) => { + const parsed = dotenv.config({ path: pathToFile }).parsed; + + const env = {}; + for (const key in parsed) { + env[key] = JSON.stringify(parsed[key]); + } + + return env; +} module.exports = { entry: "./src/Index.tsx", @@ -17,15 +28,15 @@ module.exports = { 'react-native-sqlite-storage': 'react-native-sqlite-storage' }, plugins: [ - new webpack.ProvidePlugin({ - process: 'process/browser', - }), - // new webpack.DefinePlugin({ - // "process.env.hello": JSON.stringify(true), - // }), - // new Dotenv({ path: '../Common/.env' }), - // new Dotenv({ path: '../CommonUI/.env'}), - // new Dotenv({ path: './.env'}) + new webpack.DefinePlugin({ + 'process': { + 'env': { + ...readEnvFile('../Common/.env'), + ...readEnvFile('../CommonUI/.env'), + ...readEnvFile('./.env') + } + } + }), ], module: { rules: [ diff --git a/CommonUI/src/Config.ts b/CommonUI/src/Config.ts index a36b8ad49a..a9efbb88d9 100644 --- a/CommonUI/src/Config.ts +++ b/CommonUI/src/Config.ts @@ -4,9 +4,8 @@ import Route from 'Common/Types/API/Route'; import Version from 'Common/Types/Version'; import URL from 'Common/Types/API/URL' -export const env: Function = (_key: string): string => { - console.log(process.env['production']); - return '' +export const env: Function = (key: string): string => { + return process.env[key] || ''; }; export const API_PROTOCOL: Protocol = window.location.protocol.includes('https')