module-federation / core

Module Federation is a concept that allows developers to share code and resources across multiple JavaScript applications
https://module-federation.io/
MIT License
1.27k stars 184 forks source link

When modifying files (occasionally) or rsbuild.config.ts (must be), the generated product has problems #2596

Open jameszhang2020 opened 3 weeks ago

jameszhang2020 commented 3 weeks ago

Describe the bug

I have shared multiple packages in the module federation host, and the configuration is as follows.

// rsbuild.config.ts
...
import { ModuleFederationPlugin } from '@module-federation/enhanced/rspack'
import pkgInfo from './package.json'
...

export default defineConfig(({env, command, envMode}) => {
  return {
    ...
    tools: {
      rspack: {
        plugins: [
          new ModuleFederationPlugin({
            name: 'runtime',
            shared: {
              react: { singleton: true, requiredVersion: pkgInfo.dependencies.react },
              'react-dom': {
                singleton: true,
                requiredVersion: pkgInfo.dependencies['react-dom']
              },
              '@cb/sdk': { singleton: true },
              '@cb/react-portal-provider': { singleton: true },
              '@cb/react-theme-provider': { singleton: true },
              '@cb/react-viewport-provider': { singleton: true },
              '@cb/react-box': { singleton: true },
              '@cb/react-flex': { singleton: true },
              '@cb/react-panel': { singleton: true },
              '@cb-app/workspace-context': { singleton: true },
              '@cb-app/common': { singleton: true },
              '@cb-app/core-api': { singleton: true },
              '@cb-app/points': { singleton: true },
              '@cb-app/messenger': { singleton: true },
              '@cb-app/components': { singleton: true },
              '@cb-app/shared-points': { singleton: true },
              '@cb/logger': { singleton: true },
              '@cb/editor': { singleton: true },
              lodash: { singleton: true },
              'styled-components': { singleton: true }
            }
          })
        ]
      }
    }
  }
})

On first startup, it will generate a set of configurations that can be found in the entry file (index.js), like this:

__webpack_require__.initializeSharingData = {
  scopeToSharingDataMapping: {
    "default": [
        {
            "name": "@cb/sdk",
            "version": "1.32.0",
            "factory": function(){
                     return __webpack_require__.e("verdors-node_module_cb_sdk_dist_sdk_index_js").then(function(){
                           return function(){ return __webpack_require__("../../node_modules/@cb/sdk/dist/index.js" }
                      })
             }
        },
       ...more
    ]
  }
}

However, when I edited a ts file or rsbuild.config.ts file, saved it, and then checked it again, I found that it became empty, as shown below:

__webpack_require__.initializeSharingData = {
  scopeToSharingDataMapping: {
    "default": []
  }
}

Other remotes will depend on these shared packages, so this initializeSharingData.scopeToSharingDataMapping is very important

Reproduction

N/A

Used Package Manager

yarn

System Info

System:
    OS: macOS 14.4.1
    CPU: (8) arm64 Apple M3
    Memory: 111.56 MB / 24.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.12.2 - ~/.nvm/versions/node/v20.12.2/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.12.2/bin/yarn
    npm: 10.5.0 - ~/.nvm/versions/node/v20.12.2/bin/npm
    pnpm: 9.1.4 - ~/.nvm/versions/node/v20.12.2/bin/pnpm
  Browsers:
    Chrome: 125.0.6422.142
    Safari: 17.4.1

Validations

ScriptedAlchemy commented 3 weeks ago

Consider filing this issue on rsbuild project directly

jameszhang2020 commented 3 weeks ago

Ok, thank you, issue has been created