详解vue.js根据不同环境(正式、测试)打包到不同目录


Posted in Javascript onJuly 13, 2018

1、在build文件夹中创建testing.js文件

// 配置环境变量 type 为 testing
process.env.type = '"testing"'
// 引入build.js文件
require('./build')

2、修改config文件夹中的prod.env.js文件

module.exports = {
 NODE_ENV: '"production"',
 // 将上文设置的环境变量,赋值到 type 属性上
 type: process.env.type
}

3、在package.json文件中添加npm run testing命令

"testing": "node build/testing.js", // 添加testing命令
"build": "node build/build.js"

4、config ->index.js中把build这个命令复制一份改成testing(此步为了打包到不同文件夹)

build: {
  env: require('./prod.env'),
  // Template for index.html
  index: path.resolve(__dirname, '../dist/index.html'),

  // Paths
  assetsRoot: path.resolve(__dirname, '../dist'),
  assetsSubDirectory: 'static',
  assetsPublicPath: '/mshop/',

  /**
   * Source Maps
   */

  productionSourceMap: true,
  // https://webpack.js.org/configuration/devtool/#production
  devtool: '#source-map',

  // Gzip off by default as many popular static hosts such as
  // Surge or Netlify already gzip all static assets for you.
  // Before setting to `true`, make sure to:
  // npm install --save-dev compression-webpack-plugin
  productionGzip: false,
  productionGzipExtensions: ['js', 'css'],

  // Run the build command with an extra argument to
  // View the bundle analyzer report after build finishes:
  // `npm run build --report`
  // Set to `true` or `false` to always turn it on or off
  bundleAnalyzerReport: process.env.npm_config_report
 },
 testing: {
  env: require('./prod.env'),
  index: path.resolve(__dirname, '../testing/index.html'),
  assetsRoot: path.resolve(__dirname, '../testing'),
  assetsSubDirectory: 'static',
  assetsPublicPath: '/',
  productionSourceMap: true,
  // Gzip off by default as many popular static hosts such as
  // Surge or Netlify already gzip all static assets for you.
  // Before setting to `true`, make sure to:
  // npm install --save-dev compression-webpack-plugin
  productionGzip: false,
  productionGzipExtensions: ['js', 'css'],
  // Run the build command with an extra argument to
  // View the bundle analyzer report after build finishes:
  // `npm run build --report`
  // Set to `true` or `false` to always turn it on or off
  bundleAnalyzerReport: process.env.npm_config_report
 },

5、修改build->webpack.prod.conf文件

修改filename

new HtmlWebpackPlugin({

   filename: process.env.type == '"testing"' ? config.testing.index : config.build.index
  }),

修改output

output: {
  path: process.env.type == '"testing"' ? config.testing.assetsRoot : config.build.assetsRoot,
 },

6、修改build->build.js文件

路径都修改为根据正式、测试环境判断(不然执行npm run testing, npm run build命令时输出的文件有问题)

