Back to Turborepo

Turborepo module federation starter

examples/with-vite-module-federation/README.md

2.9.155.2 KB
Original Source

Turborepo module federation starter

This is a community-maintained example. If you experience a problem, please submit a pull request with a fix. GitHub Issues will be closed.

Using this example

Run the following command:

sh
npx create-turbo@latest -e with-vite-module-federation

What's inside?

This Turborepo includes the following packages/apps:

Apps and Packages

  • react-host: a Vite React host app that consumes the remote app
  • react-remote: a Vite React remote app that exposes ./remote-app
  • @mf-vite-example/shared-ui: a stub React component library shared by both applications

Each package/app is 100% TypeScript.

Utilities

This Turborepo has some additional tools already setup for you:

  • TypeScript for static type checking
  • Vite for local development and production builds
  • Module Federation for runtime composition between the host and remote applications

Build

To build all apps and packages, run the following command:

# With [global `turbo`](https://turborepo.dev/docs/getting-started/installation#global-installation) installed (recommended)
turbo build

# Without [global `turbo`](https://turborepo.dev/docs/getting-started/installation#global-installation), use your package manager
npx turbo build
yarn dlx turbo build
pnpm exec turbo build

You can build a specific package by using a filter:

# With [global `turbo`](https://turborepo.dev/docs/getting-started/installation#global-installation) installed (recommended)
turbo build --filter=react-host

# Without [global `turbo`](https://turborepo.dev/docs/getting-started/installation#global-installation), use your package manager
npx turbo build --filter=react-host
yarn exec turbo build --filter=react-host
pnpm exec turbo build --filter=react-host

Develop

Visit localhost:4173 to interact with your host and remote applications. The host app runs on port 4173 and the remote app runs on port 4174.

To develop all apps and packages, run the following command:

# With [global `turbo`](https://turborepo.dev/docs/getting-started/installation#global-installation) installed (recommended)
turbo dev

# Without [global `turbo`](https://turborepo.dev/docs/getting-started/installation#global-installation), use your package manager
npx turbo dev
yarn exec turbo dev
pnpm exec turbo dev

You can develop a specific package by using a filter:

# With [global `turbo`](https://turborepo.dev/docs/getting-started/installation#global-installation) installed (recommended)
turbo dev --filter=react-host

# Without [global `turbo`](https://turborepo.dev/docs/getting-started/installation#global-installation), use your package manager
npx turbo dev --filter=react-host
yarn exec turbo dev --filter=react-host
pnpm exec turbo dev --filter=react-host

Remote Caching

[!TIP] Vercel Remote Cache is free for all plans. Get started today at vercel.com.

Turborepo can use a technique known as Remote Caching to share cache artifacts across machines, enabling you to share build caches with your team and CI/CD pipelines.

By default, Turborepo will cache locally. To enable Remote Caching you will need an account with Vercel. If you don't have an account you can create one, then enter the following commands:

# With [global `turbo`](https://turborepo.dev/docs/getting-started/installation#global-installation) installed (recommended)
turbo login

# Without [global `turbo`](https://turborepo.dev/docs/getting-started/installation#global-installation), use your package manager
npx turbo login
yarn exec turbo login
pnpm exec turbo login

This will authenticate the Turborepo CLI with your Vercel account.

Next, you can link your Turborepo to your Remote Cache by running the following command from the root of your Turborepo:

# With [global `turbo`](https://turborepo.dev/docs/getting-started/installation#global-installation) installed (recommended)
turbo link

# Without [global `turbo`](https://turborepo.dev/docs/getting-started/installation#global-installation), use your package manager
npx turbo link
yarn exec turbo link
pnpm exec turbo link

Learn more about the power of Turborepo: