Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack dev server custom parameters from command line

I want to pass custom parameters from command line. Something like this:

webpack-dev-server --inline --hot --customparam1=value

Exactly what I am trying to achieve is that I am working on a vue-loader application. The application has certain services that fetch data.

I have another application that acts as the api server. We need the application to run in 2 ways (because all members of our team don't have access to the api server)

So that service has 2 ways to get data:

1) If api server is running(for dev team), use http calls to get the data from localhost

2) If api server is not running(for design team), simply use static data already there in services:

var someData;
if (customparam1 === "withApi"){
   someData=getData("http://localhost:8081/apiendpoint");
} else {
   someData=[
       {a:b},
       {c:d},
       // more custom array of static data
       .....
   ]
}

So this customparam1 is supposed to be passed from webpack-dev-server command line and as per this documentation, no such way exists that I could find(did I miss something?)

How do I do it?

PS: I am on webpack 1.12.1

like image 895
rahulserver Avatar asked Mar 16 '17 17:03

rahulserver


People also ask

Is webpack CLI required?

If you're using webpack v4 or later and want to call webpack from the command line, you'll also need to install the CLI.

How do you pass arguments to Webpacks?

The easiest way, in my opinion, to pass arguments is to use Node. Webpack being the one receiving the arguments, you can save your command line arguments in a dedicated environment variable (which exists only within the session): // webpack. config.

How can we generate webpack config file automatically?

Use webpack-cli's init command to rapidly generate webpack configuration files for your project requirements, it will ask you a couple of questions before creating a configuration file. npx might prompt you to install @webpack-cli/init if it is not yet installed in the project or globally.


1 Answers

Update: 2020 / 2 / 16

1. webpack^4

If you are using webpack4 or the latest one, using the environment variables is very handy!

The webpack command line environment option --env allows you to pass in as many environment variables as you like. Environment variables will be made accessible in your webpack.config.js. For example, --env.production or --env.NODE_ENV=local (NODE_ENV is conventionally used to define the environment type, see here.)

For example, the command is like:

env.NODE_ENV=development webpack-dev-server

Inside the webpack.config.js, we can define the env variable inside new webpack.DefinePlugin() to make it available in the app.

webpack.config.js:(plugin)

// pass the env and return the webpack setting
module.exports = env => {  
  console.log(env);

  return {
    ....
    plugins: [
      new HtmlWebpackPlugin({
        template: "./public/index.pug",
        inject: false
      }),
      new webpack.DefinePlugin({ customparam1: JSON.stringify(env.customparam1) })
    ],

        }
      ]
     ....
    }
  };
};


2. create-react-app

Check react-create-app doc, the variable will start with REACT_APP_

command: REACT_APP_CUSTOM_VARIABLE=234 npm run dev

app:

console.log(process.env.REACT_APP_CUSTOM_VARIABLE) // 234

3. old answer

command:

webpack-dev-server  --customparam1=value

webpack.config.js:

var path = require("path");
var webpack = require("webpack");

function findPara(param){
    let result = '';
    process.argv.forEach((argv)=>{
        if(argv.indexOf('--' + param) === -1) return;
        result = argv.split('=')[1];
    });
    return  result;
}

const customparam1 = findPara('customparam1');

module.exports = {
   ...
    plugins: [
        new webpack.DefinePlugin({ 
            customparam1:JSON.stringify(customparam1) 
        })
    ]
};

main js file:

if(customparam1 === xxx){ ... }
like image 198
AppleJam Avatar answered Sep 17 '22 17:09

AppleJam