1. 程式人生 > >vue專案優化之使用webpack按需載入資源(require.ensure)

vue專案優化之使用webpack按需載入資源(require.ensure)

router/index.js 路由相關資訊,該路由檔案引入了多個 .vue元件
import Hello from '@/components/Hello'
import Province from '@/components/Province'
import Segment from '@/components/Segment'
import User from '@/components/User'
import Loading from '@/components/Loading'

執行 npm run build 會打包為一個整體 app.[contenthash].js ,這個檔案是非常大,可能幾兆或者幾十兆,載入會很慢,所以我們需要分模組打包,把我們想要組合在一起的元件打包到一個 chunk塊中去,分模組打包需要下面這樣使用 webpack的 require.ensure,並且在最後加入一個 chunk名,相同 chunk名字的模組將會打包到一起。

webpack中利用require.ensure()實現按需載入

1、require.ensure()

  webpack 在編譯時,會靜態地解析程式碼中的 require.ensure(),同時將模組新增到一個分開的 chunk 當中。這個新的 chunk 會被 webpack 通過 jsonp 來按需載入。

語法如下:

require.ensure(dependencies: String[], callback: function(require), chunkName: String
  • 依賴 dependencies

  這是一個字串陣列,通過這個引數,在所有的回撥函式的程式碼被執行前,我們可以將所有需要用到的模組進行宣告。

  • 回撥 callback

  當所有的依賴都載入完成後,webpack會執行這個回撥函式。require 物件的一個實現會作為一個引數傳遞給這個回撥函式。因此,我們可以進一步 require() 依賴和其它模組提供下一步的執行。

  • chunk名稱 chunkName

  chunkName 是提供給這個特定的 require.ensure() 的 chunk 的名稱。通過提供 require.ensure() 不同執行點相同的名稱,我們可以保證所有的依賴都會一起放進相同的 檔案束(bundle)。

讓我們來看以下的專案

\\ file structure
    |
    js --|
    |    |-- entry.js
| |-- a.js | |-- b.js webpack.config.js | dist
\\ entry.js

require('a');
require.ensure([], function(require){
    require('b');
});
\\ a.js
console.log('***** I AM a *****');
\\ b.js
console.log('***** I AM b *****');
\\ webpack.config.js
var path = require('path');

module.exports = function(env) {
    return {
        entry: './js/entry.js',
        output: {
            filename: 'bundle.js',
            path: path.resolve(__dirname, 'dist')
        }
    }
}

  通過執行這個專案的 webpack 構建,我們發現 webpack 建立了2個新的檔案束, bundle.js 和 0.bundle.js。

entry.js 和 a.js 被打包進 bundle.js.

b.js 被打包進 0.bundle.js.

2、require.ensure() 的坑點

(1)、空陣列作為引數

require.ensure([], function(require){
    require('./a.js');
});

以上程式碼保證了拆分點被建立,而且 a.js 被 webpack 分開打包。

(2)、依賴作為引數

require.ensure(['./a.js'], function(require) {
    require('./b.js');
});

  上面程式碼, a.js 和 b.js 都被打包到一起,而且從主檔案束中拆分出來。但只有 b.js 的內容被執行。a.js 的內容僅僅是可被使用,但並沒有被輸出。

  想去執行 a.js,我們需要非同步地引用它,如 require(‘./a.js’),讓它的 JavaScritp 被執行。

(3)、單獨打包成自己寫的名字配置 
  需要配置chunkFilename,和publicPath。publicPath是按需載入單獨打包出來的chunk是以publicPath會基準來存放的,chunkFilename:[name].js這樣才會最終生成正確的路徑和名字

module.exports={
    entry:'./js/entry.js',
    output:{
        path:path.resolve(__dirname,"./dist"),
        filename:'js/a.bundle.js',
        publicPath:"./",
        chunkFilename:'js/[name].js'
    }

所以router/index.js 修改為懶載入元件:

const Province = r => require.ensure([], () => r(require('@/components/Province.vue')), 'chunkname1')
const Segment = r => require.ensure([], () => r(require('@/components/Segment.vue')), 'chunkname2')
const Loading = r => require.ensure([], () => r(require('@/components/Loading.vue')), 'chunkname3')
const User = r => require.ensure([], () => r(require('@/components/User.vue')), 'chunkname3')

  根據 chunkame的不同, 上面的四個元件, 將會被分成3個塊打包,最終打包之後與元件相關的js檔案會分為3個 (除了app.js,manifest.js, vendor.js)

  分模組打包之後在 dist目錄下是這樣的, 這樣就把一個大的 js檔案分為一個個小的js檔案了,按需去下載,其他的使用方法和import的效果一樣