How to use cross-env in VUe-CLI

Download the cross – env

yarn add --dev cross-env
Copy the code

Modify the package.json file

  "serve": "cross-env RUNTYPE=test vue-cli-service serve".Copy the code

Modify (or create) vue.config.js

module.exports = {
  chainWebpack: (config) = > {
    config.plugin('define').tap((args) = > {
      args[0] ['process.env'] ['SELF_CONFIG'] = JSON.stringify(process.env);
      returnargs; }); }};Copy the code

Use custom variables

After running the project, the custom RUNTYPE variable in package.json is injected into the global variable process.env.self_config

// a.js
console.log('process.env', process.env.SELF_CONFIG.RUNTYPE);
// test
Copy the code