Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass command-line arguments to vue.config.js?

I’m trying to configure a vue project so that the webpack-dev-server proxies requests for /data to one of two hosts, depending on which npm-run command is invoked:

npm run serve-foo
npm run serve-bar

Here is the configuration:

// package.json
{
  scripts: {
    'serve-foo': 'vue-cli-service serve -- http://192.168.7.2',
    'serve-bar': 'vue-cli-service serve -- http://192.168.7.3',
}

and

// vue.config.js

// prints "node.exe vue-cli-service.js serve -- http://192.168.7.2"
console.log(...process.argv); 

module.exports = {
  devServer: {
    // ...
    proxy: {
      '/data': {
        target: process.argv[4]
      }
    }
  }
}

Now when I run npm run serve-foo the following error occurs. What am I doing wrong? Does vue-cli-service not support using -- to pass through arguments?

This dependency was not found:

C:\Project\http:\192.168.7.2 in multi (webpack)-dev-server/client?http://192.168.0.5:80/sockjs-node (webpack)/hot/dev-server.js ./http:/192.168.7.2, multi (webpack)-dev-server/client?http://localhost:80 (webpack)/hot/dev-server.js (webpack)-dev-server/client?http://192.168.0.5:80/sockjs-node ./http:/192.168.7.2
To install it, you can run: npm install --save C:\Project\http:\192.168.7.2
like image 656
AndyMcoy Avatar asked Apr 20 '20 01:04

AndyMcoy


1 Answers

You can achieve this via environment variables:

// package.json
{
  scripts: {
    'serve-foo': 'export BACKEND_URL=http://192.168.7.2 && vue-cli-service serve',
    'serve-bar': 'export BACKEND_URL=http://192.168.7.3 && vue-cli-service serve',
}

and

// vue.config.js    
module.exports = {
  devServer: {
    // ...
    proxy: {
      '/data': {
        target: process.env.BACKEND_URL
      }
    }
  }
}
like image 106
thSoft Avatar answered Oct 17 '22 20:10

thSoft