Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script to generate types with @module-federation/enhanced #2489

Open
5 tasks done
ATakaSKY opened this issue May 13, 2024 · 3 comments
Open
5 tasks done

Script to generate types with @module-federation/enhanced #2489

ATakaSKY opened this issue May 13, 2024 · 3 comments

Comments

@ATakaSKY
Copy link

Describe the bug

I've a webpack project with react which has 2 command under scripts, one to build the project with mode production and other one is to start the dev server in development mode:

"start": "webpack-dev-server --mode development",
"build": "webpack --mode production"

Now I am generating remote types with @module-federation/enhanced package. The problem is every time I run either of these two commands, the package always ends up regenerating the types for me. Since I just want types to be generated once before local dev/build, I turned off type generation in that common config by this option dts: { consumeTypes: false }.

To achieve type generation beforehand, I ended up creating a separate webpack config to just fetch the remote types.
I have added a script in package json which basically runs this config file:
"microfrontend-types:codegen": "webpack --mode none --config webpack.mf.types.js"

webpack.mf.types.js

const path = require("path");
const { ModuleFederationPlugin } = require("@module-federation/enhanced");

// remote to fetch types from
const TYPES_URL = "http://localhost:3001";

const config = {
  entry: {
    testApp: ["react-hot-loader/patch", path.join(__dirname, "dev/index.tsx")],
  },
  module: {
    rules: [
      {
        test: /\.ts(x?)$/,
        exclude: /node_modules/,
        use: [
          {
            loader: "ts-loader",
          },
        ],
      },
    ],
  },
  plugins: [
    new ModuleFederationPlugin({
      name: "test",
      filename: "remoteEntry.js",
      remotes: {
        testProj: `testMF@${TYPES_URL}/ui/remoteEntry.js?v=${Date.now()}`,
      },
    }),
  ],
  resolve: {
    extensions: [".tsx", ".ts", ".js"],
  },
};

// To exit process after fetching federation types
config.plugins.push({
  apply: (compiler) => {
    compiler.hooks.done.tap("ModuleFederationPlugin", () => {
      console.log("\x1b[32m", "Federation types fetched. Exiting...");
      process.exit(1);
    });
  },
});

module.exports = config;

If you look at the above config, you'll see I'm also exiting webpack process because if I don't do it, webpack starts complaining about the usage of types in components, reason being types getting regenerated here. Somehow the approach that I'm following doesn't look clean to me.

I expected the type generation thing to be one command which I could run separately irrespective whether I'm running the project or building it. Is there a way to achieve it ? I am thinking something similar to how apollo codegen works

Reproduction

NA

Used Package Manager

npm

System Info

NA

Validations

@ScriptedAlchemy
Copy link
Member

Why do you need types generated beforehand? The build should pull the types ahead of time already, and re-pull them each time to sync it per build update (like hmr)

@ScriptedAlchemy ScriptedAlchemy added 🐞 bug Something isn't working and removed 🐞 bug Something isn't working labels May 14, 2024
@ATakaSKY
Copy link
Author

ATakaSKY commented May 15, 2024

I had to generate it this way because every time types was regenerating, I was getting below error:

Attaching a screenshot for the issue I'm facing:

image

I think the project also tries to compile at the same time types are being refetched and could be the reason why it's not able to comprehend in the usage file.

@ScriptedAlchemy
Copy link
Member

Usually it should wait for type sync before attempting to build. Do you have a repo I can look at?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants