最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue項(xiàng)目打包優(yōu)化的方法實(shí)戰(zhàn)記錄

 更新時(shí)間:2022年08月25日 10:49:16   作者:一只小菜雞111  
最近入職了新公司,接手了一個(gè)新拆分出來的Vue項(xiàng)目,針對該項(xiàng)目做了個(gè)打包優(yōu)化,把經(jīng)驗(yàn)分享出來,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目打包優(yōu)化的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

1.按需加載第三方庫

例如 ElementUI、lodash 等

a, 裝包

npm install babel-plugin-component -D

b, babel.config.js

module.exports = {
  "presets": [
    "@vue/cli-plugin-babel/preset"
  ],
  "plugins": [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}

c, main.js

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)

換成

import './plugins/element.js'

element.js

import Vue from 'vue'
import { Button, Form, FormItem, Input, Message, Header, Container, Aside, Main, Menu, Submenu, MenuItemGroup, MenuItem, Breadcrumb, BreadcrumbItem, Card, Row, Col, Table, TableColumn, Switch, Tooltip, Pagination, Dialog, MessageBox, Tag, Tree, Select, Option, Cascader, Alert, Tabs, TabPane, Steps, Step, CheckboxGroup, Checkbox, Upload, Timeline, TimelineItem } from 'element-ui'
 
Vue.use(Button)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input)
Vue.use(Header)
Vue.use(Container)
Vue.use(Aside)
Vue.use(Main)
Vue.use(Menu)
Vue.use(Submenu)
Vue.use(MenuItemGroup)
Vue.use(MenuItem)
Vue.use(Breadcrumb)
Vue.use(BreadcrumbItem)
Vue.use(Card)
Vue.use(Row)
Vue.use(Col)
Vue.use(Table)
Vue.use(TableColumn)
Vue.use(Switch)
Vue.use(Tooltip)
Vue.use(Pagination)
Vue.use(Dialog)
Vue.use(Tag)
Vue.use(Tree)
Vue.use(Select)
Vue.use(Option)
Vue.use(Cascader)
Vue.use(Alert)
Vue.use(Tabs)
Vue.use(TabPane)
Vue.use(Steps)
Vue.use(Step)
Vue.use(CheckboxGroup)
Vue.use(Checkbox)
Vue.use(Upload)
Vue.use(Timeline)
Vue.use(TimelineItem)
 
// 把彈框組件掛著到了 vue 的原型對象上,這樣每一個(gè)組件都可以直接通過 this 訪問
Vue.prototype.$message = Message
Vue.prototype.$confirm = MessageBox.confirm

效果圖 

優(yōu)化 按需加載第三方工具包(例如 lodash)或者使用 CDN 的方式進(jìn)行處理。

按需加載使用的工具方法  (當(dāng)用到的工具方法少時(shí)按需加載打包)  用到的較多通過cdn 

通過form lodash 搜索 哪處用到

例如此處的 1.

換成 

按需導(dǎo)入 

效果圖

2.移除console.log

npm i babel-plugin-transform-remove-console -D

 babel.config.js

const prodPlugins = []
 
if (process.env.NODE_ENV === 'production') {
  prodPlugins.push('transform-remove-console')
}
 
module.exports = {
  presets: ['@vue/cli-plugin-babel/preset'],
  plugins: [
    [
      'component',
      {
        libraryName: 'element-ui',
        styleLibraryName: 'theme-chalk'
      }
    ],
    ...prodPlugins
  ]
}

 效果圖

3. Close SourceMap

生產(chǎn)環(huán)境關(guān)閉 功能

vue.config.js

module.exports = {
  productionSourceMap: false
}

 效果圖

4. Externals && CDN

通過 externals 排除第三方 JS 和 CSS 文件打包,使用 CDN 加載。

vue.config.js

module.exports = {
  productionSourceMap: false,
  chainWebpack: (config) => {
    config.when(process.env.NODE_ENV === 'production', (config) => {
      const cdn = {
        js: [
          'https://cdn.staticfile.org/vue/2.6.11/vue.min.js',
          'https://cdn.staticfile.org/vue-router/3.1.3/vue-router.min.js',
          'https://cdn.staticfile.org/axios/0.18.0/axios.min.js',
          'https://cdn.staticfile.org/echarts/4.1.0/echarts.min.js',
          'https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.js',
          'https://cdn.staticfile.org/quill/1.3.4/quill.min.js',
          'https://cdn.jsdelivr.net/npm/vue-quill-editor@3.0.4/dist/vue-quill-editor.js'
        ],
        css: [
          'https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.css',
          'https://cdn.staticfile.org/quill/1.3.4/quill.core.min.css',
          'https://cdn.staticfile.org/quill/1.3.4/quill.snow.min.css',
          'https://cdn.staticfile.org/quill/1.3.4/quill.bubble.min.css'
        ]
      }
      config.set('externals', {
        vue: 'Vue',
        'vue-router': 'VueRouter',
        axios: 'axios',
        echarts: 'echarts',
        nprogress: 'NProgress',
        'nprogress/nprogress.css': 'NProgress',
        'vue-quill-editor': 'VueQuillEditor',
        'quill/dist/quill.core.css': 'VueQuillEditor',
        'quill/dist/quill.snow.css': 'VueQuillEditor',
        'quill/dist/quill.bubble.css': 'VueQuillEditor'
      })
      config.plugin('html').tap((args) => {
        args[0].isProd = true
        args[0].cdn = cdn
        return args
      })
    })
  }
}

