一、打开文件夹config下面的prod.env.js

将该文件里面的代码替换成下面代码,href,imgUrl,ApiUrl都是一些本地,测试,正式不一样的地址

//获取npm脚本命令(如npm run build 获取的就是build)
const target = process.env.npm_lifecycle_event;
var href = "";
var imgUrl = "";
var ApiUrl = "";
if (target == 'local') {// 本地href = '"http://www.xxx.com"'; imgUrl = '"http://www.xxx.com"'; ApiUrl = '"http://www.xxx.com"';
} else if (target == 'pretest') {// 测试href = '"http://www.xxx.com"'; imgUrl = '"http://www.xxx.com"'; ApiUrl = '"http://www.xxx.com"';
} else {// 正式href = '"http://www.xxx.com"'; imgUrl = '"http://www.xxx.com"';ApiUrl = '"http://www.xxx.com"';
}
//导出
module.exports = {NODE_ENV: '"production"',href: href,imgUrl: imgUrl,ApiUrl: ApiUrl,
}复制代码

二、同样的打开config文件夹下面的dev.env.js

将该文件里的代码替换成下面代码

var merge = require('webpack-merge')
var prodEnv = require('./prod.env')module.exports = merge(prodEnv, {NODE_ENV: '"development"',href: '"http://www.xxx.com"', imgUrl: '"http://www.xxx.com/"',ApiUrl: '"http://www.xxx.com/"',
})复制代码

三、打开main.js

加入下面这段代码

Vue.prototype.href = process.env.href
Vue.prototype.imgUrl = process.env.imgUrl
Vue.prototype.ApiUrl = process.env.ApiUrl
复制代码

四、打开文件夹config下面的index.js

加入local(本地)和pretest(测试),build(正式)是原本就有的

将assetsPublicPath的值分别改为本地,测试,和正式的地址,

注意:local下面的assetsPublicPath的值改为./

