question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ModuleFederationPlugin not generate remoteEntry.js

See original GitHub issue

Bug report

What is the current behavior? My purpose is the building assets with only 1 js file, which is remoteEntry.js.Here is my configuration:

const HtmlWebpackPlugin = require("html-webpack-plugin");
const { ModuleFederationPlugin } = require("webpack").container;
const { LimitChunkCountPlugin } = require("webpack").optimize;
const path = require("path");

module.exports = {
  entry: {
    app2: "./src/index",
  },
  mode: "development",
  devServer: {
    static: path.join(__dirname, "dist"),
    port: 3002,
  },
  output: {
    publicPath: "auto",
    clean: true,
  },
  optimization: {
    minimize: false,
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        loader: "babel-loader",
        exclude: /node_modules/,
        options: {
          presets: ["@babel/preset-react"],
        },
      },
    ],
  },
  plugins: [
    // To learn more about the usage of this plugin, please visit https://webpack.js.org/plugins/module-federation-plugin/
    new ModuleFederationPlugin({
      name: "app2",
      filename: "remoteEntry.js",
      exposes: {
        "./App": "./src/App",
      },
      // shared: { react: { singleton: true }, "react-dom": { singleton: true } },
    }),
    new HtmlWebpackPlugin({
      template: "./public/index.html",
    }),
    new LimitChunkCountPlugin({
      maxChunks: 1,
    }),
  ],
};

Using the configuration, the build result is two files, one is index.html and the other is app2.js.But I found app2.js start with var app2;. So Is there a problem with the assignment logic on the file name?

Tip: I found if I use the shared option, the filename will be remoteEntry.js.

If the current behavior is a bug, please provide the steps to reproduce. You can clone the demo code from https://github.com/zxf4399/missing-remoteEntry.

# install dependencies
yarn
yarn start
# open Chrome browser and input `localhost:3001`
# you will find `localhost:3002/remoteEntry.js` but you can not get it after building, that's a little weird.

What is the expected behavior? app2.js to remoteEntry.js.

Other relevant information: webpack version: 5.73.0 Node.js version: 16.15.0 Operating System: Apple M1 Pro Additional tools:

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
alexander-akaitcommented, Sep 28, 2022

Yeah, you can avoid webpack comments and set it globally for all import(...)s, anyway I recommend not to do it, it is bad for perf, except you really undestand why you do it and why you need it

0reactions
zxf4399commented, Sep 28, 2022

@alexander-akait Thanks for your help, finally, I found out how to achieve my goal. Add module.parser.javascript.dynamicImportMode = eager

Here is the modified configuration:

const HtmlWebpackPlugin = require("html-webpack-plugin");
const { ModuleFederationPlugin } = require("webpack").container;
const { LimitChunkCountPlugin } = require("webpack").optimize;
const path = require("path");

module.exports = {
  entry: {
    app2: "./src/index",
  },
  mode: "development",
  devServer: {
    static: path.join(__dirname, "dist"),
    port: 3002,
  },
  output: {
    publicPath: "auto",
    clean: true,
  },
  optimization: {
    minimize: false,
  },
  module: {
    parser: {
      javascript: {
       // the core modification 
        dynamicImportMode: "eager",
      },
    },
    rules: [
      {
        test: /\.jsx?$/,
        loader: "babel-loader",
        exclude: /node_modules/,
        options: {
          presets: ["@babel/preset-react"],
        },
      },
    ],
  },
  plugins: [
    // To learn more about the usage of this plugin, please visit https://webpack.js.org/plugins/module-federation-plugin/
    new ModuleFederationPlugin({
      name: "app2",
      filename: "remoteEntry.js",
      exposes: {
        "./App": "./src/App",
      },
      // shared: { react: { singleton: true }, "react-dom": { singleton: true } },
    }),
    new HtmlWebpackPlugin({
      template: "./public/index.html",
    }),
    new LimitChunkCountPlugin({
      maxChunks: 1,
    }),
  ],
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Module Federation configuration not generating remoteEntry.js
When compiling the code, the remoteEntry.js file is not generated and we can't import it in another application using module-federation.
Read more >
Module Federation - webpack
Remote modules are modules that are not part of the current build but are loaded at runtime from a remote container. Loading remote...
Read more >
Setting up remote components and services with webpack ...
Another reason to implement this is to be able to create shared… ... Expose the component through module federation plugin (Remote → webpack.config.js)...
Read more >
Microfrontend Module Federation: Exposing Remote from ...
This will create a directory dist inside module-federation with few files. We only need remoteEntry.js. Let's serve this remote by running. yarn webpack ......
Read more >
Building React App with Module Federation and NextJS/React
Preferable not created using CRA(create react app) ... Use ModuleFederationPlugin in your webpack.config.js of the app that you wish to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found