For the environment variable profiles of vue-CLI projects, if they have variables with the same name, the values have the following priorities in descending order:

  1. .env.[mode].localDefinitions in files
  2. .env.[mode]Definitions in files
  3. .env.localDefinitions in files
  4. .envDefinitions in files

In short, mode is better than no mode, the same mode or no mode at all, and local is better than no local.

.env.local and.env are loaded in all environments.

In addition, environment variables that already exist when Vue CLI is started have the highest priority and are not overwritten by.env files.

The. Env environment file is loaded by running vue-cli-service, so if the environment file changes, you need to restart the service.

Also note that in a VUE-CLI project, all variables are available except NODE_ENV, BASE_URL, and variables starting with VUE_APP_

Definitions are not statically inserted into client code by webpack.definePlugin.

Dotenv rules for parsing env files, which we should follow when writing environment variable files:

The parsing engine currently supports the following rules:

  • BASIC=basic becomes {BASIC: 'basic'}
  • empty lines are skipped
  • lines beginning with # are treated as comments
  • empty values become empty strings (EMPTY= becomes {EMPTY: ''})
  • inner quotes are maintained (think JSON) (JSON={"foo": "bar"} becomes {JSON:"{"foo": "bar"}")
  • whitespace is removed from both ends of unquoted values (see more on trim) (FOO= some value becomes {FOO: 'some value'})
  • single and double quoted values are escaped (SINGLE_QUOTE='quoted' becomes {SINGLE_QUOTE: "quoted"})
  • single and double quoted values maintain whitespace from both ends (FOO=" some value " becomes {FOO: ' some value '})
  • double quoted values expand new lines (MULTILINE="new\nline" becomes
{MULTILINE: 'new
line'}
Copy the code