require-ensure和require-amd的區別:javascript
require(dependencies: String[], [callback: function(...)])
require.ensure(dependencies: String[], callback: function([require]), [chunkName: String])
webpack.config.amd.jshtml
var path = require("path"); module.exports = { entry: "./example.amd.js", output: { path: path.join(__dirname, "amd"), filename: "[name].bundle.js", chunkFilename: "[id].chunk.js" } };
example.amd.jsvue
require(["./module1"], function(module1) { console.log("aaa"); var module2 = require("./module2"); console.log("bbb"); });
module1.jsjava
console.log("module1"); module.exports = 1;
module2.jswebpack
console.log("module2"); module.exports = 2;
命令行中運行webpack --config webpack.config.amd.js
- main.bundle.js
- example.amd.js
- 1.chunk.js
- module1.js
- module2.jsgit
瀏覽器中運行amd/index.html,控制檯輸出:github
module1 aaa module2 bbb
webpack.config.ensure.jsweb
var path = require("path"); module.exports = { entry: "./example.ensure.js", output: { path: path.join(__dirname, "ensure"), filename: "[name].bundle.js", chunkFilename: "[name].chunk.js" } };
example.ensure.jsvue-router
require.ensure(["./module1"], function(require) { console.log("aaa"); var module2 = require("./module2"); console.log("bbb"); require("./module1"); }, 'test');
module1.js
同上vue-cli
命令行中運行webpack --config webpack.config.ensure.js
- main.bundle.js
- example.amd.js
- 1.chunk.js
- module1.js
- module2.js
瀏覽器中運行ensure/index.html,控制檯輸出:
aaa module2 bbb module1
webpack.config.ensure.chunk.js
var path = require("path"); module.exports = { entry: "./example.ensur.chunk.js", output: { path: path.join(__dirname, "ensure-chunk"), filename: "[name].bundle.js", chunkFilename: "[name].chunk.js" } };
example.ensur.chunk.js
8require.ensure(["./module1"], function(require) { console.log("aaa"); require("./module1"); console.log("bbb"); }, 'common'); require.ensure(["./module2"], function(require) { console.log("ccc"); require("./module2"); console.log("ddd"); }, 'common')
module1.js
同上
命令行中運行webpack --config webpack.config.ensure.js
- main.bundle.js
- example.amd.js
- 1.chunk.js
- module1.js
- module2.js
瀏覽器中運行ensure/index.html,控制檯輸出:
aaa
module1
bbb
ccc
1module2 ddd
使用 vue-cli構建的項目,在 默認狀況下 ,執行 npm run build 會將全部的js代碼打包爲一個總體,
打包位置是 dist/static/js/app.[contenthash].js
相似下面的路由代碼
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名字的模塊將會打包到一塊兒
router/index.js 修改成懶加載組件
const Province = r => require.ensure([], () => r(require('@/components/Province.vue')), 'chunkname1')
const Segment = r => require.ensure([], () => r(require('@/components/Segment.vue')), 'chunkname1')
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的效果同樣
參考vue-router官方文檔: https://router.vuejs.org/zh-cn/advanced/lazy-loading.html