Env is not supported in some projects. In order to unify writing environment variable forms for projects, some Modules of Node write environment variables, or write different environment variable forms according to the current branch of GitLab.

Add files.env.development,.env.test,.env.production

Import required file packages (manually if not available in the project below)

  1. npm install yargs
  2. npm install dotenv
  3. npm install fs

Code implementation

Inject implementations of env for different kinds of projects

  1. Dav in. Webpackrc. Js
const fs = require("fs"); const dotenv = require("dotenv"); const argv = require("yargs").argv; const colorconsole = require("@kenworks/colorconsole"); Const git = require("./ SRC /utils/git") const setenv = env => {const envContent = dotenv.parse(fs.readFileSync(".env." + env)); console.log(envContent, 'envContent----'); for (const k in envContent) { const str = `process.env.${k}` process.env[str] = envContent[k]; }}; // if NPM run dev test etc const envs = {production: "master", test: "test", development: "dev"}; const tag = argv.dockerEnv || argv.mode || git.current() || process.env.API_ENV || "test"; const env = Object.keys(envs).find( i => ! tag.indexOf(envs[i]) || ! tag.indexOf(i) ); console.log('env-------' , argv) console.log('tag-------' ,tag) setenv(env); const IS_BUILD = argv._[0] === "build" || ""; // const IS_PROD = IS_BUILD && env === "production"; process.env.REACT_APP_BUILD = IS_BUILD; colorconsole.log("--------------------"); Colorconsole. log(" Current mode: "+ (process.env.react_app_build? "Packaging model" : "local service "), "black", 226); Colorconsole. log(" env: "+ env, "black", 226); colorconsole.log("--------------------");Copy the code
define: { ... process.env, 'process.env.TEST': process.env.TEST, 'process.env.NODE_PUB_PATH': process.env.NODE_PUB_PATH, // 'process.env.REACT_APP_OTHER_URL': process.env.REACT_APP_OTHER_URL },Copy the code

ps:

Dva: 2.1.0

Antd: 3.16.4

React: 16.2.0