本文介绍的是vue-cli2.x项目中,关于项目本身的一些配置。
注1:说是配置,其实是我本人对webpack了解太少,加上英文水平太差,写个注释文件方便以后查看。
注2:开发过程中有后端同学提供的接口文档,为了解决跨域问题,使用proxyTbale配置代理接口(该配置仅用于开发环境,涉及到生产环境下的跨域问题本文不做说明),部署到线上时是使用nginx反向代理的。
'use strict'
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation.
const path = require('path')
module.exports = {
dev: {
assetsSubDirectory: 'static', //编译输出的二级目录
assetsPublicPath: '/', // 编译发布的根目录
// 需要使用proxyTable代理的接口
proxyTable: {
// 接口地址
'/api': { // 配置的url请求名字开头
// 代理的目标服务器地址,这个路径是我代理到服务器,即你要请求的第三方接口
target: 'http://www.baidu.com', //这里是随便写了一个地址
// https请求需要该设置
secure: false,
// 开启代理:在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题 必须设置该项
changeOrigin: true,
// 将 '/api' 替换成 '' 重写路径运行后就代理到对应的地址
pathRewrite: { "^/api": "" }
},
//资源接口1
'/resource1': {
target: 'http://www.baidu1.com',
secure: false,
changeOrigin: true,
pathRewrite: { "^/resource1": "" }
},
//资源接口2
'/resource2': {
target: 'http://www.baidu2.com',
secure: false,
changeOrigin: true,
pathRewrite: { "^/resource2": "" }
}
},
// 开发服务器配置
host: 'localhost', // 可以被 process.env.HOST 覆盖
port: 9527, // 可以被 process.env.PORT, 如果端口被占用, 将会开启另一个自由端口
autoOpenBrowser: true, // 是否自动打开浏览器
errorOverlay: true, // 在浏览器中是否打开错误蒙层
notifyOnErrors: true, // 是否展示错误的通知
poll: false, // webpack的 devserver设置监控文件改动,但有时不能工作,可设置poll(轮询)的方式解决
useEslint: true, // 是否使用eslint检查代码
showEslintErrorsInOverlay: false, // eslint 是否以报错(非警告)的方式显示
/**
* Source Maps
*/
// https://webpack.js.org/configuration/devtool/#development
devtool: 'cheap-module-eval-source-map', // source maps 格式
// If you have problems debugging vue-files in devtools,set this to false - it *may* help https://vue-loader.vuejs.org/en/options.html#cachebusting
// 如果在devtools中调试vue-files时遇到问题,请将其设置为false 可从https://vue-loader.vuejs.org/en/options.html#cachebusting获得帮助
cacheBusting: true, // 指定是否通过在文件名称后面添加一个查询字符串来创建source map的缓存
cssSourceMap: true // 开启css的source map
},
build: {
// html文件生成的地方
index: path.resolve(__dirname, '../dist/index.html'),
// 编译生成的文件目录
assetsRoot: path.resolve(__dirname, '../dist'), // 编译后生成的文件位置
assetsSubDirectory: 'static', // 编译生成的静态文件目录
assetsPublicPath: '/', //编译发布的根目录
/**
* 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, // 是否开启生产环境的gzip压缩
productionGzipExtensions: ['js', 'css'], //开启gzip压缩后的文件的后缀名称
// 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 // 是否开启打包后的分析报告
}
}