简体   繁体   中英

Webpack module federation hot reloading betwwen apps

I'm starting to experiment with the micro frontend with webpack module federation. This is for a very particular purpose because in our company we develop big software like dashboards in react in Role base access control, and I would like each section (or almost) to be a separate application.

So I managed to implement everything, only I noticed that the automatic reload of the app container was not done when I modify a remote app. I can understand why, but I wonder if there is a way to modify this? Knowing that I can't work only on the remote app because it uses the app container's redundancy provider...

Here my webpack config for app container:

const ReactRefreshPlugin = require("@pmmmwh/react-refresh-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ModuleFederationPlugin = require("webpack").container
  .ModuleFederationPlugin;
const path = require("path");
const deps = require("./package.json").dependencies;

module.exports = {
  entry: "./src/index",
  target:"web",
  mode: "development",
  devServer: {
    contentBase: path.join(__dirname, "dist"),
    port: 3001,
    hot:true
  },
  output: {
    publicPath: "auto",
  },
  resolve: {
    extensions: [".js", ".jsx", ".json", ".ts", ".tsx"],
  },
  module: {
    rules: [
      {
        test: /bootstrap\.tsx$/,
        loader: "bundle-loader",
        options: {
          lazy: true,
        },
      },
      {
        test: /\.tsx?$/,
        loader: "babel-loader",
        exclude: /node_modules/,
        options: {
          plugins: ['react-refresh/babel'],
          presets: ["@babel/preset-react", "@babel/preset-typescript"],
        },
      },
      {
        enforce: "pre",
        test: /\.js$/,
        loader: "source-map-loader",
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              importLoaders: 2,
              sourceMap: true,
            },
          },
            {
              loader: 'postcss-loader',
              options: {
                options: {}
              }
            },
        ],
      },
      {
        test: /\.s[ac]ss$/i,
        use: [
          'style-loader',
          'css-loader',
          'resolve-url-loader',
          'sass-loader',
        ],
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: [
          'file-loader',
        ],
      },
    ],
  },
  plugins: [
    new ModuleFederationPlugin({
      name: "appshell",
      filename: "remoteEntry.js",
      remotes: {
        mfsectors: "mfsectors@http://localhost:3002/remoteEntry.js",
      },
      exposes: {
        "./routes": "./src/routes",
        "./src/store/**": "./src/store"
      },
      shared: {
        ...deps,
        react: {
          eager: true,
          singleton: true,
          requiredVersion: deps.react,
        },
        "react-dom": {
          eager: true,
          singleton: true,
          requiredVersion: deps["react-dom"],
        },
      },
    }),
    new HtmlWebpackPlugin({
      template: "./public/index.html",
    }),
    new ReactRefreshPlugin({
      exclude: [/node_modules/, /bootstrap\.tsx$/],
    }),
  ],
};

and here my webpack config for app remote:

const ReactRefreshPlugin = require("@pmmmwh/react-refresh-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ModuleFederationPlugin = require("webpack").container
  .ModuleFederationPlugin;
const path = require("path");
const deps = require("./package.json").dependencies;

module.exports = {
  entry: "./src/index.ts",
  mode: "development",
  devServer: {
    contentBase: path.join(__dirname, "dist"),
    port: 3002,
    hot:true
  },
  output: {
    publicPath: "auto",
  },
  resolve: {
    extensions: [".js", ".jsx", ".json", ".ts", ".tsx"],
  },
  module: {
    rules: [
      {
        test: /bootstrap\.tsx$/,
        loader: "bundle-loader",
        options: {
          lazy: true,
        },
      },
      {
        test: /\.tsx?$/,
        loader: "babel-loader",
        exclude: /node_modules/,
        options: {
          plugins: ['react-refresh/babel'],
          presets: ["@babel/preset-react", "@babel/preset-typescript"],
        },
      },
      {
        enforce: "pre",
        test: /\.js$/,
        loader: "source-map-loader",
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              importLoaders: 2,
              sourceMap: true,
            },
          },
            {
              loader: 'postcss-loader',
              options: {
                options: {}
              }
            },
        ],
      },
      {
        test: /\.s[ac]ss$/i,
        use: [
          'style-loader',
          'css-loader',
          'resolve-url-loader',
          'sass-loader',
        ],
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: [
          'file-loader',
        ],
      },
    ],
  },
  plugins: [
    new ModuleFederationPlugin({
      name: "mfsectors",
      filename: "remoteEntry.js",
      remotes: {
        appshell: "appshell@http://localhost:3001/remoteEntry.js",
      },
      exposes: {
        "./routes": "./src/routes",
      },
      shared: {
        ...deps,
        react: {
          eager: true,
          singleton: true,
          requiredVersion: deps.react,
        },
        "react-dom": {
          eager: true,
          singleton: true,
          requiredVersion: deps["react-dom"],
        },
      },
    }),
    new HtmlWebpackPlugin({
      template: "./public/index.html",
    }),
    new ReactRefreshPlugin({
      exclude: [/node_modules/, /bootstrap\.tsx$/],
    }),
  ],
};

Thank you ! Sorry for my bad english ^^'

you could simply add this to your host's webpack.config.js :

{
...
  devServer: {
    ...
    liveReload: true,
    watchFiles: [path.resolve(__dirname, '..')], // make sure that hits your host app folder
  },
}

and then on your remote's webpack.config.js :

{
  ...
  devServer: {
    ...
    devMiddleware: {
      writeToDisk: true,
    },
  },
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM