转载请注明出处:http://www.cnblogs.com/shamoyuu/p/vue_vux_app_7.html

项目github地址:https://github.com/shamoyuu/vue-vux-iconan

想来这个项目也做了一月了,期间部署了好几次,也经常在本地联调后台接口,所以服务器地址经常换来换去。

有一次部署的时候还把localhost给部署上去了,不得不部署第二次。

我们这一章就来解决这个问题,根据命令行参数,打包自动替换不同的服务器地址,不用再修改api.js了。

首先我们修改tools/api.js,为我们的服务器地址设置一个占位符

let apiUrl = "<<<service>>>";
let picServer = "<<<pic-service>>>"; console.info("apiUrl", apiUrl);
console.info("picServer", picServer);

因为对js的压缩不会压缩字符串,所以我们就可以通过替换占位符来做到我们上面提到的功能。

然后我们新建一个server.json文件,来保存我们各个平台,各个环境下的服务器地址

{
"webapp": {
"environments": {
"dev": {
"service": "http://localhost:18080/iconan",
"pic-service": "http://iconan.bj.bcebos.com"
},
"test": {
"service": "http://meleong.duapp.com/iconan",
"pic-service": "http://iconan.bj.bcebos.com"
},
"production": {
"service": "/iconan",
"pic-service": "http://iconan.bj.bcebos.com"
}
}
},
"mobile": {
"environments": {
"dev": {
"service": "http://localhost:18080/iconan",
"pic-service": "http://iconan.bj.bcebos.com"
},
"test": {
"service": "http://meleong.duapp.com/iconan",
"pic-service": "http://iconan.bj.bcebos.com"
},
"production": {
"service": "http://meleong.duapp.com/iconan",
"pic-service": "http://iconan.bj.bcebos.com"
}
}
}
}

这样就可以一目了然,想修改也特别方便。

然后我们修改gulpfile.js,先来通过指令的参数获取到我们想要的服务器地址

const serverConfig = require(process.cwd() + "/server.json");
global.SERVERS = serverConfig[args.t]["environments"][args.e];

然后我们来替换,如果是用gulp的管道的话,可以这么写,修改webpack.build文件

"webpack.build": (done) => {
let spinner = ora("正在打包,请稍后...");
spinner.start();
webpack(webpackConfig, (err, stats) => {
spinner.stop();
if (err) throw err;
process.stdout.write(stats.toString({
colors: true,
modules: false,
children: false, // If you are using ts-loader, setting this to true will make TypeScript errors show up during build.
chunks: false,
chunkModules: false
}) + "\n\n"); if (stats.hasErrors()) {
console.log(chalk.red(" 构建出错。\n"));
process.exit();
} // let stream = gulp.src(["dist/**/*"]);
// let servers = serverConfig[args.t]["environments"][args.e];
// for (let key in servers) {
// stream = stream.pipe(replace("<<<" + key + ">>>", servers[key]))
// }
// stream.pipe(gulp.dest("dist"))
// .on('end', function () {
// console.log(chalk.cyan(" 构建完成。\n"));
// done();
// }); done();
});
},

就是图里注释掉的那段。但是这样在开发环境下不可用,因为我们是用webpack配置的开发环境,如果你用的是gulp,可以这么做。

所以我们给webpack写一个插件来实现这个功能。

首先我们修改dev任务

const webpackDevConfig = require(process.cwd() + "/build/webpack.dev.conf");

"dev": () => {
let devCompiler = webpack(webpackDevConfig); new WebpackDevServer(devCompiler)
.listen(webpackDevConfig.devServer.port, webpackDevConfig.devServer.host, function (err) {
if (err) throw new gutil.PluginError("webpack-dev-server", err);
console.log(chalk.cyan(" 服务已启动\n"));
});
}

然后修改webpack.dev.conf最后的exports = devWebpackConfig,不要返回那个Promise。

然后我们写一个插件,新建build/plugin/servers-replace-webpack-plugin.js文件

function ServersReplaceWebpackPlugin(options) {
this.options = options;
} ServersReplaceWebpackPlugin.prototype.apply = function (compiler) {
let that = this; compiler.plugin('emit', function (compilation, callback) {
// 检查所有编译好的资源文件,替换所有需要替换的地方
for (var filename in compilation.assets) {
if (filename.endsWith(".js")) {
console.info("filename =", filename);
let newFile = compilation.assets[filename].source().toString(); let servers = that.options;
for (let key in servers) {
newFile = newFile.replace("<<<" + key + ">>>", servers[key]);
} compilation.assets[filename] = {
source: function () {
return newFile;
},
size: function () {
return newFile.length;
}
};
}
} callback();
});
}; module.exports = ServersReplaceWebpackPlugin;