rm(path.join(process.env.type == '"testing"' ? config.testing.assetsRoot : config.build.assetsRoot, process.env.type == '"testing"' ? config.testing.assetsSubDirectory : config.build.assetsSubDirectory), err => {

7、根据不同环境配置不同域名地址

let baseURL
if (process.env.NODE_ENV === 'production') {
 if (process.env.type === 'testing') { // 测试环境
  baseUrl = '测试环境地址'
 } else {               // 正式环境
  baseUrl = '正式环境地址'
 }
} else {                // 本地环境
 baseUrl = '本地环境地址'
}

最后执行:

npm run testing 就会执行测试环境配置的地址,并生成testing文件夹
npm run build就会执行正式环境配置的地址,并生成dist文件夹

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
摘自百度的图片轮换效果代码
Nov 19 Javascript
replace()方法查找字符使用示例
Oct 28 Javascript
jquery.validate使用详解
Jun 02 Javascript
JS简单随机数生成方法
Sep 05 Javascript
JQuery遍历元素的后代和同胞实现方法
Sep 18 Javascript
jQuery progressbar通过Ajax请求实现后台进度实时功能
Oct 11 Javascript
require、backbone等重构手机图片查看器
Nov 17 Javascript
JS实现字符串转驼峰格式的方法
Dec 16 Javascript
详解Vue.js入门环境搭建
Mar 17 Javascript
Swiper 4.x 使用方法(移动端网站的内容触摸滑动)
May 17 Javascript
vue-cli webpack 引入swiper的操作方法
Sep 15 Javascript
利用layer实现表单完美验证的方法
Sep 26 Javascript
Angular5.0 子组件通过service传递值给父组件的方法
Jul 13 #Javascript
vue实现组件之间传值功能示例
Jul 13 #Javascript
微信小程序仿微信运动步数排行(交互)
Jul 13 #Javascript
jQuery实现监听下拉框选中内容发生改变操作示例
Jul 13 #jQuery
微信小程序中使用ECharts 异步加载数据实现图表功能
Jul 13 #Javascript
JS实现select选中option触发事件操作示例
Jul 13 #Javascript
AngularJs的UI组件ui-Bootstrap之Tooltip和Popover
Jul 13 #Javascript
You might like
PHP Cookie的使用教程详解
2013/06/03 PHP
Php无限级栏目分类读取的实现代码
2014/02/19 PHP
Laravel 5 框架入门(三)
2015/04/09 PHP
php微信开发之音乐回复功能
2018/06/14 PHP
PHP进阶学习之垃圾回收机制详解
2019/06/18 PHP
Packer 3.0 JS压缩及混淆工具 下载
2007/05/03 Javascript
jQuery dialog 异步调用ashx,webservice数据的代码
2010/08/03 Javascript
javascript+xml实现简单图片轮换(只支持IE)
2012/12/23 Javascript
对 jQuery 中 data 方法的误解分析
2014/06/18 Javascript
jQuery插件pagination实现无刷新分页
2016/05/21 Javascript
javascript中json对象json数组json字符串互转及取值方法
2017/04/19 Javascript
使用vue官方提供的模板vue-cli搭建一个helloWorld案例分析
2018/01/16 Javascript
vue-cli下的vuex的简单Demo图解(实现加1减1操作)
2018/02/26 Javascript
vue bus全局事件中心简单Demo详解
2018/02/26 Javascript
微信小程序入口场景的问题集合与相关解决方法
2019/06/26 Javascript
node后端服务保活的实现
2019/11/10 Javascript
Vue 中 a标签上href无法跳转的解决方式
2019/11/12 Javascript
[47:52]DOTA2-DPC中国联赛正赛 iG vs LBZS BO3 第二场 3月4日
2021/03/11 DOTA
python搭建简易服务器分析与实现
2012/12/15 Python
通过数据库对Django进行删除字段和删除模型的操作
2015/07/21 Python
Windows下Python的Django框架环境部署及应用编写入门
2016/03/10 Python
tensorflow入门之训练简单的神经网络方法
2018/02/26 Python
详解flask入门模板引擎
2018/07/18 Python
一文了解Python并发编程的工程实现方法
2019/05/31 Python
wxPython+Matplotlib绘制折线图表
2019/11/19 Python
浅析NumPy 切片和索引
2020/09/02 Python
python和node.js生成当前时间戳的示例
2020/09/29 Python
Topshop法国官网:英国快速时尚品牌
2018/04/08 全球购物
英国二手iPhone、音乐、电影和游戏商店:musicMagpie
2018/10/26 全球购物
关于美容院的活动方案
2014/08/14 职场文书
2014年感恩节活动策划方案
2014/10/06 职场文书
2014年组织部工作总结
2014/11/14 职场文书
2014年营销工作总结
2014/11/22 职场文书
员工2014年度工作总结
2014/12/09 职场文书
2016年教师师德师风承诺书
2016/03/25 职场文书
请假条应该怎么写?
2019/06/24 职场文书