1

I have a config file in my vue.js + webpack + npm project

/plugins/http/GLOBAL_CONST.js

const GLOBAL_CONST = {}


GLOBAL_CONST.BASE_URL = "http://www.example.com/"  // "http://localhost:3000/"  

GLOBAL_CONST.BASE_API = "http://www.example:8000/"  //  "http://localhost:8000/"  

export default GLOBAL_CONST

I have a requirement, if I run npm run dev, the

GLOBAL_CONST.BASE_URL = "http://localhost:3000/"
GLOBAL_CONST.BASE_API = "http://localhost:8000/"

if I run npm run build it use:

GLOBAL_CONST.BASE_URL = "http://www.example.com/"  
GLOBAL_CONST.BASE_API = "http://www.example:8000/"  

is it possible to make it come true?

user7693832
  • 6,119
  • 19
  • 63
  • 114

1 Answers1

0

This is commonly done with if (process.env.NODE_ENV === 'production') (equals true for npm run build), so you could use that condition when setting BASE_URL:

GLOBAL_CONST.BASE_URL = (process.env.NODE_ENV === 'production')
                      ? "http://www.example.com/"
                      : "http://localhost:3000/"

If using Vue CLI, the NODE_ENV environment variable is automatically set for you during the build. Otherwise, you could set it yourself in your build script:

// package.json
{
  "scripts": {
    "build": "NODE_ENV=production node build.js",
    "dev": "NODE_ENV=development webpack-dev-server [...]"
  }
}
tony19
  • 125,647
  • 18
  • 229
  • 307