Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
chore: support for dotenv switching (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Oct 21, 2021
1 parent da48150 commit cd19746
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions webpack.config.js
Expand Up @@ -17,18 +17,18 @@ const pkg = require("./package.json");

module.exports = (env, args = {}) => {
const isProd = args.mode === "production";
let envfile = "";
try {
envfile = fs.readFileSync(`.env`);
} catch {
// ignore
}
const config = readEnv("REEARTH_WEB", {
source: {
...dotenv.parse(envfile),
...process.env,
},
});
const envfile = loadEnv(Object.keys(env || {}).find(k => !k.startsWith("WEBPACK_")));
const config = {
api: "http://localhost:8080/api",
published: "/published.html?alias={}",
...readEnv("REEARTH_WEB", {
source: {
// When --env local is specified, .env.local will be loaded
...(envfile ? dotenv.parse(envfile) : {}),
...process.env,
},
}),
};

return {
devServer: {
Expand All @@ -46,13 +46,8 @@ module.exports = (env, args = {}) => {
},
onBeforeSetupMiddleware(devServer) {
if (!devServer) return;

devServer.app.get("/reearth_config.json", (_req, res) => {
res.json({
api: "http://localhost:8080/api",
published: "/published.html?alias={}",
...Object.fromEntries(Object.entries(config).filter(([, v]) => Boolean(v))),
});
res.json(config);
});
},
},
Expand Down Expand Up @@ -90,17 +85,17 @@ module.exports = (env, args = {}) => {
test: /\.css$/,
use: ["style-loader", "css-loader"],
},
{
test: /\.ya?ml$/,
use: [{ loader: "json-loader" }, { loader: "yaml-flat-loader" }],
},
{
exclude: [/\.(jsx?|m?js|html?|json|tsx?|css|ya?ml)$/],
loader: "file-loader",
options: {
name: "assets/[name].[contenthash:8].[ext]",
},
},
{
test: /\.ya?ml$/,
use: [{ loader: "json-loader" }, { loader: "yaml-flat-loader" }],
},
],
},
optimization: {
Expand Down Expand Up @@ -180,3 +175,11 @@ module.exports = (env, args = {}) => {
},
};
};

function loadEnv(env) {
try {
return fs.readFileSync(`.env${env ? `.${env}` : ""}`);
} catch {
// ignore
}
}

0 comments on commit cd19746

Please sign in to comment.