比如在webpack.config.js中的plugins中加入

new webpack.DefinePlugin({
        'process.env.NODE_ENV': JSON.stringify('production'),
        IS_SIGN: JSON.stringify('prodsign')
 }),

然后就可以根据IS_SIGN来判断了

/* global IS_SIGN:true */
console.log(IS_SIGN);
switch (IS_SIGN) {
    case 'localsign':
        API = 'http://192.168.1.219:9099/';
        WEB = 'http://192.168.1.114:8085/';
        break;
    case 'testsign':
        API = 'http://192.168.1.219:9099/';
        WEB = 'http://heiyanquan.github.com/activity/20170427/test.html';
        break;
    case 'prodsign':
        API = 'http://192.168.1.219:9099/';
        WEB = 'http://heiyanquan.github.com/activity/20170427/index.html';
        break;
}

需要注意的是,如果你在webpack里整合了ESLint,那么,由于ESLint会检测没有定义的变量(ESLint要求使用全局变量时要用window.xxxxx的写法),因此需要一个global注释声明(/* global IS_PRODUCTION:true */)IS_SIGN是一个全局变量来规避warning。

posted on 2017-04-27 16:31  正正果实  阅读(847)  评论(0编辑  收藏  举报