Vite
Vite is a build tool that aims to provide a faster and leaner development experience for modern web projects
Configure Module Federation
pnpm i -D @originjs/vite-plugin-federation
- Configure
vite.config.ts
import federation from "@originjs/vite-plugin-federation";
export default {
plugins: [
federation({
name: "remote-app",
filename: "remoteEntry.js",
// Modules to expose
exposes: {
"./Button": "./src/Button.vue",
},
shared: ["vue"],
}),
],
};
Examples
Build a Library
- Library Mode - Vite Docs
-
Create an entrypoint for the library, like
src/lib.ts
-
Update Vite config to build entrypoint
import { resolve } from "path"; import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; // https://vitejs.dev/config/ export default defineConfig({ // https://vitejs.dev/guide/build.html#library-mode build: { lib: { name: "DesignSystem", formats: ["es"], entry: resolve(__dirname, "src/lib.ts"), fileName: (format) => `lib.${format}.js`, }, rollupOptions: { external: ["react", "tailwindcss"], }, }, plugins: [react()], });
-
Update
tsconfig.json
to emit declarations and ignore the non-library code -
Update the
package.json
with the following:- Add the
"module"
and"types"
entries - Compile Typescript after the Vite build (the Vite build runs a cleanup, which will delete the typings)
- Add the
Last update:
August 12, 2023
Created: June 3, 2023
Created: June 3, 2023