fix: create shared virtual module files early for build mode to prevent ENOENT on cold CI builds#459
Draft
yashnextgenit1-design wants to merge 6 commits intomodule-federation:mainfrom
Conversation
commit: |
gioboa
reviewed
Mar 10, 2026
Collaborator
gioboa
left a comment
There was a problem hiding this comment.
Please check this release npm i https://pkg.pr.new/@module-federation/vite@459 🙏
Thanks for your commitment @yashnextgenit1-design
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
The first vite build in a clean CI environment (fresh node_modules) fails with ENOENT when @rollup/plugin-commonjs tries to load prebuild virtual module files from node_modules/__mf__virtual/. Retrying the same build succeeds because files persist from the failed attempt.
Root Cause:
In createEarlyVirtualModulesPlugin shared virtual module files (prebuild .js, loadShare .mjs) were only created in the
config hook for serve mode. For build mode, file creation was deferred to proxyPreBuildShared.configResolved which has enforce: 'post'.
Vite executes
configResolved hooks in order: pre → normal → post. This means Vite's internal plugins and @rollup/plugin-commonjs run their
configResolved before proxyPreBuildShared writes the virtual files. On a cold build the __mf__virtual directory only contains empty.js and
package.json at that point — the prebuild files don't exist yet, causing ENOENT when Rollup later tries to load them.
On retry, files written during the failed first build already exist on disk, so everything works.
Fix
Removed the if (_command !== 'serve') return; guard so that writeLoadShareModule, writePreBuildLibPath, and related calls now execute in the
config hook (which runs before all configResolved hooks) for both serve and build commands. Only the optimizeDeps.include configuration remains gated behind serve mode, as it is only relevant for the dev server.