public/index.html

<!DOCTYPE html>
<html lang="en">
 
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width,initial-scale=1.0">
  <link rel="icon" href="<%= BASE_URL %>favicon.ico">
  <title>
    <%= htmlWebpackPlugin.options.title %>
  </title>
  <% if(htmlWebpackPlugin.options.isProd){ %>
    <% for(var css of htmlWebpackPlugin.options.cdn.css) { %>
      <link rel="stylesheet" href="<%=css%>">
      <% } %>
        <% } %>
</head>
 
<body>
  <noscript>
    <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled.
        Please enable it to continue.</strong>
  </noscript>
  <div id="app"></div>
  <!-- built files will be auto injected -->
  <% if(htmlWebpackPlugin.options.isProd){ %>
    <% for(var js of htmlWebpackPlugin.options.cdn.js) { %>
      <script src="<%=js%>"></script>
      <% } %>
        <% } %>
</body>
 
</html>

效果圖

繼續(xù)對 ElementUI 的加載方式進(jìn)行優(yōu)化 

vue.config.js

module.exports = {
  chainWebpack: config => {
    config.when(process.env.NODE_ENV === 'production', config => {
      config.set('externals', {
        './plugins/element.js': 'ELEMENT'
      })
      config.plugin('html').tap(args => {
        args[0].isProd = true
        return args
      })
    })
  }
}

 public/index.html

<!DOCTYPE html>
<html lang="en">
 
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width,initial-scale=1.0">
  <link rel="icon" href="<%= BASE_URL %>favicon.ico">
  <title>
    <%= htmlWebpackPlugin.options.isProd ? '' : 'dev - ' %>電商后臺(tái)管理系統(tǒng)
  </title>
  <% if(htmlWebpackPlugin.options.isProd){ %>
    <!-- element-ui 的樣式表文件 -->
    <link rel="stylesheet"  />
 
    <!-- element-ui 的 js 文件 -->
    <script src="https://cdn.staticfile.org/element-ui/2.13.0/index.js"></script>
    <% } %>
</head>
 
<body>
  <noscript>
    <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled.
        Please enable it to continue.</strong>
  </noscript>
  <div id="app"></div>
  <!-- built files will be auto injected -->
</body>
 
</html>

效果圖

5.路由懶加載的方式

import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
Vue.use(VueRouter)
 
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/Home',
    component: () => import('../components/Home.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: () => import('../components/Welcome.vue')
      },
      {
        path: '/users',
        component: () => import('../components/user/Users.vue')
      },
      {
        path: '/rights',
        component: () => import('../components/power/Rights.vue')
      },
      {
        path: '/roles',
        component: () => import('../components/power/Roles.vue')
      },
      {
        path: '/categories',
        component: () => import('../components/goods/Cate.vue')
      },
      {
        path: '/params',
        component: () => import('../components/goods/Params.vue')
      },
      {
        path: '/goods',
        component: () => import('../components/goods/List.vue')
      },
      {
        path: '/goods/add',
        component: () => import('../components/goods/Add.vue')
      },
      {
        path: '/orders',
        component: () => import('../components/order/Order.vue')
      },
      {
        path: '/reports',
        component: () => import('../components/report/Report.vue')
      }
    ]
  }
]
 
const router = new VueRouter({
  routes
})
 
router.beforeEach((to, from, next) => {
  // to 要訪問的路徑
  // from 從哪里來的
  // next() 直接放行,next('/login') 表示跳轉(zhuǎn)
  // 要訪問 /login 的話那直接放行
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  // token 不存在那就跳轉(zhuǎn)到登錄頁面
  if (!tokenStr) return next('/login')
  // 否則 token 存在那就放行
  next()
})
 
export default router

其他:圖片壓縮、CSS 壓縮和提取、JS 提取...

1.部署到 Nginx

下載 Nginx,雙擊運(yùn)行 nginx.exe,瀏覽器輸入 localhost 能看到界面表示服務(wù)啟動(dòng)成功!

前端 axios 中的 baseURL 指定為 /api,配置 vue.config.js 代理如下

module.exports = {
  devServer: {
    proxy: {
      '/api': {
        target: 'http://127.0.0.1:8888',
        changeOrigin: true
      }
    }
  }
}

路由模式、基準(zhǔn)地址、404 記得也配置一下

const router = new VueRouter({
  mode: 'history',
  base: '/shop/',
  routes: [
    // ...
    {
      path: '*',
      component: NotFound
    }
  ]
})

執(zhí)行 npm run build 打包,把 dist 中的內(nèi)容拷貝到 Nginx 的 html 文件夾中

修改 Nginx 配置

