0

我正在尝试使用 ModuleFederationPlugin 和 webpack 部署基于微前端的反应 Web 应用程序,但是当我尝试单独部署一个微前端时,我得到一个没有错误的白页。

我的 webpack 配置到生产:

const { merge } = require('webpack-merge')
const ModuleFederationPlugin = require('webpack/lib/container/ModuleFederationPlugin')
const commonConfig = require('./webpack.common')
const packageJson = require('../package.json')

const prodConfig = {
    mode: 'production',
    output: {
        filename: '[name].[contenthash].js',
        publicPath: '/Microfrontends/marketing/'
    },
    plugins: [
        new ModuleFederationPlugin({
            name: 'marketing',
            filename: 'remoteEntry.js',
            exposes: {
                './MarketingModule': './src/bootstrap'
            },
            shared: packageJson.dependencies
        })
    ]
}

module.exports = merge(commonConfig, prodConfig)

我的 webpack 与 babel 通用:

const HtmlWebpackPlugin = require('html-webpack-plugin')

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']
                    }
                }
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './public/index.html'
        })
    ]
}

我的带有 React-Router 的 App.js 组件:

export default function App({ history }) {
    return (
        <Fragment>
            <StylesProvider generateClassName={generateClassName}>
                <Router history={history}>
                    <Switch>
                        <Route exact path="/Microfrontends/marketing/pricing" component={Pricing} />
                        <Route path="/Microfrontends/marketing/" component={Landing} />
                    </Switch>
                </Router>
            </StylesProvider>
        </Fragment>
    )
}

当我在服务器中部署时,所有状态请求都是 200 并且 chrome 控制台没有错误

4

0 回答 0