rspack icon indicating copy to clipboard operation
rspack copied to clipboard

Build compiles but imports break when using optimization.splitChunks.chunks = 'all' with Module Federation

Open pratyushseq opened this issue 1 year ago • 16 comments

System Info

System: OS: macOS 13.6.3 CPU: (8) arm64 Apple M1 Pro Memory: 100.59 MB / 16.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 18.6.0 - ~/Library/Caches/fnm_multishells/85041_1705309080052/bin/node Yarn: 1.22.19 - ~/Library/Caches/fnm_multishells/85041_1705309080052/bin/yarn npm: 8.13.2 - ~/Library/Caches/fnm_multishells/85041_1705309080052/bin/npm Browsers: Chrome: 120.0.6099.216 Safari: 17.2.1

Details

Add the following config in a module federation project, and the projects will compile, but the imports break in the actual app.

optimization: { splitChunks: { chunks: 'all' } }

image

As discussed with @ahabhgk and @ScriptedAlchemy, opening this issue to keep track of future solutions

Reproduce link

No response

Reproduce Steps

yarn dev with the config mentioned in any project with Module Federation

pratyushseq avatar Jan 15 '24 08:01 pratyushseq

I had to explicitly set this to be able to load the remote:

rsbuild

chunkSplit: {
            strategy: 'split-by-module'
        }

otherwise the resulting __webpack_exports__ was undefined

Gillibald avatar Jan 17 '24 16:01 Gillibald

Coming back to this it even became worse for me. Now I am only able to load the remote by specifying strategy: 'all-in-one'

Gillibald avatar Jan 25 '24 14:01 Gillibald

@Gillibald i just left the whole splitChunks part out, let it fallback to the default async mode. That works reasonably well for me.

pratyushseq avatar Jan 26 '24 05:01 pratyushseq

This issue is caused by a conflict between webpack and chunk split with federation. To resolve this, you can use enforce on cacheGroups to override the splits. It is usually safer to use split-by-module or async, as initial module splits can cause the webpack exports to be wrapped in callbacks. This can result in the return value being undefined, rather than the container interface.

ScriptedAlchemy avatar Jan 26 '24 07:01 ScriptedAlchemy

Thanks a lot. This fixes my issue. How can I achieve this with a pure rsbuild config without touching the rspack config override?

Gillibald avatar Jan 26 '24 13:01 Gillibald

Thanks a lot. This fixes my issue. How can I achieve this with a pure rsbuild config without touching the rspack config override?

@Gillibald would you please try out rsbuild 0.4.0

cc @chenjiahan

xc2 avatar Feb 07 '24 05:02 xc2

I still need to specify:

rspack: {
            optimization: {
                splitChunks: {
                    chunks: 'async'
                }
            }

otherwise, the __webpack_exports__ of the remote is undefined

Tested with:

"@module-federation/enhanced": "0.0.13",
"@module-federation/runtime": "0.0.13",
"@module-federation/runtime-tools": "0.0.13",
"@rsbuild/core": "^0.4.0",
"@rsbuild/plugin-image-compress": "^0.4.0",
"@rsbuild/plugin-react": "^0.4.0",
"@rspack/cli": "^0.5.4",
"@rspack/core": "^0.5.4",
"@rspack/dev-server": "^0.5.4",

Gillibald avatar Feb 07 '24 05:02 Gillibald

@Gillibald are you using the moduleFederation.options config?

chenjiahan avatar Feb 07 '24 06:02 chenjiahan

rsbuild.config.ts

tools: {
        rspack: {
            optimization: {
                splitChunks: {
                    chunks: 'async'
                }
            },
            output: {
                publicPath: 'auto' //required otherwise chunk loading fails
            },
            plugins: [new container.ModuleFederationPlugin(federationConfig)]
        }
    }
})

rsbuild.config.mf.ts

const federationConfig: ModuleFederationPluginOptions = {
    name: 'some',
    exposes: {
        './a': './src/components/a',
        './b': './src/components/b',
        './c': './src/components/c',
    },
    remotes: {
        client: '[email protected]',
        lib:
            process.env.NODE_ENV === 'development'
                ? 'lib@http://localhost:3002/lib.js'
                : '[email protected]'
    },
    filename: 'remoteEntry.js',
    shared: {
        react: {
            singleton: true,
            requiredVersion: dependencies['react']
        },
        'react-dom': {
            singleton: true,
            requiredVersion: dependencies['react-dom']
        },
        'react-singleton-context': {
            singleton: true,
            requiredVersion: dependencies['react-singleton-context']
        }       
    }
}

export default federationConfig

Gillibald avatar Feb 07 '24 06:02 Gillibald

I am not using options here but I am manually adding the plugin so this might cause issues.

Update: Using this

 moduleFederation: {
        options: federationConfig
    }

instead of

tools: {
        rspack: {
            plugins: [new container.ModuleFederationPlugin(federationConfig)]
        }
    }
})

Fixes the issue for me

Gillibald avatar Feb 07 '24 06:02 Gillibald

Yes using the rsbuild built-in capability allows to patch is accordingly so chunk split does not collide

ScriptedAlchemy avatar Mar 09 '24 01:03 ScriptedAlchemy

This issue has been automatically marked as stale because it has not had recent activity. If this issue is still affecting you, please leave any comment (for example, "bump"). We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

stale[bot] avatar May 08 '24 03:05 stale[bot]

bump

hardfist avatar Jul 23 '24 03:07 hardfist

This issue has been automatically marked as stale because it has not had recent activity. If this issue is still affecting you, please leave any comment (for example, "bump"). We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

stale[bot] avatar Sep 21 '24 04:09 stale[bot]

Should be fixed when i port hoisted runtime to rust

ScriptedAlchemy avatar Sep 30 '24 22:09 ScriptedAlchemy