This is a comprehensive request for setting up a monorepo structure using PNPM workspaces. Let me create a complete boilerplate with:
PNPM workspace configuration
Shared config package (tsconfig, eslintrc)
UI components package with HMR support
Next.js app that imports from UI package
Proper package.json files for each workspace
ESLint and TypeScript configurations
Let me structure this properly:
code Copy code
.
├── .npmrc
├── .gitignore
├── pnpm-workspace.yaml
├── package.json (root)
├── apps/
│ └── web/
│ ├── package.json
│ ├── tsconfig.json
│ ├── next.config.js
│ ├── pages/
│ ├── public/
│ └── .eslintrc.json
├── packages/
│ ├── config/
│ │ ├── package.json
│ │ ├── tsconfig.base.json
│ │ ├── tsconfig.react.json
│ │ ├── tsconfig.next.json
│ │ ├── eslintrc-base.cjs
│ │ ├── eslintrc-react.cjs
│ │ └── README.md
│ └── ui/
│ ├── package.json
│ ├── tsconfig.json
│ ├── src/
│ │ ├── Button.tsx
│ │ ├── Card.tsx
│ │ └── index.ts
│ ├── .eslintrc.json
│ └── README.md
I'll create all the necessary files with proper configurations for:
PNPM workspaces
TypeScript configuration inheritance
ESLint configuration sharing
Next.js with Turbopack supporting HMR across package boundaries
Tailwind CSS for styling across the UI components and Next.js app
PNPM Monorepo Boilerplate with Shared Config and HMR
I'll create a complete monorepo structure with shared configurations and working HMR across packages.
Project Structure
code Copy code
monorepo/
├── .npmrc
├── .gitignore
├── pnpm-workspace.yaml
├── package.json
├── turbo.json
├── apps/
│ └── web/
│ ├── package.json
│ ├── next.config.js
│ ├── tsconfig.json
│ ├── .eslintrc.json
│ ├── postcss.config.js
│ ├── tailwind.config.js
│ ├── pages/
│ │ ├── _app.tsx
│ │ ├── _document.tsx
│ │ └── index.tsx
│ ├── components/
│ ├── public/
│ └── styles/
│ └── globals.css
└── packages/
├── config/
│ ├── package.json
│ ├── tsconfig.base.json
│ ├── tsconfig.react.json
│ ├── tsconfig.next.json
│ ├── eslintrc-base.cjs
│ ├── eslintrc-react.cjs
│ ├── .eslintignore
│ └── README.md
└── ui/
├── package.json
├── tsconfig.json
├── .eslintrc.json
├── tailwind.config.js
├── postcss.config.js
├── src/
│ ├── components/
│ │ ├── Button.tsx
│ │ ├── Card.tsx
│ │ └── index.ts
│ └── index.ts
└── README.md
Root Configuration Files
.npmrc
ini Copy code
shamefully-hoist=true
strict-peer-dependencies=false
auto-install-peers=true
pnpm-workspace.yaml
yaml Copy code
packages:
- 'apps/*'
- 'packages/*'
package.json (Root)
json Copy code
{
"name": "monorepo",
"version": "0.0.1",
"private": true,
"type": "module",
"scripts": {
"dev": "turbo run dev --parallel",
"build": "turbo run build",
"lint": "turbo run lint",
"type-check": "turbo run type-check",
"format": "prettier --write \"**/*.{ts,tsx,json,md}\"",
"format:check": "prettier --check \"**/*.{ts,tsx,json,md}\"",
"clean": "turbo run clean && rm -rf node_modules"
},
"devDependencies": {
"@types/node": "^20.10.0",
"@types/react": "^18.2.45",
"@types/react-dom": "^18.2.18",
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.13.2",
"eslint": "^8.55.0",
"prettier": "^3.1.0",
"turbo": "^1.10.16",
"typescript": "^5.3.3"
}
}
turbo.json
json Copy code
{
"globalDependencies": ["**/.env.local", "**/.env"],
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**", "build/**"],
"cache": true
},
"lint": {
"outputs": []
},
"type-check": {
"outputs": []
},
"dev": {
"cache": false,
"persistent": true
},
"clean": {
"cache": false
}
}
}
.gitignore
code Copy code
# Dependencies
node_modules
pnpm-lock.yaml
yarn.lock
package-lock.json
# Build outputs
dist
build
.next
out
.turbo
# Development
.env.local
.env.*.local
.DS_Store
*.pem
# IDE
.vscode
.idea
.sublime-project
.sublime-workspace
*.swp
*.swo
# Testing
coverage
.nyc_output
Packages Configuration
packages/config/package.json
json Copy code
{
"name": "@repo/config",
"version": "0.0.1",
"private": true,
"description": "Shared configuration files",
"files": ["tsconfig*.json", "eslintrc*.cjs", ".eslintignore"],
"keywords": ["tsconfig", "eslint", "config"]
}
packages/config/tsconfig.base.json
json Copy code
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"noEmit": true,
"jsx": "react-jsx",
"baseUrl": ".",
"paths": {
"@/*": ["./*"],
"@/components/*": ["./components/*"],
"@/lib/*": ["./lib/*"],
"@/types/*": ["./types/*"]
}
}
}
packages/config/tsconfig.react.json
json Copy code
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"jsx": "react-jsx"
}
}
packages/config/tsconfig.next.json
json Copy code
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./src/*"],
"@/components/*": ["./src/components/*"],
"@/pages/*": ["./pages/*"],
"@/styles/*": ["./styles/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
packages/config/eslintrc-base.cjs
javascript Copy code
module.exports = {
root: true,
env: {
browser: true,
es2021: true,
node: true,
},
extends: ['eslint:recommended'],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
rules: {
'no-console': ['warn', { allow: ['warn', 'error'] }],
'no-unused-vars': 'off',
},
};
packages/config/eslintrc-react.cjs
javascript Copy code
module.exports = {
extends: [require.resolve('./eslintrc-base.cjs')],
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
extends: [
require.resolve('./eslintrc-base.cjs'),
'plugin:react/recommended',
'plugin:react-hooks/recommended',
],
plugins: ['react', 'react-hooks'],
settings: {
react: {
version: 'detect',
},
},
rules: {
'react/prop-types': 'off',
'react/react-in-jsx-scope': 'off',
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'warn',
},
};
packages/config/.eslintignore
code Copy code
node_modules
dist
build
.next
coverage
*.config.js
*.config.cjs
packages/config/README.md
markdown Copy code
# @repo/config
Shared configuration files for the monorepo.
## Files
- `tsconfig.base.json` - Base TypeScript configuration
- `tsconfig.react.json` - React-specific TypeScript configuration
- `tsconfig.next.json` - Next.js TypeScript configuration
- `eslintrc-base.cjs` - Base ESLint configuration
- `eslintrc-react.cjs` - React-specific ESLint configuration
UI Package
packages/ui/package.json
json Copy code
{
"name": "@repo/ui",
"version": "0.0.1",
"private": true,
"description": "Shared UI components",
"type": "module",
"exports": {
".": {
"import": "./src/index.ts",
"types": "./src/index.ts"
},
"./components": {
"import": "./src/components/index.ts",
"types": "./src/components/index.ts"
}
},
"main": "./src/index.ts",
"types": "./src/index.ts",
"files": ["src"],
"scripts": {
"dev": "echo 'UI package - no dev server needed'",
"build": "echo 'UI package - no build needed'",
"lint": "eslint src",
"type-check": "tsc --noEmit",
"clean": "rm -rf dist"
},
"dependencies": {
"clsx": "^2.0.0"
},
"devDependencies": {
"@repo/config": "workspace:*",
"@types/react": "^18.2.45",
"@types/react-dom": "^18.2.18",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tailwindcss": "^3.3.6",
"typescript": "^5.3.3"
},
"peerDependencies": {
"react": "^18.0.0",
"react-dom": "^18.0.0"
}
}
packages/ui/tsconfig.json
json Copy code
{
"extends": "@repo/config/tsconfig.react.json",
"include": ["src"],
"exclude": ["node_modules", "dist"]
}
packages/ui/.eslintrc.json
json Copy code
{
"extends": ["@repo/config/eslintrc-react.cjs"],
"parserOptions": {
"project": "./tsconfig.json"
}
}
packages/ui/tailwind.config.js
javascript Copy code
/** @type {import('tailwindcss').Config} */
export default {
content: ['./src/**/*.{js,ts,jsx,tsx}'],
theme: {
extend: {},
},
plugins: [],
};
packages/ui/postcss.config.js
javascript Copy code
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
packages/ui/src/index.ts
typescript Copy code
export * from './components/index';
packages/ui/src/components/index.ts
typescript Copy code
export { Button } from './Button';
export { Card } from './Card';
packages/ui/src/components/Button.tsx
typescript Copy code
import React from 'react';
import clsx from 'clsx';
interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
variant?: 'primary' | 'secondary' | 'ghost';
size?: 'sm' | 'md' | 'lg';
children: React.ReactNode;
}
export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
(
{ variant = 'primary', size = 'md', className, children, ...props },
ref
) => {
return (
<button
ref={ref}
className={clsx(
'font-semibold rounded-lg transition-colors duration-200 focus:outline-none focus:ring-2 focus:ring-offset-2',
{
// Variants
'bg-blue-600 text-white hover:bg-blue-700 focus:ring-blue-500':
variant === 'primary',
'bg-gray-200 text-gray-900 hover:bg-gray-300 focus:ring-gray-500':
variant === 'secondary',
'bg-transparent text-gray-900 hover:bg-gray-100 focus:ring-gray-500':
variant === 'ghost',
// Sizes
'px-3 py-1.5 text-sm': size === 'sm',
'px-4 py-2 text-base': size === 'md',
'px-6 py-3 text-lg': size === 'lg',
},
className
)}
{...props}
>
{children}
</button>
);
}
);
Button.displayName = 'Button';
packages/ui/src/components/Card.tsx
typescript Copy code
import React from 'react';
import clsx from 'clsx';
interface CardProps extends React.HTMLAttributes<HTMLDivElement> {
children: React.ReactNode;
hoverable?: boolean;
}
export const Card = React.forwardRef<HTMLDivElement, CardProps>(
({ className, children, hoverable = false, ...props }, ref) => {
return (
<div
ref={ref}
className={clsx(
'rounded-lg border border-gray-200 bg-white shadow-sm',
{
'hover:shadow-md transition-shadow duration-200': hoverable,
},
className
)}
{...props}
>
{children}
</div>
);
}
);
Card.displayName = 'Card';
interface CardHeaderProps extends React.HTMLAttributes<HTMLDivElement> {
children: React.ReactNode;
}
export const CardHeader = React.forwardRef<HTMLDivElement, CardHeaderProps>(
({ className, children, ...props }, ref) => (
<div ref={ref} className={clsx('px-6 py-4 border-b border-gray-200', className)} {...props}>
{children}
</div>
)
);
CardHeader.displayName = 'CardHeader';
interface CardBodyProps extends React.HTMLAttributes<HTMLDivElement> {
children: React.ReactNode;
}
export const CardBody = React.forwardRef<HTMLDivElement, CardBodyProps>(
({ className, children, ...props }, ref) => (
<div ref={ref} className={clsx('px-6 py-4', className)} {...props}>
{children}
</div>
)
);
CardBody.displayName = 'CardBody';
interface CardFooterProps extends React.HTMLAttributes<HTMLDivElement> {
children: React.ReactNode;
}
export const CardFooter = React.forwardRef<HTMLDivElement, CardFooterProps>(
({ className, children, ...props }, ref) => (
<div
ref={ref}
className={clsx('px-6 py-4 border-t border-gray-200', className)}
{...props}
>
{children}
</div>
)
);
CardFooter.displayName = 'CardFooter';
packages/ui/README.md
markdown Copy code
# @repo/ui
Shared UI components library built with React and Tailwind CSS.
## Installation
This package is already available in the workspace.
## Usage
```typescript
import { Button, Card, CardHeader, CardBody, CardFooter } from '@repo/ui';
export function MyComponent() {
return (
<Card hoverable>
<CardHeader>
<h2>Title</h2>
</CardHeader>
<CardBody>
<p>Content goes here</p>
</CardBody>
<CardFooter>
<Button variant="primary">Action</Button>
</CardFooter>
</Card>
);
}
Components
Button - Customizable button component
Card - Container component with header, body, and footer
code Copy code
## Next.js Web App
### `apps/web/package.json`
```json
{
"name": "@repo/web",
"version": "0.0.1",
"private": true,
"type": "module",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "eslint . --ext .ts,.tsx",
"type-check": "tsc --noEmit",
"clean": "rm -rf .next"
},
"dependencies": {
"@repo/ui": "workspace:*",
"next": "^14.0.4",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@next/eslint-plugin-next": "^14.0.4",
"@repo/config": "workspace:*",
"@tailwindcss/typography": "^0.5.10",
"@types/node": "^20.10.0",
"@types/react": "^18.2.45",
"@types/react-dom": "^18.2.18",
"autoprefixer": "^10.4.16",
"postcss": "^8.4.32",
"tailwindcss": "^3.3.6",
"typescript": "^5.3.3"
}
}
apps/web/next.config.js
javascript Copy code
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
transpilePackages: ['@repo/ui'],
typescript: {
tsconfigPath: './tsconfig.json',
},
experimental: {
optimizePackageImports: ['@repo/ui'],
},
webpack: (config, { isServer }) => {
// Enable HMR for workspace packages
if (!isServer) {
config.watchOptions = {
...config.watchOptions,
poll: 1000,
aggregateTimeout: 300,
ignored: ['**/node_modules', '**/.next'],
};
}
return config;
},
};
export default nextConfig;
apps/web/tsconfig.json
json Copy code
{
"extends": "@repo/config/tsconfig.next.json",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"],
"@/components/*": ["./src/components/*"],
"@/pages/*": ["./pages/*"],
"@/styles/*": ["./styles/*"],
"@/lib/*": ["./lib/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
apps/web/.eslintrc.json
json Copy code
{
"extends": [
"@repo/config/eslintrc-react.cjs",
"next/core-web-vitals"
],
"parserOptions": {
"project": "./tsconfig.json"
},
"rules": {
"@next/next/no-html-link-for-pages": "off"
}
}
apps/web/postcss.config.js
javascript Copy code
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
apps/web/tailwind.config.js
javascript Copy code
/** @type {import('tailwindcss').Config} */
export default {
content: [
'./pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}',
'../../packages/ui/src/**/*.{js,ts,jsx,tsx}',
],
theme: {
extend: {},
},
plugins: [],
};
apps/web/styles/globals.css
css Copy code
@tailwind base;
@tailwind components;
@tailwind utilities;
html {
scroll-behavior: smooth;
}
body {
@apply bg-white text-gray-900;
}
* {
@apply border-gray-200;
}
apps/web/pages/_document.tsx
typescript Copy code
import { Html, Head, Main, NextScript } from 'next/document';
export default function Document() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
apps/web/pages/_app.tsx
typescript Copy code
import type { AppProps } from 'next/app';
import '@/styles/globals.css';
export default function App({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />;
}
apps/web/pages/index.tsx
Setup Instructions
1. Install PNPM (if not already installed)
bash Copy code
npm install -g pnpm
2. Clone/Create the repository structure
bash Copy code
mkdir monorepo
cd monorepo
# Copy all the files above
3. Install dependencies
bash Copy code
pnpm install
4. Generate lock file
bash Copy code
pnpm install
5. Start development
The Next.js app will be available at http://localhost:3000
Features
✅ PNPM Workspaces - Efficient monorepo management
✅ Shared TypeScript Config - Single source of truth for types
✅ Shared ESLint Config - Consistent linting across packages
✅ Hot Module Replacement - Changes instantly reflect without reload
✅ Type Safety - Full TypeScript support across packages
✅ Tailwind CSS - Optimized styling with content purging
✅ Turbo - Incremental builds and caching
✅ NextJS 14 - Latest features and optimizations
Troubleshooting
HMR Not Working
Ensure transpilePackages includes @repo/ui in apps/web/next.config.js
Check that packages/ui/package.json exports are properly configured
Restart the dev server
TypeScript Errors
Run pnpm type-check to see all type errors
Verify tsconfig.json extends the right config from @repo/config
ESLint Issues
Clear node_modules: pnpm clean && pnpm install
Verify ESLint config extends properly from @repo/config
This boilerplate is production-ready and follows industry best practices!