简体   繁体   English

使用 CRACO、React 和 typescript 的模块联合会抛出错误:找不到模块无法解析模块“mfe1/Component”

[英]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"));
  1. Application contains 2 microfrontends - mfe1 and mfe2应用程序包含 2 个微前端 - mfe1 和 mfe2
  2. mfe1 exposes Component mfe2 imports remote component from mfe1 mfe1 暴露组件 mfe2 从 mfe1 导入远程组件
  3. Import throws error: Cannot find module 'mfe1/Component' or its corresponding type declarations.导入引发错误:找不到模块“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
  • mfe1.d.ts mfe1.d.ts

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.

相关问题 找不到模块:错误:无法使用 typescript 解析“react-select” - Module not found: Error: Can't resolve 'react-select' with typescript React - 找不到模块:无法解析组件 - React - Module not found: Can't resolve component 找不到模块:错误:无法解析“反应” - Module not found: Error: Can't resolve 'react' Typescript 找不到模块 无法解决 - Typescript Module not found Can't resolve 当我添加进度条时,它抛出模块未找到:错误:无法在 React 中解析 - When I add progress bar it throws Module not found: Error: Can't resolve in React 找不到模块:错误:无法解析 typescript 中的“openlayers” - Module not found: Error: Can't resolve 'openlayers' in typescript 找不到模块:错误:无法解析路由器组件上的“历史/createBrowserHistory” - Module not found: Error: Can't resolve 'history/createBrowserHistory' on Router component 找不到模块:错误:无法从带有 TypeScript 的 React 18 解析“./App” - Module not found: Error: Can't resolve './App' from React 18 w/TypeScript 反应:找不到模块:错误:无法解析“反应/库/更新” - React: Module not found: Error: Can't resolve 'react/lib/update' 找不到模块:错误:无法解析子组件 - Module not found: Error: Can't resolve child component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM