如何通过webpack.config.js
vs webpack
确定是否加载了webpack-dev-server
?
答案 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';
答案 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
...
}