I am trying to setup a microfrontend for my project and at base I am using vite to setup my react/typescript applications. I’ve installed the vite plugin for module federation.
And these are my vite.config.ts:
Host:
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import federation from '@originjs/vite-plugin-federation'
export default defineConfig({
build: {
target: 'esnext'
},
plugins: [react(),
federation({
name: "app",
remotes: {
"remote": "http://localhost:5001/assets/remoteEntry.js",
},
shared: ["react", "react-dom"],
})
],
})
remote:
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import federation from '@originjs/vite-plugin-federation'
export default defineConfig({
build: {
target:
},
plugins: [react(),
federation({
name: "remote",
filename: 'remoteEntry.js',
exposes: {
"Button": "./src/components/Button",
},
shared: ["react", "react-dom"],
})
],
server: {
port: 5001,
},
})
When i import the shared button in my host application i get this error:
src/App.tsx:3:20 - error TS2307: Cannot find module 'remote/Button' or its corresponding type declarations.
I am running the remote app on 5001 port. I am lost at what I am missing, hope anyone can help me out – if more context/info is needed I’m happy to provide more.
2
Answers
I came across this article while searching for micro-frontend module federation.
https://software-engineering-corner.hashnode.dev/micro-frontend-module-federation-with-vite-for-react#heading-as-a-lazy-loaded-module
See the code block below NOTE: If you prefer to implement this using typescript…
Looks like you have to bring your own types.
Just create a file inside src/ folder, named some thing like remotes.d.ts, and add the following line
declare module ‘remote/Button’