最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Webpack module federation hot reloading between apps - Stack Overflow

programmeradmin3浏览0评论

I'm starting to experiment with the micro frontend with webpack module federation. This is for a very particular purpose because in our pany 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 ^^'

I'm starting to experiment with the micro frontend with webpack module federation. This is for a very particular purpose because in our pany 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 ^^'

Share Improve this question edited May 1, 2024 at 17:45 Mangs 1,2202 gold badges26 silver badges46 bronze badges asked Feb 21, 2021 at 7:49 MinosMinos 2662 silver badges13 bronze badges 2
  • I would also like to know how to do this--as of right now, I am manually refreshing. If there were only a way to "watch" other folders with webpack-dev-server... – cjones26 Commented Jun 17, 2021 at 16:01
  • stackoverflow./questions/64919434/… see my answer here, I succeeded using chokidar – Minos Commented Jun 21, 2021 at 7:02
Add a ment  | 

2 Answers 2

Reset to default 3

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,
    },
  },
}

Thank you @alexandre_anicio! Working fine

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

评论列表(0)

  1. 暂无评论