http {
    server {
        listen       80;
 
        location / {
            # proxy_pass https://www.baidu.com;
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
        location /api {
            # 重寫地址
            # rewrite ^.+api/?(.*)$ /$1 break;
            # 代理地址
            proxy_pass http://127.0.0.1:8888;
            # 不用管
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}

重啟服務(wù)

nginx -s reload

訪問 localhost 查看下效果吧

開啟 Gzip 壓縮

前端通過 vue.config.js 配置,打包成帶有 gzip 的文件

const CompressionWebpackPlugin = require('compression-webpack-plugin')
 
module.exports = {
  configureWebpack: config => {
    if (process.env.NODE_ENV === 'production') {
      config.plugins = [...config.plugins, new CompressionWebpackPlugin()]
    }
  }
}

Nginx 中開啟 gzip 即可

總結(jié)

到此這篇關(guān)于vue項(xiàng)目打包優(yōu)化的文章就介紹到這了,更多相關(guān)vue項(xiàng)目打包優(yōu)化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue中render方法的使用詳解

    Vue中render方法的使用詳解

    這篇文章主要為大家詳細(xì)介紹了Vue中render方法的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 從零實(shí)現(xiàn)一個(gè)vue文件解析器

    從零實(shí)現(xiàn)一個(gè)vue文件解析器

    本文就討論下怎么實(shí)現(xiàn)一個(gè)處理.vue文件的loader,以及用loader處理完.vue文件怎么把內(nèi)容渲染在瀏覽器上并實(shí)現(xiàn)簡單的響應(yīng)式,對vue文件解析器相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-06-06
  • vue父子組件slot插槽的使用

    vue父子組件slot插槽的使用

    這篇文章主要介紹了vue父子組件slot插槽的使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • KKFileView結(jié)合vue多格式文件在線預(yù)覽功能實(shí)現(xiàn)

    KKFileView結(jié)合vue多格式文件在線預(yù)覽功能實(shí)現(xiàn)

    kkFileView是git的開源在線文件預(yù)覽項(xiàng)目,這篇文章主要介紹了KKFileView結(jié)合vue多格式文件在線預(yù)覽功能,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-02-02
  • vue前端獲取本地IP地址代碼實(shí)例

    vue前端獲取本地IP地址代碼實(shí)例

    再做前端頁面的時(shí)候,想獲取本地的ip地址,可能是為了和服務(wù)器通信,可能是為了展示,無論哪種,下面給大家總結(jié)下方法,這篇文章主要給大家介紹了關(guān)于vue前端獲取本地IP地址的相關(guān)資料,需要的朋友可以參考下
    2024-05-05
  • 簡易Vue評論框架的實(shí)現(xiàn)(父組件的實(shí)現(xiàn))

    簡易Vue評論框架的實(shí)現(xiàn)(父組件的實(shí)現(xiàn))

    本篇文章主要介紹了簡易 Vue 評論框架的實(shí)現(xiàn)(父組件的實(shí)現(xiàn)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • Vue.js函數(shù)式組件的全面了解

    Vue.js函數(shù)式組件的全面了解

    函數(shù)式組件就是函數(shù)是組件,組件是函數(shù),它的特征是沒有內(nèi)部狀態(tài)、沒有生命周期鉤子函數(shù)、沒有this(不需要實(shí)例化的組件),這篇文章主要給大家介紹了關(guān)于Vue.js函數(shù)式組件的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • vue使用less報(bào)錯(cuò):Inline JavaScript is not enabled問題

    vue使用less報(bào)錯(cuò):Inline JavaScript is not ena

    這篇文章主要介紹了vue使用less報(bào)錯(cuò):Inline JavaScript is not enabled問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 淺談Vue路由快照實(shí)現(xiàn)思路及其問題

    淺談Vue路由快照實(shí)現(xiàn)思路及其問題

    這篇文章主要介紹了淺談Vue路由快照實(shí)現(xiàn)思路及其問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-06-06
  • Vant彈出列表多選輸入框下拉選擇代碼(可直接復(fù)制使用)

    Vant彈出列表多選輸入框下拉選擇代碼(可直接復(fù)制使用)

    vue項(xiàng)目無論是用element中的Select選擇器,還是使用公司維護(hù)的組件,都可以輕松實(shí)現(xiàn)單選和多選的需求,這篇文章主要給大家介紹了關(guān)于Vant彈出列表多選輸入框下拉選擇的相關(guān)資料,需要的朋友可以參考下
    2024-01-01

最新評論

盱眙县| 祥云县| 凤庆县| 若尔盖县| 当雄县| 峡江县| 彭山县| 阳泉市| 崇文区| 上栗县| 隆化县| 公主岭市| 龙南县| 正蓝旗| 桐乡市| 石楼县| 靖宇县| 通海县| 大理市| 安义县| 无极县| 崇礼县| 麻栗坡县| 天门市| 博罗县| 桐城市| 法库县| 集安市| 广宁县| 东乌珠穆沁旗| 嘉黎县| 满洲里市| 蒙自县| 承德市| 云安县| 阿巴嘎旗| 特克斯县| 南郑县| 南和县| 兰考县| 新巴尔虎右旗|