Providing monorepo boilerplate with PNPM workspaces and HMR
Created a full monorepo setup using PNPM workspaces with shared TypeScript and ESLint configs in packages/config. The Next.js app in apps/web imports UI components from packages/ui with HMR enabled via Next.js 13's transpilePackages and experimental.externalDir. Included sample file tree and essential config files demonstrating this setup.
Here’s a minimal monorepo scaffold that meets your requirements:
pnpm workspaces at the root
shared config in packages/config for TS & ESLint
Next.js app in apps/web
UI package in packages/ui
HMR across package boundaries using transpilePackages (Next ≥ 13) or next-transpile-modules (Next 12) and experimental.externalDir
Directory layout
code Copy code
.
├─ pnpm-workspace.yaml
├─ package.json
├─ .npmrc
├─ packages
│ ├─ config
│ │ ├─ tsconfig.base.json
│ │ └─ eslintrc.base.js
│ └─ ui
│ ├─ package.json
│ ├─ tsconfig.json
│ └─ src
│ ├─ Button.tsx
│ └─ index.ts
└─ apps
└─ web
├─ package.json
├─ next.config.js
├─ tsconfig.json
├─ .eslintrc.js
└─ pages
└─ index.tsx
Root files
pnpm-workspace.yaml
yaml Copy code
packages:
- "apps/*"
- "packages/*"
package.json
json Copy code
{
"name": "pnpm-monorepo",
"private": true,
"version": "0.0.0",
"packageManager": "pnpm@8",
"scripts": {
"dev": "pnpm -C apps/web dev",
"build": "pnpm -C apps/web build",
"lint": "pnpm -C apps/web lint"
}
}
.npmrc (helps with hoisting for Next + pnpm)
code Copy code
public-hoist-pattern[]=*eslint*
public-hoist-pattern[]=*webpack*
public-hoist-pattern[]=*babel*
public-hoist-pattern[]=*swc*
shamefully-hoist=false
Shared config
packages/config/tsconfig.base.json
json Copy code
{
"compilerOptions": {
"target": "ES2019",
"lib": ["DOM", "DOM.Iterable", "ES2019"],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"strict": true,
"noEmit": true,
"jsx": "react-jsx",
"types": ["node"]
}
}
packages/config/eslintrc.base.js
js Copy code
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'react', 'react-hooks'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react/recommended',
'plugin:react-hooks/recommended',
'plugin:jsx-a11y/recommended'
],
settings: {
react: { version: 'detect' }
}
};
UI package (packages/ui)
packages/ui/package.json
json Copy code
{
"name": "@acme/ui",
"version": "0.0.1",
"private": true,
"main": "src/index.ts",
"types": "src/index.ts",
"scripts": {
"lint": "eslint . --ext .ts,.tsx"
},
"peerDependencies": {
"react": ">=18",
"react-dom": ">=18"
},
"dependencies": {}
}
packages/ui/tsconfig.json
json Copy code
{
"extends": "../config/tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"baseUrl": "."
},
"include": ["src"]
}
packages/ui/src/Button.tsx
tsx Copy code
import * as React from 'react';
type ButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement>;
export const Button: React.FC<ButtonProps> = ({ children, ...rest }) => {
return (
<button
style={{
padding: '8px 12px',
borderRadius: 6,
border: '1px solid #888',
cursor: 'pointer'
}}
{...rest}
>
{children}
</button>
);
};
packages/ui/src/index.ts
ts Copy code
export * from './Button';
Next.js app (apps/web)
apps/web/package.json
json Copy code
{
"name": "web",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@acme/ui": "workspace:*",
"next": "13.5.4",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"typescript": "^5.2.2",
"@types/react": "^18.2.21",
"@types/react-dom": "^18.2.7",
"eslint": "^8.50.0"
}
}
apps/web/next.config.js (Next ≥ 13 way)
js Copy code
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
// Allow importing from outside the app dir
experimental: {
externalDir: true
},
// Make sure UI package is transpiled and watched for HMR
transpilePackages: ['@acme/ui']
};
module.exports = nextConfig;
If you’re on Next 12, replace transpilePackages with next-transpile-modules:
js Copy code
const withTM = require('next-transpile-modules')(['@acme/ui']);
module.exports = withTM({ reactStrictMode: true, experimental: { externalDir: true } });
apps/web/tsconfig.json
json Copy code
{
"extends": "../../packages/config/tsconfig.base.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@acme/ui": ["../../packages/ui/src"]
}
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
"../../packages/ui/src/**/*"
],
"exclude": ["node_modules"]
}
apps/web/.eslintrc.js
js Copy code
module.exports = {
extends: ['../../packages/config/eslintrc.base.js'],
parserOptions: {
project: './tsconfig.json'
}
};
apps/web/pages/index.tsx
tsx Copy code
import Head from 'next/head';
import { Button } from '@acme/ui';
export default function Home() {
return (
<>
<Head>
<title>Monorepo Demo</title>
</Head>
<main style={{ padding: 24 }}>
<h1>Hello from Next.js + @acme/ui</h1>
<Button onClick={() => alert('HMR works!')}>Click me</Button>
</main>
</>
);
}
Notes on HMR across packages
transpilePackages: ['@acme/ui'] tells Next to SWC-transpile and watch that package. Changes under packages/ui/src will trigger hot reload in the app.
experimental.externalDir: true allows importing files from outside apps/web.
paths in tsconfig.json lets the TS server resolve @acme/ui to workspace sources for intellisense.
The .npmrc hoist patterns ensure build tooling (babel/swc/webpack/eslint) is visible to Next when using pnpm’s isolated store.
Usage
From the repo root:
bash Copy code
pnpm install
pnpm dev # runs Next dev server in apps/web
Edit packages/ui/src/Button.tsx and observe HMR in apps/web without restarting.