From d22c3bf86209e4065996b856c85fd257691f4c88 Mon Sep 17 00:00:00 2001 From: zenios Date: Fri, 16 Nov 2018 09:25:32 +0200 Subject: [PATCH] feat: Support env variable using dot env (#227) --- packages/electron-webpack/package.json | 9 +++++---- packages/electron-webpack/src/main.ts | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/electron-webpack/package.json b/packages/electron-webpack/package.json index fbe5fe1..5cf830a 100644 --- a/packages/electron-webpack/package.json +++ b/packages/electron-webpack/package.json @@ -22,31 +22,32 @@ "dependencies": { "@types/webpack-env": "^1.13.6", "async-exit-hook": "^2.0.1", - "terser-webpack-plugin": "^1.1.0", "bluebird-lst": "^1.0.6", "chalk": "^2.4.1", "crocket": "^0.9.11", "css-hot-loader": "^1.4.2", "css-loader": "^1.0.1", "debug": "^4.1.0", + "dotenv": "^6.1.0", "electron-devtools-installer": "^2.2.4", "electron-webpack-js": "~2.3.0", - "mini-css-extract-plugin": "^0.4.4", "file-loader": "^2.0.0", "fs-extra-p": "^7.0.0", "html-loader": "^1.0.0-alpha.0", "html-webpack-plugin": "^3.2.0", "lazy-val": "^1.0.3", + "mini-css-extract-plugin": "^0.4.4", "node-loader": "^0.6.0", "read-config-file": "^3.1.3", "semver": "^5.6.0", "source-map-support": "^0.5.9", "style-loader": "^0.23.1", + "terser-webpack-plugin": "^1.1.0", "url-loader": "^1.1.2", + "webpack-cli": "^3.1.2", "webpack-dev-server": "^3.1.10", "webpack-merge": "^4.1.4", - "yargs": "^12.0.2", - "webpack-cli": "^3.1.2" + "yargs": "^12.0.2" }, "peerDependencies": { "webpack": "^4.8.3" diff --git a/packages/electron-webpack/src/main.ts b/packages/electron-webpack/src/main.ts index 47da84a..b053845 100644 --- a/packages/electron-webpack/src/main.ts +++ b/packages/electron-webpack/src/main.ts @@ -1,5 +1,5 @@ import BluebirdPromise from "bluebird-lst" -import { readJson } from "fs-extra-p" +import { readJson, pathExists } from "fs-extra-p" import { Lazy } from "lazy-val" import * as path from "path" import { validateConfig } from "read-config-file" @@ -332,6 +332,23 @@ export async function configure(type: ConfigurationType, env: ConfigurationEnv | return null } else { + const processEnv = configurator.isProduction ? "production" : "development" + const dotEnvPath = path.resolve(configurator.projectDir, ".env") + const dotenvFiles = [ + `${dotEnvPath}.${processEnv}.local`, + `${dotEnvPath}.${processEnv}`, + `${dotEnvPath}.local`, + dotEnvPath, + ] + + for (const file of dotenvFiles) { + const exists = await pathExists(file) + if (exists) { + require("dotenv").config({ + path: file + }) + } + } return await configurator.configure() } }