Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if webpack-dev-server is running?

How can I determine if webpack.config.js was loaded via webpack vs webpack-dev-server?

like image 790
mpen Avatar asked Jan 19 '16 21:01

mpen


People also ask

What does webpack-dev-server do?

webpack-dev-server is Webpack's officially supported CLI-based tool for starting a static server for your assets. While you don't need any CLI tools to use Webpack, webpack-dev-server gives you a single command that starts a static server with built-in live reload.

On which port does webpack-dev-server runs by default?

How do I change webpack dev server's default port from 8080 to a different port? Bookmark this question. Show activity on this post.


3 Answers

The webpack dev server will now set the WEBPACK_DEV_SERVER environment variable, allowing for a more robust way to check.

const isDevServer = process.env.WEBPACK_DEV_SERVER; 
like image 182
Martin Barksten Avatar answered Oct 08 '22 18:10

Martin Barksten


Either:

const isDevServer = process.argv[1].indexOf('webpack-dev-server') !== -1;

or:

const isDevServer = process.argv.some(v => v.indexOf('webpack-dev-server') !== -1);

or:

const isDevServer = process.argv.some(v => v.includes('webpack-dev-server'));

I've been using the latter to a great effect. One configuration FTW!

like image 23
bjfletcher Avatar answered Oct 08 '22 20:10

bjfletcher


I found one potential soluton:

var isDevServer = path.basename(require.main.filename) === 'webpack-dev-server.js';

credit

like image 40
mpen Avatar answered Oct 08 '22 18:10

mpen