详解vue.js根据不同环境(正式、测试)打包到不同目录
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文件中添加npmruntesting命令
"testing":"nodebuild/testing.js",//添加testing命令 "build":"nodebuild/build.js"
4、config->index.js中把build这个命令复制一份改成testing(此步为了打包到不同文件夹)
build:{
env:require('./prod.env'),
//Templateforindex.html
index:path.resolve(__dirname,'../dist/index.html'),
//Paths
assetsRoot:path.resolve(__dirname,'../dist'),
assetsSubDirectory:'static',
assetsPublicPath:'/mshop/',
/**
*SourceMaps
*/
productionSourceMap:true,
//https://webpack.js.org/configuration/devtool/#production
devtool:'#source-map',
//Gzipoffbydefaultasmanypopularstatichostssuchas
//SurgeorNetlifyalreadygzipallstaticassetsforyou.
//Beforesettingto`true`,makesureto:
//npminstall--save-devcompression-webpack-plugin
productionGzip:false,
productionGzipExtensions:['js','css'],
//Runthebuildcommandwithanextraargumentto
//Viewthebundleanalyzerreportafterbuildfinishes:
//`npmrunbuild--report`
//Setto`true`or`false`toalwaysturnitonoroff
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,
//Gzipoffbydefaultasmanypopularstatichostssuchas
//SurgeorNetlifyalreadygzipallstaticassetsforyou.
//Beforesettingto`true`,makesureto:
//npminstall--save-devcompression-webpack-plugin
productionGzip:false,
productionGzipExtensions:['js','css'],
//Runthebuildcommandwithanextraargumentto
//Viewthebundleanalyzerreportafterbuildfinishes:
//`npmrunbuild--report`
//Setto`true`or`false`toalwaysturnitonoroff
bundleAnalyzerReport:process.env.npm_config_report
},
5、修改build->webpack.prod.conf文件
修改filename
newHtmlWebpackPlugin({
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文件
路径都修改为根据正式、测试环境判断(不然执行npmruntesting,npmrunbuild命令时输出的文件有问题)
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、根据不同环境配置不同域名地址
letbaseURL
if(process.env.NODE_ENV==='production'){
if(process.env.type==='testing'){//测试环境
baseUrl='测试环境地址'
}else{//正式环境
baseUrl='正式环境地址'
}
}else{//本地环境
baseUrl='本地环境地址'
}
最后执行:
npmruntesting就会执行测试环境配置的地址,并生成testing文件夹
npmrunbuild就会执行正式环境配置的地址,并生成dist文件夹
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短