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

[Bug]: ModuleFederation needs globalThis but polyfill chunks executed later #3593

Open
nanianlisao opened this issue Sep 29, 2024 · 12 comments
Labels
🐞 bug Something isn't working

Comments

@nanianlisao
Copy link
Contributor

Version

latest

Details

The minimum configuration is as follows:

moduleFederation: {
    options: {},
  },
  output: {
    polyfill: 'entry',
    overrideBrowserslist: ['chrome 70'],
  },

At this time, corejs injection does not take effect.

In addition, this seems to be a problem with rspack, webpack is ok

Reproduce link

no

Reproduce Steps

no

@nanianlisao nanianlisao added the 🐞 bug Something isn't working label Sep 29, 2024
Copy link
Contributor

Hello @nanianlisao. Please provide a reproduction repository or online demo. For background, see Why reproductions are required. Thanks ❤️

@chenjiahan
Copy link
Member

You can find the core-js code in this chunk:

image

The default lib-polyfill chunk will not be generated when using moduleFederation.options, see https://rsbuild.dev/config/module-federation/options

image

@nanianlisao
Copy link
Contributor Author

@chenjiahan You need to run it under chrome 70, and then the following error will appear.

image

The problem is that the polyfill does not take effect, not the splitChunk rule.

@nanianlisao
Copy link
Contributor Author

Any new progress? @chenjiahan

@chenjiahan
Copy link
Member

Any reproduction?

@nanianlisao
Copy link
Contributor Author

Sorry, my description is wrong, in fact the problem is that mf requires globalThis. However, after rsbuild turns on mf, corejs will be additionally divided into chunks and executed later, thus causing an error.

Uncaught ReferenceError: globalThis is not defined

https://github.com/nanianlisao/rsbuild-repro/tree/main

pnpm run dev

Open it in chrome 70 and you'll find the error

Image

@nanianlisao nanianlisao changed the title [Bug]: After moduleFederation is turned on, polyfill does not take effect. [Bug]: ModuleFederation needs globalThis but polyfill chunks executed later Nov 5, 2024
@chenjiahan
Copy link
Member

Get, I have reproduced this problem. The MF runtime runs earlier than core-js, so the core-js globalThis polyfill cannot work as expected.

I will discuss with MF members how to fix this.

@chenjiahan
Copy link
Member

A workaround is to use polyfill: "usage", this ensures that the polyfills can run before the MF runtime code.

export default defineConfig({
  plugins: [
    pluginReact({
      fastRefresh: false,
    }),
  ],
  moduleFederation: {
    options: {},
  },
  output: {
    polyfill: "usage",
    overrideBrowserslist: ["chrome 70"],
  },
});

@nanianlisao
Copy link
Contributor Author

Yes, it works. But I would like to inject polyfill via entry, for example for compatibility with some third-party packages, and it works fine in webpack.

@chenjiahan
Copy link
Member

Do you mean webpack's Module Federation v1 plugin or Module Federation enhanced?

Module Federation v1 does not use globalThis.

@chenjiahan
Copy link
Member

I have discussed this with @2heal1 and MF will consider adding a minimal globalThis polyfill to the runtime code.

@2heal1
Copy link
Member

2heal1 commented Nov 7, 2024

Try @module-federation/[email protected] by setting implementation options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants