Webpack 系列第 3 部分
珍惜时间,勤奋学习!今天给大家带来《Webpack 系列第 3 部分》,正文内容主要涉及到等等,如果你正在学习文章,或者是对文章有疑问,欢迎大家关注我!后面我会持续更新相关内容的,希望都能帮到正在学习的大家!
请查找该系列的旧部分以充分理解这个概念。
- webpack 5 系列第 1 部分
- webpack 5 系列第 2 部分
- webpack 5 系列第 4 部分
在线电子商店申请
让我们使用微前端来构建一个在线商店应用程序以实现模块化。每个微前端将代表商店的不同部分,它们将共享公共库,例如 react、设计系统和共享实用程序库。
目标:
- productlist 公开可以由其他应用导入和使用的产品列表。
- 购物车公开了从购物车添加/删除产品的功能。
- 结帐 使用购物车中的数据并处理结帐。
模块联合的配置
- 微前端1:产品列表
公开 productlist 组件以供其他微前端使用。
// webpack.config.js (productlist) const { modulefederationplugin } = require('webpack').container; module.exports = { plugins: [ new modulefederationplugin({ name: 'productlistapp', filename: 'remoteentry.js', exposes: { './productlist': './src/productlist', }, shared: { react: { singleton: true, eager: true }, 'react-dom': { singleton: true, eager: true }, // share any other libraries like a ui library, e.g., material-ui }, }), ], };
- 微前端 2:购物车
公开 cart 组件,它使用共享状态库(如 zustand)进行购物车管理。
// webpack.config.js (cart) const { modulefederationplugin } = require('webpack').container; module.exports = { plugins: [ new modulefederationplugin({ name: 'cartapp', filename: 'remoteentry.js', exposes: { './cart': './src/cart', }, shared: { react: { singleton: true, eager: true }, 'react-dom': { singleton: true, eager: true }, zustand: { singleton: true }, // zustand or redux for shared state }, }), ], };
- 微前端3:结账
使用 cart 和 productlist 组件以在结帐前显示摘要。
// webpack.config.js (checkout) const { modulefederationplugin } = require('webpack').container; module.exports = { plugins: [ new modulefederationplugin({ name: 'checkoutapp', remotes: { productlistapp: 'productlistapp@http://localhost:3001/remoteentry.js', cartapp: 'cartapp@http://localhost:3002/remoteentry.js', }, shared: { react: { singleton: true, eager: true }, 'react-dom': { singleton: true, eager: true }, }, }), ], };
组件实现:
- 微前端1:产品列表
由 productlist 微前端公开。
// src/productlist.js (productlist) import react from 'react'; const products = [ { id: 1, name: 'product 1', price: 50 }, { id: 2, name: 'product 2', price: 75 }, ]; const productlist = () => (); export default productlist;products
{products.map(product => (
- {product.name} - ${product.price}
))}
- 微前端 2:购物车
由 cart 微前端公开并管理状态(例如,使用 zustand 或 redux)。
// src/cart.js (cart) import react from 'react'; import create from 'zustand'; // zustand store for managing the cart const usecartstore = create(set => ({ cart: [], addtocart: (product) => set(state => ({ cart: [...state.cart, product] })), removefromcart: (product) => set(state => ({ cart: state.cart.filter(item => item.id !== product.id) })), })); const cart = () => { const { cart, addtocart, removefromcart } = usecartstore(); return (); }; export default cart;cart
{cart.map(product => (
- {product.name} - ${product.price}
))}
- 微前端3:结账
使用 cart 和 productlist 组件,将所有内容整合在一起。
// src/Checkout.js (Checkout) import React, { lazy, Suspense } from 'react'; const ProductList = lazy(() => import('productListApp/ProductList')); const Cart = lazy(() => import('cartApp/Cart')); const Checkout = () => (}>Checkout
Loading Products... Loading Cart...
运行应用程序的步骤:
- 运行微前端:
每个微前端(productlist、cart、checkout)将在不同的端口上提供服务(例如,productlist 在 localhost:3001 上,cart 在 localhost:3002 上,checkout 在 localhost:3003 上)。
您需要使用 webpack 开发服务器设置每个微前端并单独运行它们。
- 使用远程模块:
在 checkout 微前端中,我们从各自的远程微前端动态导入 productlist 和 cart 组件。
- 共享依赖项:
每个微前端共享 react、react-dom,以及可能的其他共享依赖项,例如状态库(例如 zustand)或设计系统(material-ui)。
本篇关于《Webpack 系列第 3 部分》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于文章的相关知识,请关注golang学习网公众号!

- 上一篇
- Bunjs 与 PM2

- 下一篇
- 当代版木牛流马?国外网友造出「会走路的桌子」,引百万人围观