In Vue cli, How to use custom mode and build like in production mode? - vue.js

I'm building a Vue app and want to override some env variables while building in one build case, so I created a custom mode and sat a command in package.json like:
vue-cli-service build --mode myMode --modern
And put the env vars I want in the file specified for this mode:
// file name: .env.myMode
VUE_APP_MY_VAR=123
Now how can I build using the same build steps in production mode but within my custom mode?
Because when I build using the previous command it doesn't mangle or compress the js files for example.

Just add NODE_ENV=production into .env.myMode
Docs
Then NODE_ENV will determine the primary mode your app is running in - development, production or test - and consequently, what kind of webpack config will be created.

Related

Getting environment variables at runtime in a compiled VueJS app

I'm working on a VueJS app that uses env vars in multiple places. It runs on GCloud with nginx serving the files compiled with vue-cli-service build.
When developing everything works well with the env vars set in .env.development and .env.development.local files and used in JS with process.env.VUE_APP_FOO. I'm not using .env.production as some of these env vars shouldn't be committed to our repository.
For the staging and prod environment of all of our projects, we use GCloud's config maps which let us provide env vars to the pods. The issue in this project is that vue-cli-service build requires the env vars to be available at build time, which is not the case in our setup. Config maps are only available in the pods that run the images.
Out of curiosity, I checked the compiled code and all uses of process.env are quite simply replaced by an object with all vue env vars (basic ones + VUE_APP_* ones). So for example,
console.log(process.env.VUE_APP_FOO);
is compiled to
console.log(Object({NODE_ENV: "production", BASE_URL: "/", VUE_APP_FOO: "bar"}).VUE_APP_FOO);
Except that in our case, VUE_APP_FOO is missing from the object as it's not available in the environment when building the app.
So as is, it doesn't seem possible to provide env vars when the server is started or the JS file is served. Is there a way to tell vue-cli-service to not compile the env that way? Or any other alternative?
The only one I found so far is to replace the uses of env vars with their actual value directly in the compiled JS file when the pod starts using sed, but that's pretty ugly and could break easily.
One approach you can follow is to provide the production values when building locally. Another approach is to setup a continuous integration workflow that fetches your environment variables from wherever it is stored, builds the apps and pushes to production servers. I personally work with approach 2.
It is relatively easy to setup a github workflow that runs whenever your code is pushed to a particlar branch

How can I use environment variables if I use vuejs with CDN?

I was using Vuejs CDN to develop my app. Now, I want to separate dev and prod since the API endpoints are separated. I don't want to expose my dev API endpoint. How can I do it with the simplest way?
I tried to use dotenv. Put the DEBUG variable in .env.
var debug = process.env.DEBUG;
console.log(debug);
Vue.directive('focus', {
inserted: function (el) {
el.focus()
}
})
var vm = new Vue({...
My .env file
DEBUG=true
I can read the debug variable. However, it gives an error of "Vue is not defined" since I didn't install Vue with npm. Or I must install Vue with npm?
If you're using Vue CLI, facilities for this are already built in in the form of .env files. You'd create .env.dev and .env.production files (and probably a .env.local for local development), containing:
VUE_APP_ENDPOINT=https://...
Which in your Javascript files you can access as:
const endpoint = process.env.VUE_APP_ENDPOINT;
Then you create different builds for your two different environments:
vue-cli-service build --mode dev # or --mode production
The variables will be baked into the build, so your two different builds only contain their respective endpoints.

Missing .env in Vue CLI after run build

I'm building an app using Vue CLI 3.
I've included .env in my project and everything works fine.
But when i'm building the app for production via npm run build there is no .env file in my dist folder, so i can't change my environmental variables in production server.
Any solution or it's just fine?
This is supposed to happen. The env variables are embedded in your build.
You can make seperate .env files for production. These variables will be used during the production build.
Create a new .env file named: .env.production
Source: https://cli.vuejs.org/guide/mode-and-env.html#modes
It is normal because application needed to be recompiled when .env file changed.
For convenience you can use .env.prod or .env.dev for your CI/CD pipeline.

Vue webpack environment variables configuration

I am new to vue in general and i am trying to configure some environment variables for some projects of mine so i can do some tests with cookies but apperently i ran the simple webpack configurations when creating these projects, therefore i dont have access to the config directory to edit said variables.
I created a vue.config.js file and used the following lines:
module.exports = {
publicPath: 'myAppName'
}
However if i run it on development mode, or simply use npm run serve my app runs at "http://localhost:8080/myAppName" instead of simply "myAppName".
How do i correctly configure my environment variables for my projects without having to start over from scratch? I am using vueCli 3 btw.
I tried following these examples but none have worked:
Using Environment Variables with Vue.js
I also have .env file and a .env.development file but i am not sure what to add to it.

Vue CLI 3 creating lots of '*.hot-update.js' files. How to prevent that?

For development purposes i don't use npm run serve because i'm integrating Vue with my backend project. Instead, i wrote my own command in package.json:
"dev": "vue-cli-service build --mode development --watch"
And it all works great, but it generates tons of webpack's hot-update.js files in my build dist directory and the problem is that they don't get deleted.
Is there a way to configure vue-cli/webpack in such way that these files are automatically deleted or not even created in the first place?
I believe the development mode will automatically enable hot reloading when the watch flag is enabled. Even though without the watch flag the development mode flag on build will not include hot reloading. Confusing. I had to add this to my vue.config.js file:
module.exports = {
chainWebpack(config) {
config.plugins.delete("hmr")
},
};
Note: that will ruin
npm run serve