我怎样才能确定webpack.config.js
是否通过webpack
和webpack-dev-server
加载?
或者:
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!
我找到了一个可能的解决方
var isDevServer = path.basename(require.main.filename) === 'webpack-dev-server.js';
或者使用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
...
}