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

the default import cannot be correctly resolve #2995

Open
5 tasks done
h11g opened this issue Sep 25, 2024 · 9 comments
Open
5 tasks done

the default import cannot be correctly resolve #2995

h11g opened this issue Sep 25, 2024 · 9 comments

Comments

@h11g
Copy link

h11g commented Sep 25, 2024

Describe the bug

Several factors combined together led to this problem.

  1. The app uses module federation
  2. The app uses a third-party library, thirty-lib, which uses i18next
  3. The mf config of the app sets thirty-lib and i18next to the shared scope
  4. The third-party library uses a multi-entry packaging method
Details image image

From the code, it can be seen that i18next in the bundled is a named import, but it is actually a default import, which leads to the inability to find the i18next.on method.

When I try to remove multiple entries or take i18next out of the shared score, this project works fine

Details
export default defineConfig((env) => {
  return {
      ...
      rollupOptions: {
        input: {
          index: './src/react.ts',
          // comment to make project work
          vanilla: './src/vanilla.ts',
        },
        external: ['i18next', 'react-i18next', 'react', 'react-dom'],
      },
    },
   ...
  }
})

or

new ModuleFederationPlugin({
  ...
      shared: {
        ...
        // comment to make project work
        i18next: {
          singleton: true,
          requiredVersion: dep.i18next,
        },
      },
    }),
})

I provided a demo on codesandbox, which makes it easier to debug the project when downloaded locally

Reproduction

https://codesandbox.io/p/devbox/mf-bundle-demo-635f7l

Used Package Manager

pnpm

System Info

System:
    OS: macOS 15.0
    CPU: (8) arm64 Apple M3
    Memory: 79.30 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.14.0 - ~/.nvm/versions/node/v20.14.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.14.0/bin/yarn
    npm: 10.8.1 - ~/.nvm/versions/node/v20.14.0/bin/npm
    pnpm: 9.11.0 - ~/.nvm/versions/node/v20.14.0/bin/pnpm
    bun: 1.1.12 - ~/.bun/bin/bun
  Browsers:
    Chrome: 129.0.6668.59
    Safari: 18.0

Validations

@loveholly
Copy link

Hi @ScriptedAlchemy, any comments here?

@ScriptedAlchemy
Copy link
Member

Okay the problem is indeed the default import.
I would sugggest trying this with webpack and see if you get the same issue.
If not, please report it to the rspack repo - looks like a bug.
The bundler should wrap in default import factory handler, i think.

@ScriptedAlchemy
Copy link
Member

if i go i18next.default.on, it works in the library

@loveholly
Copy link

Thanks for your kindly reply @ScriptedAlchemy. Actually this issue is not only occurred on rspack but also on webpack. The problem is indeed default import. We tested many situation, and finally found that this problem happened when the following three conditions all met:

When we use i18next in our codebase under the above conditions:

import i18next from 'i18next'

i18next.init()

Then the bundler will resolve it incorrectly, treating i18next not as a default import. Seems like because init function also is a named export from i18next.

Hope these information can help.

@ScriptedAlchemy
Copy link
Member

Most likely this needs to be filed with the webpack core and rspack core projects. Does original federation in webpack also have the issue?

I'll take a look in my source but it seems like a deeper problem to how modules are wrapped. I can make it look for .default but that will have other problem or basically reverse the issue to only default

@h11g
Copy link
Author

h11g commented Oct 6, 2024

yeap, original federation in webpack also have the same issue. @ScriptedAlchemy

@ScriptedAlchemy
Copy link
Member

I'd open an issue with webpack core. I'd also try it with rspack, which my team has direct control over. If it's a problem in rspack, I can get that patched if a GitHub issue is opened.

As a workaround you can use a runtime plugin on loadShare / the resolver for it and you could automatically return the default export or re merge and modify the exports as you wish.

@h11g h11g changed the title module federation with multiple entry, the default import cannot be correctly resolve i18next the default import cannot be correctly resolve Oct 11, 2024
@jmvtrinidad
Copy link

jmvtrinidad commented Oct 17, 2024

@ScriptedAlchemy The bug is also open on rspack
web-infra-dev/rspack#8043
looks like the team is already on it

@ScriptedAlchemy
Copy link
Member

Perfect. Then we will need to wait for it to be fixed on the Rust end so that it aligns correctly with webpack.

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

4 participants