详解vue.config.js文件中配置选项的配置
vue.config.js(相当于之前的webpack.config.js) 是一个可选的配置文件,如果项目的 (和package.json同级的) 根目录中存在这个文件,那么它会被@vue/cli-service自动加载。你也可以使用package.json中的vue字段,但是这种写法需要注意严格遵照 JSON 的格式来写
const path = require('path') // 引入path模块
// 封装path绝对路径拼接的函数resolve ,方便直接引入函数拼接使用
function resolve (dir) {
return path.join(__dirname, dir) // path.join(__dirname)设置绝对路径
}
//autoprefixer 是一款自动管理浏览器前缀的插件
const autoprefixer = require('autoprefixer');
// postcss-px-to-viewport是一款将px单位转换为视口单位的 (vw, vh, vmin, vmax)的PostCSS插件
const pxtoviewport = require('postcss-px-to-viewport');
const postcss-pxtorem = require('postcss-pxtorem')
// 向外导出一个文件的配置对象,在这个对象中对需要配置的选项进行配置
module.exports = {
// 执行 npm run build 统一配置文件路径(本地访问dist/index.html需'./')
// NODE_ENV:Node.js 暴露给执行脚本的系统环境变量。通常用于确定在开发环境还是生产环境
// publicPath: '/',
publicPath: process.env.NODE_ENV === 'production' ? '' : '/',
outputDir: 'dist', // 输出文件目录
// assetsDir: 'static', // 放置静态资源
// indexPath: 'index.html', // 可以不设置一般会默认
// filenameHashing:true, // 文件命名
lintOnSave: false, //设置是否在开发环境下每次保存代码时都启用 eslint验证
// runtimeCompiler: false, // 是否使用带有浏览器内编译器的完整构建版本
configureWebpack: { // 别名配置
resolve: {
alias: {
//'src': '@', 默认已配置
'assets': '@/assets',
'common': '@/common',
'components': '@/components',
'api': '@/api',
'views': '@/views',
'plugins': '@/plugins',
'utils': '@/utils',
}
}
// 使用前面可加~
},
productionSourceMap: false, //如果你不需要生产环境的 source map,可以将其设置为 false 以加速生产环境构建
// css.loaderOptions向css传递相关的loader选项,支持的loader有css-loader,postcss-loader...
css: {
loaderOptions: {
postcss: { // postcss是加载器,用来处理css的加载程序
plugins: [ // plugins译为插件
autoprefixer(),//配置插件autoprefixer,自动补全css浏览器前缀
pxtoviewport({ // postcss-px-to-viewport,将px单位转换为视口单位的PostCSS 插件.
viewportWidth: 375 //viewportWidth视口端口宽度
}),
// flexible配置
postcss-pxtorem({
rootValue: 32,// 设计稿宽度的1/10
propList: ["*"],// 需要做转化处理的属性,如`hight`、`width`、`margin`等,`*`表示全部
selectorBlackList: [".mint-"]
})
]
}
}
},
//chainWebpack是一个函数,会接收一个基于webpack-chain的ChainableConfig实例。允许对内部的 webpack 配置进行更细粒度的修改。
chainWebpack: config => {
config.resolve.alias
.set('@', resolve('./src'))
config.plugin('html')
.tap(args => {
args[0].title = '社保卡密码重置'
return args
})
config.optimization.minimizer('terser').tap((args) => {
args[0].terserOptions.compress.drop_console = true
return args
})
},
// devServer服务器配置
devServer: {
port: 2021, // 设置端口号
host: '10.32.120.69', // ip 本地
disableHostCheck: true, //是否关闭用于 DNS 重绑定的 HTTP 请求的 HOST 检查
hotOnly: false, // 热更新
https: false, // https:{type:Boolean}配置前缀
open: false, //配置自动启动浏览器
proxy: null, //设置代理
proxy: { //目的是解决跨域,若测试环境不需要跨域,则不需要进行该配置
'/api': { // 拦截以 /api 开头的url接口
target: 'https://api.taobao.cn/', //目标接口域名
changeOrigin: true, //是否跨域
ws: true, //如果要代理 websockets,配置这个参数
secure: false, // 如果是https接口,需要配置这个参数
// 标识替换
// 原请求地址为 /api/getData 将'/api'替换''时,
// 代理后的请求地址为: http://xxx.xxx.xxx/getData
// 若替换为'/other',则代理后的请求地址为 http://xxx.xxx.xxx/other/getData
"/h5api/icard-pwd": {
target: 'http://116.255.137.170:9835',
// target: 'http://192.168.2.23:8020',
changeOrigin: true,
logLevel: 'debug' //日志调试配置
},
// 统一认证模块API转发
'/h5api/auth/': {
target: 'http://116.255.137.170:9802',
changeOrigin: true,
logLevel: 'debug'
},
// 业务模块API转发
'/h5api/icard/': {
target: 'http://116.255.137.170:8020',
changeOrigin: true,
logLevel: 'debug'
},
'/h5/connect/': {
target: 'http://116.255.137.170:9800',
changeOrigin: true,
logLevel: 'debug'
},
pathRewrite: { // 标识替换
'^/api': '/', //重写接口 后台接口指向不统一 所以指向所有/
'^/api': '/api/mock'
}
}
}
},
// 这个插件中添加的service worker只在生产环境中启用(例如,只有当你运行npm run build或yarn build时)。
// 不推荐在开发模式中启用service worker,因为它会导致使用以前缓存的资产而不包括最新的本地更改的情况。
pwa: {
// serviceWorker:false,
// 允许您从一个现有的service worker文件开始,并创建一个该文件的副本,并将“预缓存清单”注入其中。
// workboxPluginMode:'InjectManifest',
// workboxOptions: {
// //swSrc: './app/sw.js', /* Empty file. */
// },
iconPaths: {
favicon32: "favicon.ico",
favicon16: "favicon.ico",
appleTouchIcon: "favicon.ico",
maskIcon: "favicon.ico",
msTileImage: "favicon.ico"
}
}
};