如何确定webpack.config.js是否通过webpack与webpack-dev-server加载?
webpack.config.js
webpack
webpack-dev-server
pod7payv1#
更新:环境变量为changed到WEBPACK_SERVE。
WEBPACK_SERVE
webpack开发服务器will now设置了WEBPACK_DEV_SERVER环境变量,允许更健壮的检查方式。
WEBPACK_DEV_SERVER
const isDevServer = process.env.WEBPACK_DEV_SERVER;
2w3kk1z52#
或者:const isDevServer = process.argv[1].indexOf('webpack-dev-server') !== -1;或:const isDevServer = process.argv.some(v => v.indexOf('webpack-dev-server') !== -1);或:const isDevServer = process.argv.some(v => v.includes('webpack-dev-server'));我一直在使用后者,效果很好。一种配置FTW!
const isDevServer = process.argv[1].indexOf('webpack-dev-server') !== -1;
const isDevServer = process.argv.some(v => v.indexOf('webpack-dev-server') !== -1);
const isDevServer = process.argv.some(v => v.includes('webpack-dev-server'));
deyfvvtc3#
我发现了一个潜在的解决方案:
var isDevServer = path.basename(require.main.filename) === 'webpack-dev-server.js';
credit
jecbmhm34#
或者使用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 ... }
lvjbypge5#
通常,webpack-dev-server与mode='development'(https://webpack.js.org/configuration/mode/#root)一起运行,而webpack与mode=production一起运行。如果是,你可以使用的值:
mode='development'
mode=production
process.env.NODE_ENV
5条答案
按热度按时间pod7payv1#
更新:环境变量为changed到
WEBPACK_SERVE
。webpack开发服务器will now设置了
WEBPACK_DEV_SERVER
环境变量,允许更健壮的检查方式。2w3kk1z52#
或者:
const isDevServer = process.argv[1].indexOf('webpack-dev-server') !== -1;
或:
const isDevServer = process.argv.some(v => v.indexOf('webpack-dev-server') !== -1);
或:
const isDevServer = process.argv.some(v => v.includes('webpack-dev-server'));
我一直在使用后者,效果很好。一种配置FTW!
deyfvvtc3#
我发现了一个潜在的解决方案:
credit
jecbmhm34#
或者使用Webpack Environment Variables:
lvjbypge5#
通常,
webpack-dev-server
与mode='development'
(https://webpack.js.org/configuration/mode/#root)一起运行,而webpack
与mode=production
一起运行。如果是,你可以使用的值: