Vite

Sharing Vite packages through pnpm workspaces

Set up a pnpm workspace for Vite apps and shared UI, keep React resolution consistent, and verify linked-package HMR and dependency-ordered builds.

Sharing Vite packages through pnpm workspaces

A client app and an admin app can use the same UI package without releasing it to a registry after every change. I use pnpm workspaces to declare the local dependencies and Vite to consume them during development.

How workspace packages connect

pnpm stores packages by content and links project-specific dependencies into their install trees. Workspaces add discovery and linking for the local packages you declare. The workspace:* protocol requires a local workspace package, so a missing package does not silently become a registry download.

flowchart TD
    ROOT["pnpm workspace"] --> UI["Shared UI"]
    ROOT --> UTILS["Shared utilities"]
    UI --> CLIENT["Client app"]
    UI --> ADMIN["Admin app"]
    UTILS --> CLIENT
    UTILS --> ADMIN

Each package should declare the dependencies it imports. Avoid relying on an accidental hoisted dependency from another app. Explicit declarations also give recursive commands a graph they can use to schedule work.

Defining the workspace and the shared UI package

The directory tree separates deployable applications under apps/ from libraries under packages/. The workspace configuration includes both sets, and the root manifest exposes a common recursive build command.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
my-monorepo/
├── pnpm-workspace.yaml
├── package.json
├── tsconfig.base.json
├── packages/
│   └── shared-ui/
│       ├── package.json
│       ├── tsconfig.json
│       ├── vite.config.ts
│       └── src/
│           ├── index.ts
│           └── components/
└── apps/
    ├── client-web/
    │   ├── package.json
    │   ├── tsconfig.json
    │   ├── vite.config.ts
    │   └── src/
    └── admin-web/
        ├── package.json
        ├── tsconfig.json
        ├── vite.config.ts
        └── src/
1
2
3
4
# pnpm-workspace.yaml
packages:
  - 'packages/*'
  - 'apps/*'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// package.json (root)
{
  "name": "my-vite-monorepo",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "build": "pnpm -r run build",
    "dev:client": "pnpm --filter client-web dev",
    "dev:admin": "pnpm --filter admin-web dev",
    "lint": "pnpm -r run lint"
  },
  "devDependencies": {
    "prettier": "^3.3.3",
    "typescript": "^5.6.3"
  }
}

The UI package exposes its entry through its manifest. React belongs in the UI library’s peer dependencies because the consumer owns the React runtime. A workspace-only package can expose source directly when its consumers know how to transform it. A published library normally needs a deliberate public entry and build output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// packages/shared-ui/package.json
{
  "name": "@namju/shared-ui",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "main": "./src/index.ts",
  "types": "./src/index.ts",
  "exports": {
    ".": {
      "types": "./src/index.ts",
      "import": "./src/index.ts"
    }
  },
  "scripts": {
    "build": "vite build",
    "lint": "eslint src"
  },
  "peerDependencies": {
    "react": "^18.3.1",
    "react-dom": "^18.3.1"
  },
  "devDependencies": {
    "@types/react": "^18.3.11",
    "@types/react-dom": "^18.3.0",
    "@vitejs/plugin-react": "^4.3.2",
    "typescript": "^5.6.3",
    "vite": "^5.4.8"
  }
}

Resolving React consistently in Vite

The app depends on the UI package using the workspace protocol. Linked packages can otherwise resolve another React installation, which is one cause of an invalid hook call. Align the peer dependency ranges and use Vite’s resolve.dedupe for React and React DOM where necessary.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// apps/client-web/package.json
{
  "name": "client-web",
  "version": "1.0.0",
  "private": true,
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "@namju/shared-ui": "workspace:*",
    "react": "^18.3.1",
    "react-dom": "^18.3.1"
  },
  "devDependencies": {
    "@types/react": "^18.3.11",
    "@types/react-dom": "^18.3.0",
    "@vitejs/plugin-react": "^4.3.2",
    "typescript": "^5.6.3",
    "vite": "^5.4.8"
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// apps/client-web/vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';

export default defineConfig({
  plugins: [react()],
  resolve: {
    // 1. Resolve React and React DOM consistently across workspace links.
    dedupe: ['react', 'react-dom'],
    alias: {
      // Map a source alias when needed.
      '@': path.resolve(__dirname, './src')
    }
  },
  server: {
    port: 5173,
    watch: {
      // 2. Observe changes to the linked workspace package.
      ignored: ['!**/node_modules/@namju/shared-ui/**']
    }
  },
  optimizeDeps: {
    // 3. Keep the local workspace package out of dependency optimization.
    exclude: ['@namju/shared-ui']
  }
});

A linked package outside the usual project boundary may need an explicit filesystem or watcher configuration. Check the actual resolved path in dev-server logs. The example excludes the local package from dependency optimization so edits can be seen as source. Keep that exclusion specific, since ordinary dependencies can still benefit from optimization.

Building dependencies and testing source changes

Run the recursive build from the root. pnpm can schedule package scripts in dependency order, while independent apps may run concurrently. Make sure the selected script writes the artifacts its downstream packages actually consume.

1
2
# Run recursive builds from the workspace root
pnpm -r run build

pnpm recursively builds the shared package and its consumers Figure 1. The shared UI dependency builds before the applications that use it.

Start the client app and edit a shared component. Confirm that the rendered component updates and that the app still uses one React instance.

1
2
# Start the client web development server
pnpm --filter client-web dev

Development log for an edit to the linked shared UI package Figure 2. The Vite server observes a source change in the linked workspace package.

Type-checking and deploying a workspace app

Runtime package resolution and TypeScript resolution are separate. The app can use project references to define the dependency between type-checked projects. Align its type settings with the entry it actually imports.

1
2
3
4
5
6
7
8
9
10
// apps/client-web/tsconfig.json
{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "composite": true
  },
  "references": [
    { "path": "../../packages/shared-ui" }
  ]
}

For production, deploy the app’s built static files when that is all the runtime needs. For a Node deployment that also needs packages, evaluate pnpm deploy with the workspace’s configuration and locked pnpm version. I verify that an app can build from a clean checkout and that the deployed output does not rely on a symlink outside the artifact directory.

pnpm has built-in support for monorepositories (AKA multi-package repositories, multi-project repositories, or monolithic repositories). You can create a workspace to unite multiple projects inside a single repository.
This post is licensed under CC BY 4.0 by the author.