如何使用ModuleFederation在React Remote中集成角分量

发布于 2025-01-31 03:21:43 字数 2718 浏览 3 评论 0原文

我有以下结构:

-mfp/
 - react-host/
  - webpack.dev.js
  - index.js
  - bootstrp.js
  - src/
   - App.js
 - angular-remote/
  - webpack.config.js
  - src/
   - main.ts
   - bootstrp.ts
   - app/
    - app.module.ts

我的webpack.config.js在我的角镜像中,以下是

const webpack = require('webpack');
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');


module.exports = {
  output: {
    uniqueName: "ang",
    publicPath: "http://localhost:8082/"
  },
  optimization: {
    runtimeChunk: false
  },
  plugins: [
    new ModuleFederationPlugin({
        library: { type: 'var', name: 'ang' },
        name: "ang",
        filename: "remoteEntry.js",
        exposes: {
            './angApp': './src/app/app.component.ts',
        },        
      
        shared: {
          '@angular/core': { eager: true, singleton: true },
          '@angular/common': { eager: true, singleton: true },
          '@angular/router': { eager: true, singleton: true },
        },
    }),
  ],
};

我的webpack.dev.js在我的React-Remote中,以下是

const { merge } = require('webpack-merge');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
const packageJson = require('../package.json');

module.exports = = {
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-react', '@babel/preset-env'],
            plugins: ['@babel/plugin-transform-runtime'],
          },
        },
      },
    ],
  },
  mode: 'development',
  devServer: {
    port: 8081,
    historyApiFallback: {
      index: 'index.html',
    },
  },
  plugins: [
    new ModuleFederationPlugin({
      name: 'marketing',
      filename: 'remoteEntry.js',
      exposes: {
        './MarketingApp': './src/bootstrap',
      },
      shared: packageJson.dependencies,
    }),
    new HtmlWebpackPlugin({
      template: './public/index.html',
    }),
  ],
};

我的< code> app.module.ts 是以下内容:

import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  // bootstrap: []

  bootstrap: [AppComponent]
})
export class AppModule { }

我仍然在我的react中看到我的angular应用,我可以在我的react-host app.js中导入以下

import angApp from 'ang/angApp'

内容错误,我什至在控制台的“网络”选项卡中看到了呼叫成功返回的呼叫,但什么也不会出现。

I have the following structure:

-mfp/
 - react-host/
  - webpack.dev.js
  - index.js
  - bootstrp.js
  - src/
   - App.js
 - angular-remote/
  - webpack.config.js
  - src/
   - main.ts
   - bootstrp.ts
   - app/
    - app.module.ts

my webpack.config.js in my angular-remote look the following

const webpack = require('webpack');
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');


module.exports = {
  output: {
    uniqueName: "ang",
    publicPath: "http://localhost:8082/"
  },
  optimization: {
    runtimeChunk: false
  },
  plugins: [
    new ModuleFederationPlugin({
        library: { type: 'var', name: 'ang' },
        name: "ang",
        filename: "remoteEntry.js",
        exposes: {
            './angApp': './src/app/app.component.ts',
        },        
      
        shared: {
          '@angular/core': { eager: true, singleton: true },
          '@angular/common': { eager: true, singleton: true },
          '@angular/router': { eager: true, singleton: true },
        },
    }),
  ],
};

My webpack.dev.js in my react-remote look the following

const { merge } = require('webpack-merge');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin');
const packageJson = require('../package.json');

module.exports = = {
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-react', '@babel/preset-env'],
            plugins: ['@babel/plugin-transform-runtime'],
          },
        },
      },
    ],
  },
  mode: 'development',
  devServer: {
    port: 8081,
    historyApiFallback: {
      index: 'index.html',
    },
  },
  plugins: [
    new ModuleFederationPlugin({
      name: 'marketing',
      filename: 'remoteEntry.js',
      exposes: {
        './MarketingApp': './src/bootstrap',
      },
      shared: packageJson.dependencies,
    }),
    new HtmlWebpackPlugin({
      template: './public/index.html',
    }),
  ],
};

My app.module.ts is the following:

import { BrowserModule } from '@angular/platform-browser';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  // bootstrap: []

  bootstrap: [AppComponent]
})
export class AppModule { }

Still I don't see my angular app in my react, I can import in my react-host App.js the following

import angApp from 'ang/angApp'

without any error,I even see at the network tab in the console the call's to the JS return successfully but nothing appears.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为人所爱 2025-02-07 03:21:43

我发现了一个很棒的 git repo 这这会符合我打算完全的内容。做。如果有人遇到这个问题,值得检查。

I found a wonderful git repo that implements exactly what I intended to do. if any one come across this issue, worth checking it out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文