1. 程式人生 > 其它 >vue專案打包流程(簡潔):

vue專案打包流程(簡潔):

技術標籤:vue

文章目錄

1.建立vue.config.js檔案:

vue-cli3 腳手架搭建完成後,專案目錄中沒有 vue.config.js 檔案,需要手動建立
在這裡插入圖片描述
在裡面配置:

module.exports = {
  productionSourceMap: false, //不輸出map檔案
}

配置後啟動打包命令:npm run build

2.開啟CDN加速:

在vue.config.js中配置:

// 是否為生產環境
const isProduction = process.env.NODE_ENV !== 'development'; // 本地環境是否需要使用cdn const devNeedCdn = false // cdn連結 const cdn = { // cdn:模組名稱和模組作用域命名(對應window裡面掛載的變數名稱) externals: { //使用了那個就寫那個 vue: 'Vue', vuex: 'Vuex', 'vue-router': 'VueRouter', 'marked': 'marked', 'highlight.js': 'hljs'
, 'nprogress': 'NProgress', 'axios': 'axios' }, // cdn的css連結 css: [ ], // cdn的js連結 js: [ 'https://cdn.bootcss.com/vue/2.6.10/vue.min.js', 'https://cdn.bootcss.com/vuex/3.1.2/vuex.min.js', 'https://cdn.bootcss.com/vue-router/3.1.3/vue-router.min.js', 'https://cdn.bootcss.com/axios/0.19.2/axios.min.js'
] } module.exports = { //第一步配置的(除了這個其餘都需要配置) productionSourceMap: false, //不輸出map檔案 chainWebpack: config => { // ============注入cdn start============ config.plugin('html').tap(args => { // 生產環境或本地需要cdn時,才注入cdn if (isProduction || devNeedCdn) args[0].cdn = cdn return args }) // ============注入cdn start============ }, configureWebpack: config => { // 用cdn方式引入,則構建時要忽略相關資源 if (isProduction || devNeedCdn) config.externals = cdn.externals } }

還需要去public中的index.html中配置:

//寫在head標籤中
 <!-- 使用CDNCSS檔案 -->
 <% for (var i in htmlWebpackPlugin.options.cdn &&
  htmlWebpackPlugin.options.cdn.css) { %>
  <link
          href="<%= htmlWebpackPlugin.options.cdn.css[i] %>"
          rel="stylesheet"
  />
  <% } %>
  <!-- 使用CDNCSS檔案 -->


  <!-- 使用CDNJS檔案 -->
 <% for (var i in htmlWebpackPlugin.options.cdn &&
  htmlWebpackPlugin.options.cdn.js) { %>
  <script src="<%= htmlWebpackPlugin.options.cdn.js[i] %>"></script>
  <% } %>
  <!-- 使用CDNJS檔案 -->

3.壓縮程式碼:

需要先安裝外掛

npm i -D uglifyjs-webpack-plugin
// 程式碼壓縮  配置在configureWebpack中
//在configureWebpack中加入
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

// 程式碼壓縮
config.plugins.push(
    new UglifyJsPlugin({
        uglifyOptions: {
            //生產環境自動刪除console
            compress: {
                drop_debugger: true,
                drop_console: true,
                pure_funcs: ['console.log']
            }
        },
        sourceMap: false,
        parallel: true
    })
)

4.公共程式碼抽離,寫在configureWebpack模組中

//跟著剛剛的程式碼壓縮後面繼續寫
// 公共程式碼抽離
config.optimization = {
   splitChunks: {
       cacheGroups: {
           vendor: {
               chunks: 'all',
               test: /node_modules/,
               name: 'vendor',
               minChunks: 1,
               maxInitialRequests: 5,
               minSize: 0,
               priority: 100
           },
           common: {
               chunks: 'all',
               test: /[\\/]src[\\/]js[\\/]/,
               name: 'common',
               minChunks: 2,
               maxInitialRequests: 5,
               minSize: 0,
               priority: 60
           },
           styles: {
               name: 'styles',
               test: /\.(sa|sc|c)ss$/,
               chunks: 'all',
               enforce: true
           },
           runtimeChunk: {
               name: 'manifest'
           }
       }
   }
}

5.骨架屏

//安裝外掛
npm install vue-skeleton-webpack-plugin

在src下新建Skeleton資料夾,其中新建index.js以及index.vue,在其中寫入以下內容,其中,骨架屏的index.vue頁面樣式請自行編輯

//index.js
import Vue from 'vue'
import home from './index.vue'
import list from './a.vue'
export default new Vue({
  components: {
    home,
    list
  },
  template: `
  <div>
   <home id="home" style="display:none"/>
   <list id="list" style="display:none"/>
  </div>
 `
})
//骨架屏頁面
<template>
  <div class="skeleton-wrapper">
    <header class="skeleton-header"></header>
    <section class="skeleton-block">
      <img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi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g4MTJ6TTgwOCAxODRoMjQydjMwSDgwOHpNOTE3IDQ4aDEzM3YzN0g5MTd6Ii8+PC9nPjwvc3ZnPg==">
    </section>
  </div>
</template>
 
<script>
  export default {
    name: 'skeleton'
  }
</script>
 
<style scoped>
  .skeleton-header {
    height: 40px;
    background: #1976d2;
    padding:0;
    margin: 0;
    width: 100%;
  }
  .skeleton-block {
    display: flex;
    flex-direction: column;
    padding-top: 8px;
  }
 
</style>
//vue.config.js
//骨架屏渲染
const SkeletonWebpackPlugin = require('vue-skeleton-webpack-plugin')
//path引入
const path = require('path')

//configureWebpack模組中寫入內容
// 骨架屏渲染
config.plugins.push(new SkeletonWebpackPlugin({
      webpackConfig: {
        entry: {
          app: path.join(__dirname, './src/Skeleton/index.js'),
        },
      },
      minimize: true,
      quiet: true,
      // 如果不設定那麼所有的路由都會共享這個骨架屏元件
      router: {
        mode: 'hash',
        // 給對應的路由設定對應的骨架屏元件,skeletonId的值根據元件設定的id
        routes: [
	      { path: '/list', skeletonId: 'home' },
	      { path: '/kc', skeletonId: 'list' },
	    ]
    }))

ok! 完成!