How can I determine if webpack.config.js
was loaded via webpack
vs webpack-dev-server
?
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.
How do I change webpack dev server's default port from 8080 to a different port? Bookmark this question. Show activity on this post.
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;
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!
I found one potential soluton:
var isDevServer = path.basename(require.main.filename) === 'webpack-dev-server.js';
credit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With