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

问题描述 投票:10回答:3

我怎样才能确定webpack.config.js是否通过webpackwebpack-dev-server加载?

webpack webpack-dev-server
3个回答
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!


6
投票

我找到了一个可能的解决方

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

credit


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
  ...
}
© www.soinside.com 2019 - 2024. All rights reserved.