core icon indicating copy to clipboard operation
core copied to clipboard

add option to exclude shared dependencies from build

Open tky753 opened this issue 1 year ago • 7 comments

Clear and concise description of the problem

I use rsbuild for building, and want to minimize my remote module size, so I don't want shared dependencies generated on remotes as I can load it from my host. I tried these config in rsbuild.config.ts:

import { ModuleFederationPlugin } from '@module-federation/enhanced/rspack'
......
  output: {
    externals: ["react"]
  },
  tools: {
    rspack: (config, { appendPlugins }) => {
      appendPlugins([
        new ModuleFederationPlugin({
          shared: {
            react: { singleton: true }
          }
       })
    }
  }

but where I run rsbuild build, shared react is still generated at: dist\static\js\async\lib-react.e00de504.js

Suggested solution

add ModuleFederationPluginOptions options:

/* exclude shared dependencies from build, default: false */
external?:  boolean

Alternative

No response

Additional context

No response

Validations

  • [X] Read the Contributing Guidelines.
  • [X] Check that there isn't already an issue that request the same feature to avoid creating a duplicate.

tky753 avatar Jul 25 '24 09:07 tky753

you can set shared: { react: { import:false } }

2heal1 avatar Jul 26 '24 09:07 2heal1

{ import: false } means shared will not be bundled into dist

2heal1 avatar Jul 26 '24 09:07 2heal1

you can set shared: { react: { import:false } }

@2heal1 I tried it, not work, still lib-react.xxxxxxxx.js generated

tky753 avatar Jul 29 '24 00:07 tky753

+1

itmanyong avatar Aug 08 '24 08:08 itmanyong

react-lib is a chunk group. It includes react, react-dom, react/jsx-runtime, schedular. Youd need to look through the chunks contents and make them all external. react-lib is just a name, might not even have react in it if you look closer

ScriptedAlchemy avatar Aug 09 '24 00:08 ScriptedAlchemy

pluginReact({splitChunks: {react: false, router: false}}) or something like that. Will disable the react chunk group entirely

ScriptedAlchemy avatar Aug 09 '24 00:08 ScriptedAlchemy

You can try writing a Webpack plugin,like this:

compiler.hooks.thisCompilation.tap(this.PLUGIN_NAME, (compilation, { normalModuleFactory }) => {
      normalModuleFactory.hooks.afterResolve.tap(this.PLUGIN_NAME, ({ context, request }) => {
        if (this.cropShared.includes(request)) {
          return false
        }
      })
    })

zhongming955 avatar Sep 11 '24 05:09 zhongming955

Stale issue message

github-actions[bot] avatar Nov 10 '24 15:11 github-actions[bot]