Я развертываю приложение реакции в режиме heroku
в staging
. Поскольку heroku по умолчанию выполняет сценарии build
и start
, он не пройдет env.ENVIRONMENT=staging
. Локально я делаю npm run build:stag
, и он работает.
пакет. json
"scripts": {
"start:dev": "webpack-dev-server --env.ENVIRONMENT=development --mode development",
"start:stag": "NODE_ENV=staging node -r dotenv/config server.js",
"start": "node -r dotenv/config server.js",
"build": "webpack --mode production",
"build:stag": "webpack --env.ENVIRONMENT=staging --mode production"
}
Я не уверен, как заставить start:stag
работать на heroku
или netlify
.
webpack.config. js
const webpack = require("webpack");
const path = require("path");
const fs = require("fs");
const dotenv = require("dotenv");
module.exports = env => {
// Get the root path
const currentPath = path.join(__dirname);
// Create the fallback path (the production .env)
const currEnv = env.ENVIRONMENT;
const basePath = currentPath + "/.env";
const envPath = basePath + "." + currEnv;
const finalPath = fs.existsSync(envPath) ? envPath : basePath;
// Set the path parameter in the dotenv config
const fileEnv = dotenv.config({ path: finalPath }).parsed;
// reduce it to a nice object, the same as before (but with the variables from the file)
const envKeys = Object.keys(fileEnv).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(fileEnv[next]);
return prev;
}, {});
return {
// other things........
plugins: [
new webpack.DefinePlugin(envKeys),
new webpack.HotModuleReplacementPlugin()
]
};
};