I've be banging my head against the wall on how to do this. I've been developing on a vue-cli application, and apparently, in the node world, we all believe there exists one development, one staging, and one production environment.
I've been trying to reduce as much manual labor as I possibly can, so I don't manually keep moving files around, changing configs, etc.
I have 5 environments in total, but 3 dev environments that I need to clear up with this system, and I really have no way of going about it, so I need some guidance.
- 1st dev environment, localhost:12345 - I have placed variables where it turns off authentication and specifies the url path. If this was called MyLocal for process.env.NODE_ENV, then I could use env.mylocal's variables.
- 2nd dev environment, localhost:12345 (yes same one) - I have placed variables where all my authentication comes back, and I specify the same url path. If this was called DevLocal for process.env.NODE_ENV, then I could use env.devlocal's variables
- 3rd dev environment, dev.example.com/newAppLoc/web - I have placed variables where all my authentication comes back, and I specify a new url path. If this was called Development for process.env.NODE_ENV, then I could use env.development's variables
The 4th and 5th would be staging and production respectively.
If I had the option of creating my own environment names to more than 3, this question could have been avoided. Because all I'd need to do at that point is specify which environment to use, and let the build scripts run. Then at that point, I can publish my whole repo without worrying which env files to send, since it's already been specified in the build.
But since there is only 3 I can specify, I need help in figuring this out. I have to worry about my files being sent in fear of one file's presence overriding another config. I also have to worry about my files being sent on a repo that others can fetch and have their project settings get overridden. In my opinion, this whole scheme seems way too flakey, and I'm pretty pessimistic towards this strict and overthought mess. Yet, I would love to know what kind of workflows I can see that have this in place to more than 3 environments.
Also, there is no way I can override the values of "development", "staging", "production". There are checks in place where they prevent you from comparing values to something else. Meaning, in your webpack.config.js or vue.config.js, if your process.env.NODE_ENV is MyLocal, and you try to compare it to "MyLocal", it will literally become false.
Here is the scripts I have in my package.json
"serve": "vue-cli-service serve",
"serve:mylocal": "vue-cli-service serve --mode MyLocal",
"build:mylocal": "vue-cli-service build --mode MyLocal",
"build:devlocal": vue-cli-service build --mode devlocal",
"build:dev": "vue-cli-service build --mode development",
"build:prod": "vue-cli-service build",
So:
const isDevBuild = process.env.NODE_ENV === "MyLocal"; // false if process.env.NODE_ENV = MyLocal
const isDevBuild = process.env.NODE_ENV === "production"; // true if process.env.NODE_ENV = production
I was thinking:
- MyLocal would be the same as .env.local
- DevLocal would be the same as .env.development.local
- Development would be the same as .env.development
But the problem is, how do you specify which to use? They are based on precedence just by existing, which makes it harder to control.