日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

Vue.js CLI4 Vue.config.js标准配置 (最全注释)

發布時間:2023/12/10 vue 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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标准配置 (最全注释)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。