local: {env: require('./prod.env'),index: path.resolve(__dirname, '../dist/index.html'),assetsRoot: path.resolve(__dirname, '../dist'),assetsSubDirectory: 'static',// assetsPublicPath: 'http://static.cszhenghui.com/',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-pluginproductionGzip: 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 offbundleAnalyzerReport: process.env.npm_config_report},pretest: {env: require('./prod.env'),index: path.resolve(__dirname, '../dist/index.html'),assetsRoot: path.resolve(__dirname, '../dist'),assetsSubDirectory: 'static',// assetsPublicPath: 'http://static.cszhenghui.com/',assetsPublicPath: 'http://static.xxx.com/',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-pluginproductionGzip: 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 offbundleAnalyzerReport: process.env.npm_config_report},build: {env: require('./prod.env'),index: path.resolve(__dirname, '../dist/index.html'),assetsRoot: path.resolve(__dirname, '../dist'),assetsSubDirectory: 'static',assetsPublicPath: 'http://static.xxx.com/',// assetsPublicPath: 'http://static.singercat.com/',// assetsPublicPath: API_ROOT_DICT,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-pluginproductionGzip: 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 offbundleAnalyzerReport: process.env.npm_config_report},
复制代码

五、打开build文件夹下面的webpack.base.conf.js

添加下面这段代码

const target = process.env.npm_lifecycle_event;
var publicPath = "";
if (process.env.NODE_ENV === 'production') {if (target == 'local') {publicPath = config.local.assetsPublicPath} else if (target == 'pretest') {publicPath = config.pretest.assetsPublicPath} else {publicPath = config.build.assetsPublicPath}
} else {publicPath = config.dev.assetsPublicPath
}
复制代码

同时将output下面的publicPath改成下面代码那样的

module.exports = {entry: {app: './src/main.js'},output: {path: config.build.assetsRoot,filename: '[name].js',publicPath: publicPath// publicPath: process.env.NODE_ENV === 'production'//   ? config.build.assetsPublicPath//   : config.dev.assetsPublicPath},
}
复制代码

六、在build文件夹下面新建local.js和pretest.js

local.js

require('./check-versions')()process.env.NODE_ENV = 'production'var ora = require('ora')
var rm = require('rimraf')
var path = require('path')
var chalk = require('chalk')
var webpack = require('webpack')
var config = require('../config')
var webpackConfig = require('./webpack.prod.conf')var spinner = ora('building for production...')
spinner.start()rm(path.join(config.local.assetsRoot, config.local.assetsSubDirectory), err => {if (err) throw errwebpack(webpackConfig, function (err, stats) {spinner.stop()if (err) throw errprocess.stdout.write(stats.toString({colors: true,modules: false,children: false,chunks: false,chunkModules: false}) + '\n\n')console.log(chalk.cyan('  Build complete.\n'))console.log(chalk.yellow('  Tip: built files are meant to be served over an HTTP server.\n' +'  Opening index.html over file:// won\'t work.\n'))})
})复制代码

pretest.js

require('./check-versions')()process.env.NODE_ENV = 'production'var ora = require('ora')
var rm = require('rimraf')
var path = require('path')
var chalk = require('chalk')
var webpack = require('webpack')
var config = require('../config')
var webpackConfig = require('./webpack.prod.conf')var spinner = ora('building for production...')
spinner.start()rm(path.join(config.pretest.assetsRoot, config.pretest.assetsSubDirectory), err => {if (err) throw errwebpack(webpackConfig, function (err, stats) {spinner.stop()if (err) throw errprocess.stdout.write(stats.toString({colors: true,modules: false,children: false,chunks: false,chunkModules: false}) + '\n\n')console.log(chalk.cyan('  Build complete.\n'))console.log(chalk.yellow('  Tip: built files are meant to be served over an HTTP server.\n' +'  Opening index.html over file:// won\'t work.\n'))})
})
复制代码

两个文件跟build.js不同的就是下面这一句

path.join(config.pretest.assetsRoot, config.pretest.assetsSubDirectory)
复制代码

七、最后打开根目录下面的package.json文件

在scripts里面加上local和pretest这两句

  • npm run local 执行local.js (本地)

  • npm run pretest 执行pretest.js (测试)

  • npm run build 执行build.js (正式)

"scripts": {"dev": "node build/dev-server.js","start": "node build/dev-server.js","build": "node build/build.js","pretest": "node build/pretest.js","local": "node build/local.js","unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run","e2e": "node test/e2e/runner.js","test": "npm run unit && npm run e2e"
},
复制代码

转载于:https://juejin.im/post/5b33592d51882574b72f19fd

使用不同的命令去打包正式,测试,本地vue项目相关推荐

  1. vue项目打包部署到服务器,Vue项目打包部署到apache服务器的方法步骤

    vue项目在开发环境下,让项目运行起来,是通过npm run dev命令,原理是在本地搭建了一个express服务器. 但是在服务器上就不是这样的,必须要通npm run build命令来对整个项目进 ...

  2. mvn打包并部署本地tomcat_Maven项目打包成war包部署到Tomcat的方法

    Maven项目打包成war包部署到Tomcat的方法 有关于 Maven 项目的打包部署,我这里用的是 Eclipse 编辑器,以此来做个简单的记录. 实践环境 操作系统: Windows IDE: ...

  3. 打包指令_将Vue项目打包为Windows应用(.exe)

    2021年1月9日更新: 新增:解决国内用户下载electron速度太慢的问题 调整:由于electron版本升级,删除原文中对electron版本限制在2.0的命令行语句 背景 朋友是做商品零售,每 ...

  4. 【问题篇】打包Vue-cli3创建的vue项目成App的apk文件

    1.修改vue项目配置(下面的是基于脚手架3方式创建的项目的打包方式) 1.1打开cmd(管理员方式) 1.2输入 vue ui,进入脚手架管理界面 1.3点击配置进入配置界面 1.4修改公共路径为. ...

  5. webpack打开项目命令_webpack打包好的页面在项目中怎么运行?

    使用webpack打包好了页面和相关的js文件,然后命令输入webpack-dev-server,再浏览器中输入: http://localhost:9090/WebRoot... 打开页面能正常的加 ...

  6. vue 打包html静态页面,vue项目打包、vue项目打包后空白界面解决办法

    Vue脚手架提供了一个命令npm run build进行打包项目,在package.json中有一个build属性,对应执行命令node build/build.js.执行成功后,项目目录下多了一个d ...

  7. mvn打包并部署本地tomcat_maven项目自动打包部署到tomcat中

    1. 在eclipse配置tomcat,部署路径设置为tomcat的webapps目录下. 2.修改tomcat-users.xml 3.maven的settings.xml 文件打开,写入用户名和密 ...

  8. cordova+vue 项目打包成Android(apk)应用

    现在使用vue开发的项目越来越多,使用vue开发的移动端打包就成了最大的问题. 现在前端打包方案有好多种,但是综合来说,我比较喜欢用cordova来进行Android和ios的打包,配置完成之后,每次 ...

  9. 微信/企业微信-本地(MAC)VUE项目调用JS-SDK,开发测试环境搭建

    调用微信SDK前置条件 根据官方文档,前端在使用微信的接口前要先进行配置,配置信息得从后端获取,后端在计算signature时需要前端传入当前页面的URL,开发者要在公众平台设置JS SDK安全域名, ...

最新文章

  1. @FunctionalInterface
  2. iphone照片删掉又出现_两个月前删的IPhone手机照片还有救?很简单,三招帮你轻松恢复...
  3. python 线程异步执行踩坑
  4. cmail服务器安装后无法登录的解决办法
  5. Linux下grub.cnf详解
  6. 【深度学习框架】Tensorflow Session.run()函数的进一步理解
  7. mi5x的Android的版本,小米5XMIUI11最新稳定版刷机包(最新系统完整固件升级吧安卓8.0)...
  8. Windows 95/98虚拟机OS安装说明书[仅限VirtualBox]
  9. 开源中国众包平台的个人空间 工作日志 正文 关于你对软件众包的误解,你真的错了。
  10. adb shell循环命令_android adb实用命令小结
  11. 如何用python做比分网_python爬虫足球比分-yltg888
  12. CSS -- CSS字体样式、文本样式、去掉列表的小圆点、背景、背景渐变
  13. 大型园区网络解决方案-IBN
  14. 脚本语言、编程语言、中间件
  15. 【Bug小记】input:-webkit-autofill:输入框自动填充背景问题
  16. java 药店管理系统_java药店管理平台
  17. docker build过程中出错 no such host
  18. Java简单实现本地缓存
  19. Android 部分 Broadcast 篇
  20. 【Babylonjs】起步

热门文章

  1. 论文阅读9 | COCAS:一个大规模换装的行人重识别数据集
  2. 2018天池比赛首战落幕
  3. 人人都要有底层逻辑 - 底层逻辑一定要围绕人
  4. R-RCN 论文理解3
  5. win10双屏实现窗口流畅拖动
  6. 手把手教你写保研简历|计算机保研|保研夏令营文书写作|简历模板
  7. 如意验证对接TV 神,马搭建 源码,搭建简单,仅供学习交流
  8. getParentFile()方法的一些使用技巧
  9. 手势识别:使用EfficientNet模型迁移、VGG16模型迁移
  10. APP注册登录那点事