Hi, I am trying to migrate from @originjs/vite-plugin-federation to this as it seems better supported and has HMR support. But I cannot get the HMR to work from the remotes (though the app loads). Currently I am just trying to get this set up between 3 different apps.
This is my host/vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { federation } from '@module-federation/vite';
import dotenv from 'dotenv';
const env = dotenv.config({ path: `../../.env` });
export default defineConfig({
plugins: [
federation({
name: 'host',
remotes: {
core: {
type: 'module',
name: 'core',
entry: `${env.parsed?.VITE_CORE_URL}/assets/remoteEntry.js`,
entryGlobalName: 'core',
shareScope: 'default'
},
globalStore: {
type: 'module',
name: 'globalStore',
entry: `${env.parsed?.VITE_GLOBAL_STORE_URL}/assets/remoteEntry.js`,
entryGlobalName: 'globalStore',
shareScope: 'default'
}
},
filename: 'assets/remoteEntry.js',
shared: ['react', 'react-dom', 'react-router-dom']
}),
react()
],
build: {
modulePreload: false,
target: 'esnext',
minify: false,
cssCodeSplit: false
}
});
This is my core/vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { federation } from '@module-federation/vite';
import dotenv from 'dotenv';
const env = dotenv.config({ path: `../../.env` });
export default defineConfig({
plugins: [
federation({
name: 'core',
filename: 'assets/remoteEntry.js',
remotes: {
globalStore: {
type: 'module',
name: 'globalStore',
entry: `${env.parsed?.VITE_GLOBAL_STORE_URL}/assets/remoteEntry.js`,
entryGlobalName: 'globalStore',
shareScope: 'default'
}
},
exposes: {
'./App': './src/App.tsx'
},
shared: ['react', 'react-dom', 'react-router-dom']
}),
react()
],
build: {
modulePreload: false,
target: 'esnext',
minify: false,
cssCodeSplit: false
}
});
My globalStore/vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { federation } from '@module-federation/vite';
export default defineConfig({
plugins: [
federation({
name: 'globalStore',
filename: 'assets/remoteEntry.js',
exposes: {
'./Store': './src/store.tsx'
},
shared: ['react', 'react-dom', 'react-router-dom']
}),
react()
],
build: {
modulePreload: false,
target: 'esnext',
minify: false,
cssCodeSplit: false
}
});
The globalStore shouldn't matter as right now I am just trying to get the HMR working between Host and Core and have basically made my App.tsx to look like this in core. I have to keep globalStore in my config though as otherwise it will break as other components need it even though they aren't imported anywhere.
import React, { useEffect } from 'react';
export default function App() {
return (
<div>Core App Test</div>
);
}
And in Host its this
import React from 'react';
const Core = React.lazy(() => import('core/App'));
export default function App() {
return (
<>
<Core />
</>
);
}
So as you can see, just about as barebones as it gets, and yet HMR does not work. Though in the console I do see this
[vite] hot updated: /src/App.tsx [client:223:18](http://localhost:3100/@vite/client)
And in the network tab I can see this, which even includes the changes I made to core/App.tsx! Which just baffels me that they are there, yet it will not update them on my host.
import { createHotContext as __vite__createHotContext } from "/@vite/client";import.meta.hot = __vite__createHotContext("/src/App.tsx");import __vite__cjsImport0_react_jsxDevRuntime from "/node_modules/.vite/deps/react_jsx-dev-runtime.js?v=f6c91154"; const jsxDEV = __vite__cjsImport0_react_jsxDevRuntime["jsxDEV"];
import RefreshRuntime from "/@react-refresh";
const inWebWorker = typeof WorkerGlobalScope !== "undefined" && self instanceof WorkerGlobalScope;
let prevRefreshReg;
let prevRefreshSig;
if (import.meta.hot && !inWebWorker) {
if (!window.__vite_plugin_react_preamble_installed__) {
throw new Error("@vitejs/plugin-react can't detect preamble. Something is wrong. See https://github.com/vitejs/vite-plugin-react/pull/11#discussion_r430879201");
}
prevRefreshReg = window.$RefreshReg$;
prevRefreshSig = window.$RefreshSig$;
window.$RefreshReg$ = (type, id) => {
RefreshRuntime.register(type, "/Users/MyUserName/Projects/MyApp/apps/core/src/App.tsx " + id);
};
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
}
export default function Index() {
return /* @__PURE__ */ jsxDEV("div", { children: "Core App Test Change" }, void 0, false, {
fileName: "/Users/MyUserName/Projects/MyApp/apps/core/src/App.tsx",
lineNumber: 25,
columnNumber: 5
}, this);
}
_c = Index;
var _c;
$RefreshReg$(_c, "Index");
if (import.meta.hot && !inWebWorker) {
window.$RefreshReg$ = prevRefreshReg;
window.$RefreshSig$ = prevRefreshSig;
}
if (import.meta.hot && !inWebWorker) {
RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
RefreshRuntime.registerExportsForReactRefresh("/Users/MyUserName/Projects/MyApp/apps/core/src/App.tsx", currentExports);
import.meta.hot.accept((nextExports) => {
if (!nextExports) return;
const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate("/Users/MyUserName/Projects/MyApp/apps/core/src/App.tsx", currentExports, nextExports);
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
});
});
}
Either I am missing or something is broken. If you need further information from me please let me know. Thank you for the support.
Hi, I am trying to migrate from @originjs/vite-plugin-federation to this as it seems better supported and has HMR support. But I cannot get the HMR to work from the remotes (though the app loads). Currently I am just trying to get this set up between 3 different apps.
This is my host/vite.config.ts
This is my core/vite.config.ts
My globalStore/vite.config.ts
The globalStore shouldn't matter as right now I am just trying to get the HMR working between Host and Core and have basically made my App.tsx to look like this in core. I have to keep globalStore in my config though as otherwise it will break as other components need it even though they aren't imported anywhere.
And in Host its this
So as you can see, just about as barebones as it gets, and yet HMR does not work. Though in the console I do see this
[vite] hot updated: /src/App.tsx [client:223:18](http://localhost:3100/@vite/client)And in the network tab I can see this, which even includes the changes I made to core/App.tsx! Which just baffels me that they are there, yet it will not update them on my host.
Either I am missing or something is broken. If you need further information from me please let me know. Thank you for the support.