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.56k stars 243 forks source link

Fix/do not inject runtime into build time chunk(#3225) #3229

Open lnlfps opened 2 weeks ago

lnlfps commented 2 weeks ago

Description

When working with umijs or father, during the compilation of less modules, some 'build time chunks' are generated. These temporary chunks lack chunkGraph information, leading to errors when generating runtime code.

// enhanced/scr/lib/container/runtime/FederationRuntime.ts 49  

const conditionMap = this.compilation.chunkGraph.getChunkConditionMap(  
  this.chunk,  chunkHasJs,);  

Then The current chunk id is build time chunk, the variable this.compilation.chunkGraph is undefined,causing an exception to be thrown

Related Issue

https://github.com/module-federation/core/issues/3225

Types of changes

Checklist

netlify[bot] commented 2 weeks ago

Deploy Preview for module-federation-docs ready!

Name Link
Latest commit 07005c8930f79c8981e632753d66608652b0e20c
Latest deploy log https://app.netlify.com/sites/module-federation-docs/deploys/673b2351cceb520008bea34f
Deploy Preview https://deploy-preview-3229--module-federation-docs.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

lnlfps commented 2 weeks ago

The merge operation for this pull request has been blocked. This might be because I don't have the necessary permissions to perform the merge, or perhaps my workflow was incorrect. Could you please help me how to complete this merge? thanks!

ScriptedAlchemy commented 2 weeks ago

@lnlfps are you a bytedance employee?

If yes, I'll expedite the merge or ship a canary

If no, I'll review it in the morning and prepare release if tests pass.

lnlfps commented 2 weeks ago

@ScriptedAlchemy I am not an employee of ByteDance. Thanks very much for your response and assistance. I am planning to implement module federation in our UmiJS project.

I see that there are a few of test cases failing. The reasons for the failures are exactly the issues I am currently encountering.

image

When processing a style modules, the base mf runtime was not injected into the chunk context, but the share runtime was loaded, resulting in an undefined error.

My method might not be complete, and I hope you have better solutions to address similar problems.

ScriptedAlchemy commented 1 week ago

@lnlfps please see CI errors

changeset-bot[bot] commented 1 week ago

⚠️ No Changeset found

Latest commit: 07005c8930f79c8981e632753d66608652b0e20c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

lnlfps commented 1 week ago

I tried to find the cause, which might be an issue when using the mini-css-extract-plugin Webpack plugin to load LESS files. It fails during the compilation of loading modules when importing the federation runtime module, at this time the chunkGraph is undefined, leading to a failure in code generation.

Below is the debugging screenshot for the exception.

image

Here, an undefined error occurs, causing the federation runtime to fail to generate code correctly.

image

I have resubmitted the code, adding only the undefined check. It runs correctly on my local machine and fixes the issue I encountered previously. Please review the code, thank you very much. @ScriptedAlchemy

ScriptedAlchemy commented 1 week ago

@lnlfps please add a new build test case under test/configCases in enhanced package so that in future we will have a build test for this kind of issue

ScriptedAlchemy commented 3 days ago

@lnlfps please add test cases

lnlfps commented 2 days ago

@ScriptedAlchemy hi, I've been a bit busy. I delved deeper into the cause of the issue. It seems that another runtime plugin of mine added the RuntimeGlobals.ensureChunkHandlers runtime dependency to the current build chunk before FederationRuntime, which caused this problem. I delayed the registration of my plugin, which temporarily resolved the issue.

If need me to provide test cases to recurrence this issue, i will do it later. If not, I will close this pull request.

ScriptedAlchemy commented 1 day ago

Gotcha. You can use the afterPlugins hook if you want to tap others later. Just ammend your plugin. We do this in some of federation as well i think. That might work