close
Skip to content

fix: solve errors with mixing dev/prod React apps#491

Merged
gioboa merged 2 commits into
mainfrom
fix/476
Mar 20, 2026
Merged

fix: solve errors with mixing dev/prod React apps#491
gioboa merged 2 commits into
mainfrom
fix/476

Conversation

@gioboa

@gioboa gioboa commented Mar 17, 2026

Copy link
Copy Markdown
Collaborator

Close #476

@pkg-pr-new

pkg-pr-new Bot commented Mar 17, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@module-federation/vite@491

commit: 03a3b95

@wojtask9

Copy link
Copy Markdown

out of curiosity. Why different paths for react ?

@gioboa gioboa left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR is not working, in fact tests are failing. With react 18 mix dev and prod is not a problem but with React 19 is not working anymore. 😞 I spent 2 days on this issue and it's still not working

@gioboa
gioboa marked this pull request as draft March 17, 2026 13:24
@alternativniy

Copy link
Copy Markdown

@gioboa I struggled for a long time with mixing dev and prod React 19 apps. The error I kept encountering was “dispatcher.getOwner is not a function”. I imagine there are others as well

The only solution I came up with is to switch React to production mode using the Vite plugin for the dev server when mixing dev and prod builds

// vite.config.ts plugins section
react({
  reactRefreshHost: isDev && isMF ? "http://localhost:5001" : undefined,
  babel: {
    plugins: ["babel-plugin-react-compiler"],
    presets:
      isDev && !isMF
        ? [
            [
              "@babel/preset-react",
              { runtime: "automatic", development: false },
            ],
          ]
        : [],
  },
})

Plus, add all React dependencies to shared, such as: react/jsx-runtime, react/jsx-dev-runtime, react/compiler-runtime, react-dom/client

It's actually a bad solution, since it completely breaks HMR, but at least it works to some extent. I hope this helps in some way...

@alternativniy

Copy link
Copy Markdown

I also tried replacing the shared React instance with the dev version using a runtime plugin, but it still breaks after the build.

Getting Vite to build the dev version alongside the prod version is quite a challenge; I did this using esbuild with a separate Node script. This is probably a more promising approach—you could try using a proxy to build both versions and switch between them based on some specific criteria.

Comment thread src/index.ts Fixed
@gioboa
gioboa marked this pull request as ready for review March 20, 2026 12:24

@gioboa gioboa left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alternativniy I started again from a clean main code and with this simple changes the vite-vite example is working with host dev and remote prod.

@alternativniy

Copy link
Copy Markdown

@gioboa It’s expected that everything will work correctly with the host dev. In practice, however, the opposite is usually required host prod and remote dev.

For example, we need this so that each remote’s dev server can be launched separately and operate independently of the host.

@gioboa

gioboa commented Mar 20, 2026

Copy link
Copy Markdown
Collaborator Author

For example, we need this so that each remote’s dev server can be launched separately and operate independently of the host.

my latest commit supported both mixed scenarios.

@gioboa gioboa left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests With Different Frameworks

Screenshot 2026-03-20 at 15 25 27

@gioboa
gioboa merged commit 1fa7088 into main Mar 20, 2026
2 checks passed
@gioboa
gioboa deleted the fix/476 branch March 20, 2026 14:26
falldowngoboone pushed a commit to falldowngoboone/module-federation-vite that referenced this pull request Mar 20, 2026
Comment thread src/index.ts
'\u2029': '\\u2029',
};

function escapeUnsafeJsSourceChars(str: string): string {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just started to read code.. still piecing things together... react 19 src must have some of these chars?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a code comment would go a long ways here

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaning up old browser tabs... curious what this change was about

@devinrhode2

Copy link
Copy Markdown

One question I have: Why did react think we're in a testing environment??

"preview": "vite build && vite preview"
"preview": "vite build && vite preview",
"mixed:1": "vite build && vite preview",
"mixed:2": "MIXED_VV=2 NODE_ENV=production vite"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to set production to serve the correct bundle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: React 19 - Mixing dev/prod React builds breaks

5 participants