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

React Module Federation is broken from 15.4.6+ #14629

Closed
rathpc opened this issue Jan 25, 2023 · 14 comments
Closed

React Module Federation is broken from 15.4.6+ #14629

rathpc opened this issue Jan 25, 2023 · 14 comments
Assignees
Labels
outdated scope: react Issues related to React support for Nx type: bug

Comments

@rathpc
Copy link
Contributor

rathpc commented Jan 25, 2023

Current Behavior

With the recent changes in this release (https://github.com/nrwl/nx/releases/tag/15.4.6) specifically these 3:

It seems that module federation serving and building is broken because the withModuleFederation method has not been refactored to take into account the change that happened with regards to getWebpackConfig -> withReact and the latter now returning a function instead of just a config object.

As a result, the building and serving of both host and remote applications throw errors like the following:

ERROR in container entry ./Module[0]
Module not found: Error: Can't resolve './src/remote-entry.ts' in '/code/frontend'
resolve './src/remote-entry.ts' in '/code/frontend'
  using description file: /code/frontend/package.json (relative path: .)
    Field 'browser' doesn't contain a valid alias configuration
    using description file: /code/frontend/package.json (relative path: ./src/remote-entry.ts)
      no extension
        Field 'browser' doesn't contain a valid alias configuration
        /code/frontend/src/remote-entry.ts doesn't exist
      .ts
        Field 'browser' doesn't contain a valid alias configuration
        /code/frontend/src/remote-entry.ts.ts doesn't exist
      .tsx
        Field 'browser' doesn't contain a valid alias configuration
        /code/frontend/src/remote-entry.ts.tsx doesn't exist
      .mjs
        Field 'browser' doesn't contain a valid alias configuration
        /code/frontend/src/remote-entry.ts.mjs doesn't exist
      .js
        Field 'browser' doesn't contain a valid alias configuration
        /code/frontend/src/remote-entry.ts.js doesn't exist
      .jsx
        Field 'browser' doesn't contain a valid alias configuration
        /code/frontend/src/remote-entry.ts.jsx doesn't exist
      as directory
        /code/frontend/src/remote-entry.ts doesn't exist

This file likely needs to be updated: https://github.com/nrwl/nx/blob/master/packages/react/src/module-federation/with-module-federation.ts as well as documentation around how to use the new methods like withReact, withWeb, withNx and composePlugins if they should be used directly at all. The only information I found on this was within other issues or the pull requests themselves. Even the release notes are pretty limited on descriptive information about this change.

Expected Behavior

I expect module federation builds and serving to continue working as it previously had as there were no documented breaking changes to this functionality after 15.4.5

GitHub Repo

No response

Steps to Reproduce

Set up a module federation project just as described here: https://nx.dev/recipes/module-federation/faster-builds and it will no longer work on any version of Nx after 15.4.5.

Nx Report

   Node : 16.15.1
   OS   : darwin x64
   yarn : 1.22.19

   nx : 15.6.1
   @nrwl/angular : Not Found
   @nrwl/cypress : 15.6.1
   @nrwl/detox : Not Found
   @nrwl/devkit : 15.6.1
   @nrwl/esbuild : Not Found
   @nrwl/eslint-plugin-nx : 15.6.1
   @nrwl/expo : Not Found
   @nrwl/express : 15.6.1
   @nrwl/jest : 15.6.1
   @nrwl/js : 15.6.1
   @nrwl/linter : 15.6.1
   @nrwl/nest : Not Found
   @nrwl/next : Not Found
   @nrwl/node : 15.6.1
   @nrwl/nx-cloud : Not Found
   @nrwl/nx-plugin : Not Found
   @nrwl/react : 15.6.1
   @nrwl/react-native : Not Found
   @nrwl/rollup : 15.6.1
   @nrwl/schematics : Not Found
   @nrwl/storybook : 15.6.1
   @nrwl/web : 15.6.1
   @nrwl/webpack : 15.6.1
   @nrwl/workspace : 15.6.1
   @nrwl/vite : Not Found
   typescript : 4.9.4
   ---------------------------------------
   Local workspace plugins:
   ---------------------------------------
   Community plugins:

Failure Logs

ERROR in container entry ./Module[0]
Module not found: Error: Can't resolve './src/remote-entry.ts' in '/code/frontend'
resolve './src/remote-entry.ts' in '/code/frontend'
  using description file: /code/frontend/package.json (relative path: .)
    Field 'browser' doesn't contain a valid alias configuration
    using description file: /code/frontend/package.json (relative path: ./src/remote-entry.ts)
      no extension
        Field 'browser' doesn't contain a valid alias configuration
        /code/frontend/src/remote-entry.ts doesn't exist
      .ts
        Field 'browser' doesn't contain a valid alias configuration
        /code/frontend/src/remote-entry.ts.ts doesn't exist
      .tsx
        Field 'browser' doesn't contain a valid alias configuration
        /code/frontend/src/remote-entry.ts.tsx doesn't exist
      .mjs
        Field 'browser' doesn't contain a valid alias configuration
        /code/frontend/src/remote-entry.ts.mjs doesn't exist
      .js
        Field 'browser' doesn't contain a valid alias configuration
        /code/frontend/src/remote-entry.ts.js doesn't exist
      .jsx
        Field 'browser' doesn't contain a valid alias configuration
        /code/frontend/src/remote-entry.ts.jsx doesn't exist
      as directory
        /code/frontend/src/remote-entry.ts doesn't exist

Additional Information

No response

@AgentEnder AgentEnder added the scope: react Issues related to React support for Nx label Jan 26, 2023
@Tassfighter
Copy link

Workaround:

Goto to your app folder and edit module-federation.config.js

From

module.exports = {
  name: 'image-selection',
  exposes: {
    './Module': './src/remote-entry.ts',
  },
};

To

module.exports = {
  name: 'image-selection',
  exposes: {
    './Module': './apps/{appname}/src/remote-entry.ts',
  },
};

@jaysoo
Copy link
Member

jaysoo commented Jan 27, 2023

There is a context missing that needs to point to the app's root directory.

Something like this should work until we patch it in the next release.

//webpack.config.js
const { composePlugins, withNx } = require('@nrwl/webpack');
const { withReact } = require('@nrwl/react');
const { withModuleFederation } = require('@nrwl/react/module-federation');

const baseConfig = require('./module-federation.config');

const config = {
  ...baseConfig,
};

module.exports = composePlugins(
  withNx(),
  withReact(),
  withModuleFederation(config),
  config => {
     config.context = __dirname;
    return config;
  }
);

@jonesy212
Copy link

I updated this in my remote. The project did build this time, but the error is still happening on my end now in the host also when I try to run the command 'pnpm nx serve host --devRemotes dashboard'

ERROR in ./apps/host/src/app/app.tsx 6:48-74
Module not found: Error: Can't resolve 'dashboard/Module' in 'path/to/apps/host/src/app'

webpack compiled with 1 error (198da9a27f713c31)
[ ready ] http://localhost:4200

I also just tried adding the context config @jaysoo with no change in the error.

@jaysoo
Copy link
Member

jaysoo commented Jan 27, 2023

Try this in the host. There is a problem handling the withModuleFederation in composePlugins right now.

const { withModuleFederation } = require('@nrwl/react/module-federation');
const baseConfig = require('./module-federation.config');

const defaultConfig = {
  ...baseConfig,
};

module.exports = withModuleFederation(defaultConfig);

@jonesy212
Copy link

Currently removing the devkit is creating errors. Seems that there are a lot of changes happening from that upgrade creating breaking changes. Is there documentation for updating from 15.50 to 15.6. So as not to cross error conversations @jaysoo.

@jaysoo
Copy link
Member

jaysoo commented Jan 27, 2023

You should be able to use these configs.

Host:

const { withModuleFederation } = require('@nrwl/react/module-federation');
const baseConfig = require('./module-federation.config');

const defaultConfig = {
  ...baseConfig,
};

module.exports = withModuleFederation(defaultConfig);

Remote:


const { composePlugins, withNx } = require('@nrwl/webpack');
const { withModuleFederation } = require('@nrwl/react/module-federation');
const baseConfig = require('./module-federation.config');

const defaultConfig = {
  ...baseConfig,
};

module.exports = composePlugins(withModuleFederation(defaultConfig), config => {
  config.context = __dirname;
  return config;
});

@jaysoo
Copy link
Member

jaysoo commented Jan 27, 2023

See here https://github.com/jaysoo/issue14629

We should be able to patch it today.

Updated docs for webpack config: https://nx.dev/packages/webpack/documents/webpack-config-setup (we're still working on more docs).

@jaysoo
Copy link
Member

jaysoo commented Jan 27, 2023

Fix this here #14653

@rathpc
Copy link
Contributor Author

rathpc commented Jan 27, 2023

Fix this here #14653

Thank you for the fast turnaround on this @jaysoo! Two questions for you:

  1. Does the composePlugins function replace the need for webpack-merge if you have multiple configs you need to merge together or would that still serve a purpose as the former is exclusive to combining the plugins of the config only?
  2. Do you know which release this will be included with?

@jaysoo
Copy link
Member

jaysoo commented Jan 27, 2023

  1. composePlugins is specifically for Nx, where the exported functions have signature (config) => config, where as webpack-merge operates on the config object itself. You can use both in conjuction. (more docs are coming!)
  2. We can release a patch today for 15.6.3.

@rathpc
Copy link
Contributor Author

rathpc commented Jan 27, 2023

  1. composePlugins is specifically for Nx, where the exported functions have signature (config) => config, where as webpack-merge operates on the config object itself. You can use both in conjuction. (more docs are coming!)

    1. We can release a patch today for 15.6.3.

Ok, great thank you again!

@jonesy212
Copy link

@jaysoo Thank you so much for your quick responses and turn around time. I also figured out one of the issues that may be happening as well. When you install a new workspace, there are no polyfills being created. In the example you provided, it does have them. I tried creating an empty workspace as well as an integrated one, then creating the host and remote dashboard. So if someone is starting from scratch, it will cause issues. Just wanted to point that out. There may be other files, but I stopped there when I came across that.

@jaysoo
Copy link
Member

jaysoo commented Jan 30, 2023

@jonesy212 The polyfills are not generated by default anymore. Modern browsers don't require them. We'll write a guide for how to enable polyfills for those that need them, but they aren't required for module federation to work.

15.6.3 is released and module federation is working with it. Following the guide is working again.

@jaysoo jaysoo closed this as completed Jan 30, 2023
@github-actions
Copy link

github-actions bot commented Mar 3, 2023

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: react Issues related to React support for Nx type: bug
Projects
None yet
Development

No branches or pull requests

5 participants