日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
百度工程師帶你了解ModuleFederation

1、什么是Module Federation(MF)?

普遍直譯為『模塊聯(lián)邦』,我們看看官網(wǎng)是怎么說(shuō)的?

十多年的掇刀網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都營(yíng)銷網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整掇刀建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)公司從事“掇刀網(wǎng)站設(shè)計(jì)”,“掇刀網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

Motivation

Multiple separate builds should form a single application. These separate builds should not have dependencies between each other, so they can be developed and deployed individually. This is often known as Micro-Frontends, but is not limited to that.

多個(gè)獨(dú)立的構(gòu)建可以形成一個(gè)應(yīng)用程序。這些獨(dú)立的構(gòu)建不會(huì)相互依賴,因此可以單獨(dú)開(kāi)發(fā)和部署它們。

這通常被稱為微前端,但并不僅限于此。

通俗點(diǎn)講,即MF提供了能在當(dāng)前應(yīng)用中遠(yuǎn)程加載其他服務(wù)器上應(yīng)用的能力。對(duì)此,可以引出下面兩個(gè)概念:

  • host:引用了其他應(yīng)用的應(yīng)用
  • remote:被其他應(yīng)用所使用的應(yīng)用

它與我們普遍討論的基座應(yīng)用、微應(yīng)用有所不同,它是去中心化的,相互之間是平等的,每個(gè)應(yīng)用是單獨(dú)部署在各自的服務(wù)器,每個(gè)應(yīng)用都可以引用其他應(yīng)用,也能被其他應(yīng)用所引用,即每個(gè)應(yīng)用可以充當(dāng)host的角色,亦可以作為remote出現(xiàn)。

2、應(yīng)用場(chǎng)景

  • 微前端:通過(guò)shared以及exposes可以將多個(gè)應(yīng)用引入同一應(yīng)用中進(jìn)行管理。
  • 資源復(fù)用,減少編譯體積:可以將多個(gè)應(yīng)用都用到的通用組件單獨(dú)部署,通過(guò)MF的功能在runtime時(shí)引入到其他項(xiàng)目中,這樣組件代碼就不會(huì)編譯到項(xiàng)目中,同時(shí)亦能滿足多個(gè)項(xiàng)目同時(shí)使用的需求,一舉兩得。

3、如何使用

項(xiàng)目結(jié)構(gòu)如下:

module-home:首頁(yè),在layout展示一個(gè)字符串。

module-layout:布局,只包含一個(gè)html模板。

module-lib:暴露工具方法,共享lodash庫(kù)。

3.1 相關(guān)配置參數(shù)一覽

3.2 各應(yīng)用的配置

// apps/module-lib/webpack.config.js
plugins: [
new ModuleFederationPlugin({
name: 'lib',
filename: 'remoteLib.js',
library: { type: 'var', name: 'lib' },
exposes: {
// 提供工具方法
'./utils': './index.js',
},
shared: ["lodash"]
})
]


// apps/module-home/webpack.config.js
plugins: [
new ModuleFederationPlugin({
name: 'home',
filename: 'remoteHome.js',
library: { type: 'var', name: 'home' },
exposes: {
// 提供掛載方法
'./mount': './index.js',
},
shared: ["lodash"]
})
]


// apps/module-layout/webpack.config.js
plugins: [
new ModuleFederationPlugin({
name: 'main',
filename: 'remoteMain.js',
remotes: {
'lib': 'lib@http://localhost:3001/remoteLib.js',
'home': 'home@http://localhost:3003/remoteHome.js',
},
shared: ['lodash']
}),
new HtmlWebpackPlugin({
template: path.resolve(__dirname, './public/index.html'),
inject: 'body'
})
]


// apps/module-layout/boot.js
import {getUid, setUid} from 'lib/utils' // 使用module-lib中暴露的方法
import {mount} from 'home/mount' // 使用module-home中暴露的掛載方法
import _ from 'lodash';
setUid();
setUid();
console.log(getUid())
console.log(_.get)


mount()

如下圖所示:在layout中展示了home掛載的節(jié)點(diǎn),控制臺(tái)也打印了調(diào)用lib中方法的log,同時(shí)lib分享的lodash也生效了(全程只加載了一個(gè)lodash)。

3.3 以remoteLib為例簡(jiǎn)要分析

// 定義全局變量
var lib;
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({


/***/ "webpack/container/entry/lib":
/*!***********************!*\
!*** container entry ***!
\***********************/
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {


eval("var moduleMap = {\n\t\"./utils\": () => {\n\t\treturn __webpack_require__.e(\"index_js\").then(() => (() => ((__webpack_require__(/*! ./index.js */ \"./index.js\")))));\n\t}\n};\nvar get = (module, getScope) => {\n\t__webpack_require__.R = getScope;\n\tgetScope = (\n\t\t__webpack_require__.o(moduleMap, module)\n\t\t\t? moduleMap[module]()\n\t\t\t: Promise.resolve().then(() => {\n\t\t\t\tthrow new Error('Module \"' + module + '\" does not exist in container.');\n\t\t\t})\n\t);\n\t__webpack_require__.R = undefined;\n\treturn getScope;\n};\nvar init = (shareScope, initScope) => {\n\tif (!__webpack_require__.S) return;\n\tvar name = \"default\"\n\tvar oldScope = __webpack_require__.S[name];\n\tif(oldScope && oldScope !== shareScope) throw new Error(\"Container initialization failed as it has already been initialized with a different share scope\");\n\t__webpack_require__.S[name] = shareScope;\n\treturn __webpack_require__.I(name, initScope);\n};\n\n// This exports getters to disallow modifications\n__webpack_require__.d(exports, {\n\tget: () => (get),\n\tinit: () => (init)\n});\n\n//# sourceURL=webpack://module-lib/container_entry?");


/***/ })


1、moduleMap:用來(lái)映射expose的模塊
2、get方法:導(dǎo)出給host應(yīng)用,用于獲取remote expose的模塊
3、init方法:導(dǎo)出給host應(yīng)用,用于將remote模塊注入


get方法中調(diào)用了__webpack_require__.e加載chunk


/******/ // This file contains only the entry chunk.
/******/ // The chunk loading function for additional chunks
/******/ __webpack_require__.e = (chunkId) => {
/******/ return Promise.all(Object.keys(__webpack_require__.f).reduce((promises, key) => {
/******/ __webpack_require__.f[key](chunkId, promises);
/******/ return promises;
/******/ }, []));
/******/ };


__webpack_require__.e調(diào)用__webpack_require__.f


/******/ __webpack_require__.f.consumes = (chunkId, promises) => {
/******/ if(__webpack_require__.o(chunkMapping, chunkId)) {
/******/ chunkMapping[chunkId].forEach((id) => {
// 如果host已經(jīng)有則直接使用,否則去remote安裝
/******/ if(__webpack_require__.o(installedModules, id)) return promises.push(installedModules[id]);
/******/ var onFactory = (factory) => {
/******/ installedModules[id] = 0;
/******/ __webpack_require__.m[id] = (module) => {
/******/ delete __webpack_require__.c[id];
/******/ module.exports = factory();
/******/ }
/******/ };
/******/ var onError = (error) => {
/******/ delete installedModules[id];
/******/ __webpack_require__.m[id] = (module) => {
/******/ delete __webpack_require__.c[id];
/******/ throw error;
/******/ }
/******/ };
/******/ try {
/******/ var promise = moduleToHandlerMapping[id]();
/******/ if(promise.then) {
/******/ promises.push(installedModules[id] = promise.then(onFactory)['catch'](onError));
/******/ } else onFactory(promise);
/******/ } catch(e) { onError(e); }
/******/ });
/******/ }
/******/ }
/******/ })();
  • host加載自己的bundle main.js,其中使用jsonp加載對(duì)應(yīng)remote提供的remoteLib.js;
  • 在remote中暴露了全局變量,host將remote注入后可以獲取對(duì)應(yīng)模塊,其中的共享模塊使用前會(huì)檢查自身有沒(méi)有,如果沒(méi)有再去加載remote的內(nèi)容。

4、拓展學(xué)習(xí)

可以學(xué)習(xí)開(kāi)源項(xiàng)目:基于 Webpack 5 Module Federation,優(yōu)雅且實(shí)用的微前端解決方案 https://github.com/yuzhanglong/mf-lite。


分享標(biāo)題:百度工程師帶你了解ModuleFederation
URL分享:http://m.5511xx.com/article/cdchgjh.html