然后分别为webpack.dev.conf和webpack.prod.conf添加这个插件。

它的参数就是在gulpfile.js里声明的 global.SERVERS

此系列到此结束。

完结,散花~

【前端】Vue2全家桶案例《看漫画》之七、webpack插件开发——自动替换服务器API-URL的更多相关文章

  1. 【前端】Vue2全家桶案例《看漫画》之一、添加四个导航页

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/vue_vux_app_1.html 项目github地址:https://github.com/shamoyuu/ ...

  2. 【前端】Vue2全家桶案例《看漫画》之番外篇、express上传漫画(可选)

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/vue_vux_app_extra_1.html 项目github地址:https://github.com/sha ...

  3. 【前端】Vue2全家桶案例《看漫画》之六、图片阅读页

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/vue_vux_app_6.html 项目github地址:https://github.com/shamoyuu/ ...

  4. 【前端】Vue2全家桶案例《看漫画》之四、漫画页

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/vue_vux_app_4.html 项目github地址:https://github.com/shamoyuu/ ...

  5. 【前端】Vue2全家桶案例《看漫画》之二、完成首页基本样式

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/vue_vux_app_2.html 项目github地址:https://github.com/shamoyuu/ ...

  6. 【前端】Vue2全家桶案例《看漫画》之五、引入axios

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/vue_vux_app_5.html 项目github地址:https://github.com/shamoyuu/ ...

  7. 【前端】Vue2全家桶案例《看漫画》之三、引入vuex

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/vue_vux_app_3.html 项目github地址:https://github.com/shamoyuu/ ...

  8. Vue2全家桶+Element搭建的PC端在线音乐网站

    目录 1,前言 2,已有功能 3,使用 4,目录结构 5,页面效果 登录页 首页 排行榜 歌单列表 歌单详情 歌手列表 歌手详情 MV列表 MV详情 搜索页 播放器 1,前言 项目基于Vue2全家桶及 ...

  9. Vue2全家桶之一:vue-cli(vue脚手架)超详细教程

    本文转载于:https://www.jianshu.com/p/32beaca25c0d   都说Vue2简单上手容易,的确,看了官方文档确实觉得上手很快,除了ES6语法和webpack的配置让你感到 ...

随机推荐

  1. [DeeplearningAI笔记]改善深层神经网络1.1_1.3深度学习使用层面_偏差/方差/欠拟合/过拟合/训练集/验证集/测试集

    觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.1 训练/开发/测试集 对于一个数据集而言,可以将一个数据集分为三个部分,一部分作为训练集,一部分作为简单交叉验证集(dev)有时候也成为验 ...

  2. Apache 配置SSI速记

    1. 启用模块 httpd.conf LoadModule filter_module modules/mod_filter.so 2. <Directory 的Options配置中增加Incl ...

  3. z3 巧解CTF逆向题

    z3 巧解逆向题 题目下载链接:http://reversing.kr/download.php?n=7 这次实验的题目为Reversing.kr网站中的一道题目. 题目要求: ReversingKr ...

  4. BZOJ 3329: Xorequ [数位DP 矩阵乘法]

    3329: Xorequ 题意:\(\le n \le 10^18\)和\(\le 2^n\)中满足\(x\oplus 3x = 2x\)的解的个数,第二问模1e9+7 \(x\oplus 2x = ...

  5. Apache Hadoop配置Kerberos指南

    通常,一个Hadoop集群的安全使用kerberos来进行保障.在启用Kerberos后,需要用户进行身份验证.用户通过验证后可以使用GRANT/REVOKE语句来进行基于角色的访问控制.本文介绍一下 ...

  6. Microsoft Visual Studio 中出现 Windows has triggered a breakpoint in xxx.exe的一个解决方案

    今天在用VS发布Release版本的过程中,碰到了一个问题,就是程序编译没有问题,但是在运行过程中出现了 根据经验,此类问题一般都是由于程序开发过程中的代码编写不规范导致内存写覆盖或者是使用了不同版本 ...

  7. Lua利用cjson读写json

    前言 本文结合本人的实际使用经验和代码示例,介绍如何在Lua中对json进行encode和decode.我这里采用的是Lua CJson库,是一个高性能的JSON解析器和编码器,其性能比纯Lua库要高 ...

  8. ubuntu下smb的配置

    PS:    转自Ubuntu中文论坛 -------------------------------------------------------------------------------- ...

  9. LeetCode - 626. Exchange Seats

    Mary is a teacher in a middle school and she has a table seat storing students' names and their corr ...

  10. Centos启动默认打开网络

    Centos打开网络 测试的时候发现网络没有打开,得到图像界面点击网络打开.比较麻烦去搜索了解决方法在此记录下来. 通过 /etc/sysconfig/network-script/, 编辑ifcfg ...