如何检测webpack-dev-server是否正在运行?

时间:2016-01-19 21:33:56

标签: webpack webpack-dev-server

如何通过webpack.config.js vs webpack确定是否加载了webpack-dev-server

4 个答案:

答案 0 :(得分:14)

或者:

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

或:

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

或:

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

我一直在使用后者,效果很好。一个配置FTW!

答案 1 :(得分:6)

我发现了一个潜在的解决方案:

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

credit

答案 2 :(得分:6)

webpack开发服务器will now设置了WEBPACK_DEV_SERVER环境变量,从而提供了一种更强大的检查方法。

const isDevServer = process.env.WEBPACK_DEV_SERVER;

答案 3 :(得分:3)

或使用Webpack Environment Variables

// The webpack command line
webpack-dev-server --open --env.devServer

// webpack.config.js
module.exports = env => {
  console.log('isDevServer: ', env.devServer) // true
  ...
}