Vite配置優(yōu)雅的code?spliiting代碼分割詳解
Vite如何配置分割代碼
1.什么是代碼分割/code spliiting
前端生態(tài) rollup 和 webpack都有的概念。 如果把所有代碼都打包到一起,可能最終的代碼非常大。從而影響加載時(shí)間。
而且,很多代碼是初始加載時(shí),不需要的。因此,我們可以根據(jù)代碼使用的緊急程度,將代碼分割打包后,可以按需加載。
2.Vite 中 rollup code spliiting分割默認(rèn)方法原理
rollup code-spliiting代碼分割 默認(rèn)是由es6 esm(ECMAScript Module)的import
, export
js模塊化功能實(shí)現(xiàn)的,CommonJS標(biāo)準(zhǔn)無法實(shí)現(xiàn)。
// 提前安裝rollup npm i -g rollup
案例 目錄
├─dist └─src foo.js main.js main1.js
(1)按照動(dòng)態(tài)導(dǎo)入語句分割打包測試。
//// foo.js export default 'hello foo!'; // main.js文件 // 動(dòng)態(tài)導(dǎo)入案例1 export default function () { import('./foo.js') .then(() => { // console.log(導(dǎo)入成功); }) .catch(() => {}); }
執(zhí)行 rollup src/main.js -f cjs -d dist
打包/main.js 生成兩個(gè)文件
打包后的代碼展示
// dist\foo-xxxxxx.js 'use strict'; var foo = 'hello foo!'; exports["default"] = foo; // dist\main.js 'use strict'; // 動(dòng)態(tài)導(dǎo)入案例1 function main () { Promise.resolve().then(function () { return require('./foo-e385385a.js'); }) .then(() => { // console.log(導(dǎo)入成功); }) .catch(() => {}); } module.exports = main;
按照動(dòng)態(tài)導(dǎo)入語句分割打包測試驗(yàn)證成功。
(2)按照資源導(dǎo)入入口點(diǎn)分割打包測試。
// foo.js export default 'hello foo!';
// main.js文件 // 資源靜態(tài)導(dǎo)入案例1 import foo from './foo.js'; export default function () { console.log(foo); } // main1.js文件 // 資源靜態(tài)導(dǎo)入案例2 import foo from './foo.js'; export default function () { console.log(foo); }
執(zhí)行 rollup src/main.js src/main1.js -f cjs -d dist
打包/main.js和/main1.js文件 生成三個(gè)文件
打包后的代碼展示
// dist\foo-xxxx.js 'use strict'; var foo = 'hello foo!'; exports.foo = foo; // dist\main.js 'use strict'; var foo = require('./foo-f41bffe6.js'); // 靜態(tài)導(dǎo)入案例 function main () { console.log(foo.foo); } module.exports = main; // dist\main1.js 'use strict'; var foo = require('./foo-f41bffe6.js'); function main1 () { console.log(foo.foo); } module.exports = main1;
按照資源導(dǎo)入入口點(diǎn)分割打包測試驗(yàn)證成功。
(3)manualChunks函數(shù) 手動(dòng)自定義分割。(下面的案例)
3.如何在Vite中配置(vite.config.ts)代碼分割/code spliiting (核心關(guān)鍵)
Vite代碼分割方法1
// vite.config.ts build: { // rollup 配置 rollupOptions: { output: { // key自定義 value[] 插件同步package.json名稱 或 src/相對路徑下的指定文件 (自己可以看manualChunks ts類型) manualChunks: { // vue vue-router合并打包 vue: ['vue', 'vue-router'], echarts: ['echarts'], lodash: ['lodash'], // 兩個(gè)文件合并成一個(gè)helloWorld文件 helloWorld: ['src/components/HelloWorld.vue','src/components/HelloWorld1.vue'], ... } } } }
Vite代碼分割方法2
// vite.config.ts build: { // rollup 配置 rollupOptions: { output: { manualChunks(id: any): string { if (id.includes("style.css")) { // 需要單獨(dú)分割那些資源 就寫判斷邏輯就行 return 'src/style.css'; } if (id.includes("HelloWorld.vue")) { // 單獨(dú)分割hello world.vue文件 return 'src/components/HelloWorld.vue'; } // // 最小化拆分包 if (id.includes("node_modules")) { return id .toString() .split("node_modules/")[1] .split("/")[0] .toString(); } } } } }
以上就是Vite配置優(yōu)雅的code spliiting代碼分割詳解的詳細(xì)內(nèi)容,更多關(guān)于Vite配置code spliiting的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue3項(xiàng)目引入阿里iconfont圖標(biāo)與字體及使用教程
Iconfont國內(nèi)功能很強(qiáng)大且圖標(biāo)內(nèi)容很豐富的矢量圖標(biāo)庫,提供矢量圖標(biāo)下載、在線存儲(chǔ)、格式轉(zhuǎn)換等功能,下面這篇文章主要給大家介紹了關(guān)于Vue3項(xiàng)目引入阿里iconfont圖標(biāo)與字體及使用教程,需要的朋友可以參考下2023-05-05vue3+element?plus中利用el-menu如何實(shí)現(xiàn)路由跳轉(zhuǎn)
這篇文章主要給大家介紹了關(guān)于vue3+element?plus中利用el-menu如何實(shí)現(xiàn)路由跳轉(zhuǎn)的相關(guān)資料,在Vue?Router中我們可以使用el-menu組件來實(shí)現(xiàn)菜單導(dǎo)航,通過點(diǎn)擊菜單項(xiàng)來跳轉(zhuǎn)到不同的路由頁面,需要的朋友可以參考下2023-12-12vue.prototype和vue.use的區(qū)別和注意點(diǎn)小結(jié)
這篇文章主要介紹了vue.prototype和vue.use的區(qū)別和注意點(diǎn)小結(jié),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04el-elementUI使用el-date-picker選擇年月
本文主要介紹了el-elementUI使用el-date-picker選擇年月,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02vue+elemet實(shí)現(xiàn)表格手動(dòng)合并行列
這篇文章主要為大家詳細(xì)介紹了vue+elemet實(shí)現(xiàn)表格手動(dòng)合并行列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08VScode更新后安裝vetur仍無法格式化vue文件的解決
這篇文章主要介紹了VScode更新后安裝vetur仍無法格式化vue文件的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue中用動(dòng)態(tài)組件實(shí)現(xiàn)選項(xiàng)卡切換效果
本篇文章主要介紹了vue中用動(dòng)態(tài)組件實(shí)現(xiàn)選項(xiàng)卡切換效果,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03