前言:
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