Web组件化(Web Components) - React转换为Web Component
最近在思考如何实践微前端, 方案有许多,Web Components也是其中之一。 本文就先从如何将React组件转换为Web Component组件开始,探索Web Component实现微前端的方案。 当然市面上成熟的框架,如SingleSPA,QianKun自然也是可以实现Micro-Frontend,本文只是作为一种可能性研究。
首先,参考我先前的文章,建立一个React项目。
最终结构如下:
webpack.config.js
const path = require('path');
module.exports = {
entry: './src/app.tsx',
module: {
rules: [
{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript",
],
},
}
}
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js'
}
};
package.json
{
"name": "web-component",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack --mode=development",
"build": "webpack --mode=production"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.15.8",
"@babel/preset-env": "^7.15.8",
"@babel/preset-react": "^7.14.5",
"@babel/preset-typescript": "^7.15.0",
"@types/react": "^17.0.31",
"@types/react-dom": "^17.0.10",
"babel-loader": "^8.2.3",
"babel-plugin-transform-class-properties": "^6.24.1",
"webpack": "^5.59.1",
"webpack-cli": "^4.9.1"
},
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2",
"typescript": "^4.4.4"
}
}
.babelrc
{
"presets": ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"],
"plugins": ["@babel/plugin-proposal-class-properties"]
}
app.tsx
import React from 'react';
import ReactDOM from 'react-dom';
function Hello() {
return (
<div>
Hello World!
</div>
);
}
// Name of our class doesn't matter.
class HelloElement extends HTMLElement {
// Happens every time an instance of this element is mounted
// (can be called again when moved from one container element to another)
connectedCallback() {
ReactDOM.render(
<div>
<Hello></Hello>
<button onClick={() => alert("Clicked")}>
Click Me!
</button>
</div>,
this
);
}
}
const tagName = "hello-component";
if (!window.customElements.get(tagName)) {
// prevent rerunning on hot module reloads
// register to be rendered in place of every <evil-plan> tag
window.customElements.define(tagName, HelloElement);
}
index.html
<html>
<script src='bundle.js'></script>
<body>
<hello-component></hello-component>
</body>
</html>
执行npm run build
webpack会在public文件夹下生成bundle.js, 并被index.html引用。 index.html中使用
最后浏览index.html页面即可。
当然,该demo仅仅是第一步,后面还会讨论:
1:Shell App与Web Component的交互,包括通过属性传参、触发事件等等
2:路由,Shell App如何通过路由变化加载对用的Web Component。 作为React应用,Web Component应该选择什么路由策略,才不会影响Shell App。
3:样式隔离。 Web Component间的样式互相隔离,但Shell App可以通过设置全局变量改变Web Component的样式
4:动态加载Web Component
5:其他暂时未想到的
顺便说一句,GitHub的网站就是通过Web Component实现的,打开源码可以看到。
参考:
https://tinloof.com/blog/how-to-create-microfrontends-with-web-components-in-react/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具