You can use the --define option, which takes var=value as an argument. Webpack will simply replace all occurrences with a value. For example, using the following code:
if (ENABLE_API) { // Use api } else { // Use static data }
When you run:
webpack-dev-server --inline --hot --define ENABLE_API
this will lead to:
if (true) { // Use api } else { // Use static data }
You also need to run it with --define ENABLE_API=false , otherwise it will ENABLE_API error that ENABLE_API is undefined. Since this is a simple text replacement, the value you pass will be inserted as is, so if you want to get the string, you will need to use '"value"' (note the quotes inside the quotes), otherwise it will be interpreted as normal JavaScript, but I could not get the line to work from the command line.
You can achieve the same by using webpack.DefinePlugin (I linked the webpack 2 document, but it works the same in webpack 1). In doing so, you can perform more complex replacements, as well as use utilities such as JSON.stringify to create a string version. For example, to overcome the problem of passing a string from the command line, you can use:
new webpack.DefinePlugin({ API_TYPE: JSON.stringify(process.env.API_TYPE) })
And start with the environment variable API_TYPE set to withApi :
API_TYPE=withApi webpack-dev-server --inline --hot
and each API_TYPE will be replaced with the string 'withApi' , if you don't specify it at all, it will just be undefined.
source share