Where is the process.env.BASE_URL set in vue-cli3?

when you create a template using cli3 (select vue-router), an environment variable of process.env.BASE_URL is used in router.js .

I looked up the documentation for ide/mode-and-env.html-sharp%E6%A8%A1%E5%BC%8F" rel=" nofollow noreferrer "> vue-cli3 environment variables and patterns , and found that you can only use environment variables that start with VUE_APP_ (which can be used in runnable code).

what should I do if I want to change the process.env.BASE_URL environment variable according to the pattern?

what if I don"t want to use the environment variable that starts with VUE_APP_ ?

Jun.23,2021

variables starting with this can be received, so there is no problem
"serve": "vue-cli-service serve-- open-- mode dev"
when configuring serve, you can directly call to different environments. I am looking for @ cli configuration problem to find this problem, hoping to help those in need.


configure baseUrl

in vue.config.js in the root directory.

if you want to see the transformation of the principle, you can take a look at this part of the source code. I have previously written some analysis articles related to vue-cli 3

Source code: @ vue/cli-service/lib/util/resolveClientEnv.js

module.exports = function resolveClientEnv (options, raw) {
  const env = {}
  Object.keys(process.env).forEach(key => {
    if (prefixRE.test(key) || key === 'NODE_ENV') {
      env[key] = process.env[key]
    }
  })
  env.BASE_URL = options.baseUrl

  if (raw) {
    return env
  }

  for (const key in env) {
    env[key] = JSON.stringify(env[key])
  }
  return {
    'process.env': env
  }
}

the core is:

env.BASE_URL = options.baseUrl
Menu