[英]Module federation using CRACO, React and typescript throws error: Module not found Can't resolve module "mfe1/Component"
craco.config.js for mfe1 mfe1 的 craco.config.js
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
module.exports = {
mode: 'development',
devServer: {
port: 9999,
},
plugins:
new ModuleFederationPlugin(
{
name: "mfe1",
filename:"remoteEntry.js",
library: {
type: "var",
name: "mfe1",
},
exposes: {
// expose each component
"./Component": "./src/index.tsx",
},
})
};
craco.config.js for mfe2 mfe2 的 craco.config.js
const ModuleFederation = require('webpack/lib/container/ModuleFederationPlugin');
module.exports = {
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
},
module: {
rules: [
{
test: /\.tsx?$/,
use: "ts-loader",
exclude: /node_modules/,
},
],
},
resolve: {
extensions: [".tsx", ".ts", ".js"],
},
output: {
publicPath: "http://localhost:3000/",
},
plugins:
new ModuleFederation({
name: 'mfe2',
filename: 'remoteEntry.js',
remotes: {
mfe1: 'mfe1@http://localhost:9999/remoteEntry.js',
}
}),
}
const RemoteComponent = React.lazy(()=>import("mfe1/Component"));
Do not expose extension in line: "./Component": "./src/index.tsx", use simply不要在行中暴露扩展名:“./Component”:“./src/index.tsx”,简单地使用
exposes: {
// expose each component
"./Component": "./src/index",
},
btw it's recommended not exposing index.tsx as the "usual" setup is index.js顺便说一句,建议不要公开 index.tsx,因为“通常”设置是 index.js
import('./app/bootstrap.tsx'); //its for react + moduleFederation
bootstrap will contains the originial index.js content then bootstrap will import into the root.render. bootstrap 将包含原始 index.js 内容,然后 bootstrap 将导入到 root.render。
After this you can simply expose "./Component": "./src/App".在此之后,您可以简单地公开“./Component”:“./src/App”。
You must declare the Module in your host application using a module declaration file.您必须使用模块声明文件在主机应用程序中声明模块。 In your case, you need to create a file in the same folder as the file you are instantiating, the Sub-Application.
在您的情况下,您需要在与您正在实例化的文件(子应用程序)相同的文件夹中创建一个文件。
App.tsx应用程序.tsx
const RemoteComponent = React.lazy(()=>import("mfe1/Component"));
mfe1.d.ts: mfe1.d.ts:
declare module "mfe1/Component" {
const Component: React.ComponentType;
export default Component;
}
You can see a working example here: https://github.com/module-federation/module-federation-examples/tree/cf2d070c7bbb0d5f447cfe33f69e07bcf85f8ddb/typescript你可以在这里看到一个工作示例: https://github.com/module-federation/module-federation-examples/tree/cf2d070c7bbb0d5f447cfe33f69e07bcf85f8ddb/typescript
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.