🔧 Added process.env support via Webpack

This commit is contained in:
2021-10-19 14:32:47 -07:00
parent c1e9a30419
commit 9024c3cce9
3 changed files with 95 additions and 90 deletions

View File

@@ -9,7 +9,7 @@ services:
container_name: threetwo-ui container_name: threetwo-ui
env_file: ./docker-compose.env env_file: ./docker-compose.env
environment: environment:
DOCKERHOST: "{{.Node.Hostname}}" DOCKER_SOCKET_HOST: "{{.Node.Hostname}}:8051"
restart: unless-stopped restart: unless-stopped
labels: labels:
- "traefik.enable=true" - "traefik.enable=true"

View File

@@ -8,7 +8,7 @@ import { success } from "react-notification-system-redux";
const WebSocketContext = createContext(null); const WebSocketContext = createContext(null);
export const WebSocketProvider = ({ children }): ReactElement => { export const WebSocketProvider = ({ children }): ReactElement => {
const dispatch = useDispatch(); const dispatch = useDispatch();
const socketHost = process.env.DOCKERHOST + ":8051" || SOCKET_BASE_URI; const socketHost = process.env.DOCKER_SOCKET_HOST || SOCKET_BASE_URI;
const socket: Socket = io(socketHost); const socket: Socket = io(socketHost);
socket.on("connect", () => { socket.on("connect", () => {

View File

@@ -1,11 +1,13 @@
const path = require("path"); const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin"); const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin"); const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const webpack = require("webpack");
const outputDirectory = "dist"; const outputDirectory = "dist";
const BundleAnalyzerPlugin = const BundleAnalyzerPlugin =
require("webpack-bundle-analyzer").BundleAnalyzerPlugin; require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
module.exports = { module.exports = (env) => {
return {
entry: ["babel-polyfill", "./src/client/index.tsx"], entry: ["babel-polyfill", "./src/client/index.tsx"],
output: { output: {
path: path.join(__dirname, outputDirectory), path: path.join(__dirname, outputDirectory),
@@ -89,11 +91,14 @@ module.exports = {
title: "express-typescript-react", title: "express-typescript-react",
}), }),
new webpack.DefinePlugin({ new webpack.DefinePlugin({
"process.env.DOCKERHOST": JSON.stringify(process.env.DOCKERHOST), "process.env.DOCKER_SOCKET_HOST": JSON.stringify(
process.env.DOCKER_SOCKET_HOST,
),
}), }),
new MiniCssExtractPlugin({ new MiniCssExtractPlugin({
filename: "./css/[name].css", filename: "./css/[name].css",
chunkFilename: "./css/[id].css", chunkFilename: "./css/[id].css",
}), }),
], ],
};
}; };