Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure environment specific variables using vue-cli

I am trying to figure out how to deploy my vue app that was generated with vue-cli 3 to production. I plan on hosting it as static files (that is no server side code). I need to set certain variables in my code based on the current environment (dev vs production). These include api-urls and authentication information (none of which is secret).

What is the best way of doing this?

Here are the config docs for vue-cli 3: https://cli.vuejs.org/config/

like image 880
Sebastian Avatar asked Dec 03 '22 11:12

Sebastian


2 Answers

You can just add your variables to existing DefinePlugin config with chainWebpack:

// vue.config.js

module.exports = {
  chainWebpack: config => {
    config
      .plugin('define')
      .tap(args => {
          args[0] = {
             ...args[0],
             "MY_API_URL": JSON.stringify(process.env.URL),
             // other stuff
          }
          return args
       })
  }
}

And configure environment variables in .env.

like image 78
Max Sinev Avatar answered Dec 10 '22 11:12

Max Sinev


You have to start the variable names with VUE_APP (https://cli.vuejs.org/guide/mode-and-env.html)

like image 34
Sebastian Avatar answered Dec 10 '22 12:12

Sebastian