Vue.js CLI4 Vue.config.js标准配置 (最全注释)
前言:
Vue.js CLI工具 不知不觉发展到了4.0时代,CLI给人最直白的感受是没有了build文件夹跟config文件夹,所有的配置都在Vue.config.js完成。那么该文件的配置至关重要。现在我们来看一下最新配置是怎么配置的。
有三种方式,推荐第二种标准版(无需安装依赖,直接复制即可配置)。
1、依赖库
npm install vue-cli-configjs
2、标准版
// vue.config.js
const path = require('path');
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);
const resolve = (dir) => path.join(__dirname, dir);
module.exports = {publicPath: process.env.NODE_ENV === 'production' ? '/site/vue-demo/' : '/', // 公共路径indexPath: 'index.html' , // 相对于打包路径index.html的路径outputDir: process.env.outputDir || 'dist', // 'dist', 生产环境构建文件的目录assetsDir: 'static', // 相对于outputDir的静态资源(js、css、img、fonts)目录lintOnSave: false, // 是否在开发环境下通过 eslint-loader 在每次保存时 lint 代码runtimeCompiler: true, // 是否使用包含运行时编译器的 Vue 构建版本productionSourceMap: !IS_PROD, // 生产环境的 source mapparallel: require("os").cpus().length > 1, // 是否为 Babel 或 TypeScript 使用 thread-loader。该选项在系统的 CPU 有多于一个内核时自动启用,仅作用于生产构建。pwa: {}, // 向 PWA 插件传递选项。chainWebpack: config => {config.resolve.symlinks(true); // 修复热更新失效// 如果使用多页面打包,使用vue inspect --plugins查看html是否在结果数组中config.plugin("html").tap(args => {// 修复 Lazy loading routes Errorargs[0].chunksSortMode = "none";return args;});config.resolve.alias // 添加别名.set('@', resolve('src')).set('@assets', resolve('src/assets')).set('@components', resolve('src/components')).set('@views', resolve('src/views')).set('@store', resolve('src/store'));},css: {extract: IS_PROD,requireModuleExtension: false,// 去掉文件名中的 .moduleloaderOptions: {// 给 less-loader 传递 Less.js 相关选项less: {// `globalVars` 定义全局对象,可加入全局变量globalVars: {primary: '#333'}}}},devServer: {overlay: { // 让浏览器 overlay 同时显示警告和错误warnings: true,errors: true},host: "localhost",port: 8080, // 端口号https: false, // https:{type:Boolean}open: false, //配置自动启动浏览器hotOnly: true, // 热更新// proxy: 'http://localhost:8080' // 配置跨域处理,只有一个代理proxy: { //配置多个跨域"/api": {target: "http://172.11.11.11:7071",changeOrigin: true,// ws: true,//websocket支持secure: false,pathRewrite: {"^/api": "/"}},"/api2": {target: "http://172.12.12.12:2018",changeOrigin: true,//ws: true,//websocket支持secure: false,pathRewrite: {"^/api2": "/"}},}}
}
3、升级版
// vue.config.js
const path = require('path');const CompressionWebpackPlugin = require("compression-webpack-plugin"); // 开启gzip压缩, 按需引用
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i; // 开启gzip压缩, 按需写入
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin; // 打包分析const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);
const resolve = (dir) => path.join(__dirname, dir);
module.exports = {publicPath: process.env.NODE_ENV === 'production' ? '/site/vue-demo/' : '/', // 公共路径indexPath: 'index.html' , // 相对于打包路径index.html的路径outputDir: process.env.outputDir || 'dist', // 'dist', 生产环境构建文件的目录assetsDir: 'static', // 相对于outputDir的静态资源(js、css、img、fonts)目录lintOnSave: false, // 是否在开发环境下通过 eslint-loader 在每次保存时 lint 代码runtimeCompiler: true, // 是否使用包含运行时编译器的 Vue 构建版本productionSourceMap: !IS_PROD, // 生产环境的 source mapparallel: require("os").cpus().length > 1, // 是否为 Babel 或 TypeScript 使用 thread-loader。该选项在系统的 CPU 有多于一个内核时自动启用,仅作用于生产构建。pwa: {}, // 向 PWA 插件传递选项。chainWebpack: config => {config.resolve.symlinks(true); // 修复热更新失效// 如果使用多页面打包,使用vue inspect --plugins查看html是否在结果数组中config.plugin("html").tap(args => {// 修复 Lazy loading routes Errorargs[0].chunksSortMode = "none";return args;});config.resolve.alias // 添加别名.set('@', resolve('src')).set('@assets', resolve('src/assets')).set('@components', resolve('src/components')).set('@views', resolve('src/views')).set('@store', resolve('src/store'));// 压缩图片// 需要 npm i -D image-webpack-loaderconfig.module.rule("images").use("image-webpack-loader").loader("image-webpack-loader").options({mozjpeg: { progressive: true, quality: 65 },optipng: { enabled: false },pngquant: { quality: [0.65, 0.9], speed: 4 },gifsicle: { interlaced: false },webp: { quality: 75 }});// 打包分析// 打包之后自动生成一个名叫report.html文件(可忽视)if (IS_PROD) {config.plugin("webpack-report").use(BundleAnalyzerPlugin, [{analyzerMode: "static"}]);}},configureWebpack: config => {// 开启 gzip 压缩// 需要 npm i -D compression-webpack-pluginconst plugins = [];if (IS_PROD) {plugins.push(new CompressionWebpackPlugin({filename: "[path].gz[query]",algorithm: "gzip",test: productionGzipExtensions,threshold: 10240,minRatio: 0.8}));}config.plugins = [...config.plugins, ...plugins];},css: {extract: IS_PROD,requireModuleExtension: false,// 去掉文件名中的 .moduleloaderOptions: {// 给 less-loader 传递 Less.js 相关选项less: {// `globalVars` 定义全局对象,可加入全局变量globalVars: {primary: '#333'}}}},devServer: {overlay: { // 让浏览器 overlay 同时显示警告和错误warnings: true,errors: true},host: "localhost",port: 8080, // 端口号https: false, // https:{type:Boolean}open: false, //配置自动启动浏览器hotOnly: true, // 热更新// proxy: 'http://localhost:8080' // 配置跨域处理,只有一个代理proxy: { //配置多个跨域"/api": {target: "http://172.11.11.11:7071",changeOrigin: true,// ws: true,//websocket支持secure: false,pathRewrite: {"^/api": "/"}},"/api2": {target: "http://172.12.12.12:2018",changeOrigin: true,//ws: true,//websocket支持secure: false,pathRewrite: {"^/api2": "/"}},}}
}
结语
上述代码可以直接复制,也可以按需引入,一般都用的到,注意里面需要安装的依赖。
**注意第三种方式:**可能在安装以下依赖时,会报错,建议按以下顺序安装。
npm install --save-dev compression-webpack-plugin
npm install --save-dev image-webpack-loader
Vue.js CLI4 Vue.config.js标准配置 (最全注释)相关推荐
- vue-cli的webpack模版,相关配置文件dev-server.js与webpack.config.js配置解析
1.下载vue-cli [html] view plain copy npm install vue-cli -g vue-cli的使用与详细介绍,可以到github上获取https://github ...
- vue avatar_Avatar para Vue.js组件
vue avatar 头像 (v-avatar) Este componente esta altamente inspirado y basado en el trabajo de https:// ...
- tailwindcss 官网(六)定制:配置( `tailwind.config.js `、-p、important、核心插件、`resolveConfig`)、主题 `theme` 配置
tailwindcss 官网(六)定制:配置( tailwind.config.js.-p.important.核心插件.resolveConfig).主题 theme 配置 文章目录 tailwin ...
- webpack之webpack.config.js配置
webpack之webpack.config.js配置 wbepack.config.js webpack的配置文件详解: 作用: 指示 webpack 干那些活(当你运行 webpack 指令时 ...
- 【 Vue全家桶 · Vue CLI(四)】Vue项目的详细目录结构解析
文章目录 前言 -- 一级目录解析 一. dist 二. node_modules 三. public 四. src(基础版) 4.1 main.js 4.2 App.vue 4.3 src / as ...
- Vue项目 跨域 解决方案与 vue.config.js 配置解析
为什么会出现跨域? 出于浏览器的同源策略限制.同源策略(Sameoriginpolicy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以说 ...
- vue cli3.3 以上版本配置vue.config.js
// vue.config.js 配置说明 //官方vue.config.js 参考文档 https://cli.vuejs.org/zh/config/#css-loaderoptions // 这 ...
- vue.config.js多页配置
vue.config.js多页配置,具体看代码: "use strict"; const path = require("path"); var webpack ...
- vue-cli3中的vue.config.js配置
vue-cli3中的vue.config.js配置 我的跨域是配置通过chrome浏览器的跨域设置,前端修改跨域问题,以此解决跨域的,故没有配置代理: const path = require('pa ...
最新文章
- 特斯拉无人驾驶却在高速路驰骋,四名乘客喝酒唱歌开party,网友:12分应该扣给谁?...
- 30段极简Python代码:这些小技巧你都Get了么?
- ios NSComparator 三种枚举类型
- NVIDIA AGX Xavier环境配置
- solr模糊查询_《Solr实战》之一
- 北理工在线作业计算机的主要特点是( ),北理工18秋《计算机组成原理》在线作业【答案】...
- DOM——获取元素的方式
- JProfiler 简要使用说明
- Oracle迁移PPAS:中文表名的处理
- 苹果sf字体_字体基础知识
- mysql 系统工程师_数据库系统工程师难考吗?
- 参数化三维管网建模系统MagicPipe3D
- 蓝桥杯 算法训练 调和数列问题
- CSS3火焰文字特效制作教程
- 九九乘法表 - Java (矩形、正三角形、倒三角形)
- windows 环境下 0x色彩对应表
- 我热爱计算机作文450字,热爱音乐的我作文450字
- (二十八:2021.01.10)MICCAI 2019 追踪之论文纲要(中)
- python图形界面实践_用wxPython打造Python图形界面
- 服务器性能自动化测试脚本
热门文章
- linux 怎么禁止遍历目录,linux下遍历目录功能实现
- 华为OV小米鸿蒙,华为鸿蒙开源,小米OV们会采用吗?
- c语言中通过分隔符取字符串,C语言切割多层字符串(strtok_r strtok使用方法)
- js遍历json对象
- 解决 Tomcat 添加 Cookie 域名报错问题 : CookieProcessor
- 显示当前行号、文件名和函数名(二)
- The path is not a valid path to the xx-generic kernel headers
- 循环队列及C语言实现一
- [react] 同时引用这三个库react.js、react-dom.js和babel.js它们都有什么作用?
- [react] 使用ES6的class定义的组件不支持mixins了,那用什么可以替代呢?