Skip to content

Commit bed9e05

Browse files
committed
add example for automating a pkg-based repo
1 parent 4200eda commit bed9e05

37 files changed

+7266
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"root": true,
3+
"ignorePatterns": ["**/*"],
4+
"plugins": ["@nx"],
5+
"overrides": [
6+
{
7+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
8+
"rules": {
9+
"@nx/enforce-module-boundaries": [
10+
"error",
11+
{
12+
"enforceBuildableLibDependency": true,
13+
"allow": [],
14+
"depConstraints": [
15+
{
16+
"sourceTag": "*",
17+
"onlyDependOnLibsWithTags": ["*"]
18+
}
19+
]
20+
}
21+
]
22+
}
23+
},
24+
{
25+
"files": ["*.ts", "*.tsx"],
26+
"extends": ["plugin:@nx/typescript"],
27+
"rules": {}
28+
},
29+
{
30+
"files": ["*.js", "*.jsx"],
31+
"extends": ["plugin:@nx/javascript"],
32+
"rules": {}
33+
},
34+
{
35+
"files": ["*.spec.ts", "*.spec.tsx", "*.spec.js", "*.spec.jsx"],
36+
"env": {
37+
"jest": true
38+
},
39+
"rules": {}
40+
},
41+
{
42+
"files": "*.json",
43+
"parser": "jsonc-eslint-parser",
44+
"rules": {}
45+
}
46+
]
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.DS_Store
2+
node_modules
3+
*.log
4+
.next
5+
dist
6+
dist-ssr
7+
*.local
8+
.env
9+
.cache
10+
server/dist
11+
public/dist
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Add files here to ignore them from prettier formatting
2+
/dist
3+
/coverage
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Using Nx Generators to Automate Package Scaffolding for PNPM workspaces
2+
3+
[![package-based monorepo](https://img.shields.io/static/v1?label=Nx%20setup&message=package-based%20monorepo&color=orange)](https://nx.dev/concepts/integrated-vs-package-based#package-based-repos)
4+
5+
Source code for the corresponding Youtube video:
6+
7+
- Video: https://youtu.be/myqfGDWC2go
8+
9+
## What's inside?
10+
11+
This contains a simple example of a PNPM workspaces based monorepo (note: you can just swap it with NPM or Yarn workspace if you prefer those). The workspace contains
12+
13+
- a single React library `ui`
14+
- a local [Nx Plugin](https://nx.dev/plugins/intro/getting-started#create-a-local-plugin) in the `automation` package, that comes with a generator to scaffold new React libraries following the setup of `ui`.
15+
16+
## How to run it
17+
18+
You can just run operations with Nx such as
19+
20+
- building all projects: `pnpm nx run-many -t build`
21+
- building just a single project: `pnpm nx build ui`
22+
23+
And you can also use the generator to scaffold new React libraries:
24+
25+
```
26+
pnpm nx g automation:reactlib mynewreactlib
27+
```
28+
29+
Alternatively 👇
30+
31+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github.com/nrwl/nx-recipes/tree/main/standalone-angular-app?file=README.md)
32+
33+
## Learn More
34+
35+
- [Docs: Nx Plugins](https://nx.dev/plugins/intro/getting-started)
36+
- [Video: Scaffold new Pkgs in a PNPM Workspaces Monorepo](https://youtu.be/myqfGDWC2go)
37+
- [Blog: Introduction to PNPM workspaces based monorepos](https://dev.to/nx/setup-a-monorepo-with-pnpm-workspaces-and-speed-it-up-with-nx-1eem)
38+
- [Video: Lightning Fast PNPM Workspaces](https://youtu.be/PwfR77oe1E8)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { getJestProjects } from '@nx/jest';
2+
3+
export default {
4+
projects: getJestProjects(),
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const nxPreset = require('@nx/jest/preset').default;
2+
3+
module.exports = { ...nxPreset };
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"tasksRunnerOptions": {
3+
"default": {
4+
"runner": "nx/tasks-runners/default",
5+
"options": {
6+
"cacheableOperations": ["build"]
7+
}
8+
}
9+
},
10+
"targetDefaults": {
11+
"build": {
12+
"dependsOn": ["^build"]
13+
},
14+
"lint": {
15+
"inputs": [
16+
"default",
17+
"{workspaceRoot}/.eslintrc.json",
18+
"{workspaceRoot}/.eslintignore"
19+
]
20+
},
21+
"test": {
22+
"inputs": ["default", "^default", "{workspaceRoot}/jest.preset.js"]
23+
}
24+
},
25+
"affected": {
26+
"defaultBase": "main"
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "myorg",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"e2e": "node sanity-check.js"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"devDependencies": {
14+
"@nx/eslint-plugin": "16.3.2",
15+
"@nx/jest": "16.3.2",
16+
"@nx/js": "16.3.2",
17+
"@nx/linter": "16.3.2",
18+
"@nx/plugin": "^16.3.2",
19+
"@swc-node/register": "~1.4.2",
20+
"@swc/cli": "~0.1.62",
21+
"@swc/core": "~1.3.51",
22+
"@types/jest": "^29.4.0",
23+
"@types/node": "18.7.1",
24+
"@typescript-eslint/eslint-plugin": "^5.58.0",
25+
"@typescript-eslint/parser": "^5.58.0",
26+
"eslint": "~8.15.0",
27+
"eslint-config-prettier": "8.1.0",
28+
"jest": "^29.4.1",
29+
"jest-environment-jsdom": "^29.4.1",
30+
"jsonc-eslint-parser": "^2.1.0",
31+
"nx": "16.3.2",
32+
"prettier": "^2.6.2",
33+
"ts-jest": "^29.1.0",
34+
"ts-node": "10.9.1",
35+
"tsup": "^6.7.0",
36+
"typescript": "^5.0.4"
37+
},
38+
"dependencies": {
39+
"@nx/devkit": "16.3.2",
40+
"@swc/helpers": "~0.5.0",
41+
"tslib": "^2.3.0"
42+
},
43+
"nx": {
44+
"targets": {
45+
"e2e": []
46+
}
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"extends": ["../../.eslintrc.json"],
3+
"ignorePatterns": ["!**/*"],
4+
"overrides": [
5+
{
6+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
7+
"rules": {}
8+
},
9+
{
10+
"files": ["*.ts", "*.tsx"],
11+
"rules": {}
12+
},
13+
{
14+
"files": ["*.js", "*.jsx"],
15+
"rules": {}
16+
},
17+
{
18+
"files": ["./package.json", "./generators.json"],
19+
"parser": "jsonc-eslint-parser",
20+
"rules": {
21+
"@nx/nx-plugin-checks": "error"
22+
}
23+
}
24+
]
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# automation
2+
3+
This library was generated with [Nx](https://nx.dev).
4+
5+
## Building
6+
7+
Run `nx build automation` to build the library.
8+
9+
## Running unit tests
10+
11+
Run `nx test automation` to execute the unit tests via [Jest](https://jestjs.io).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"generators": {
3+
"reactlib": {
4+
"factory": "./src/generators/reactlib/generator",
5+
"schema": "./src/generators/reactlib/schema.json",
6+
"description": "reactlib generator"
7+
}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* eslint-disable */
2+
export default {
3+
displayName: 'automation',
4+
preset: '../../jest.preset.js',
5+
transform: {
6+
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
7+
},
8+
moduleFileExtensions: ['ts', 'js', 'html'],
9+
coverageDirectory: '../../coverage/packages/automation',
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "automation",
3+
"version": "0.0.1",
4+
"type": "commonjs",
5+
"generators": "./generators.json"
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"name": "automation",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "packages/automation/src",
5+
"projectType": "library",
6+
"targets": {
7+
"build": {
8+
"executor": "@nx/js:tsc",
9+
"outputs": ["{options.outputPath}"],
10+
"options": {
11+
"outputPath": "dist/packages/automation",
12+
"main": "packages/automation/src/index.ts",
13+
"tsConfig": "packages/automation/tsconfig.lib.json",
14+
"assets": [
15+
"packages/automation/*.md",
16+
{
17+
"input": "./packages/automation/src",
18+
"glob": "**/!(*.ts)",
19+
"output": "./src"
20+
},
21+
{
22+
"input": "./packages/automation/src",
23+
"glob": "**/*.d.ts",
24+
"output": "./src"
25+
},
26+
{
27+
"input": "./packages/automation",
28+
"glob": "generators.json",
29+
"output": "."
30+
},
31+
{
32+
"input": "./packages/automation",
33+
"glob": "executors.json",
34+
"output": "."
35+
}
36+
]
37+
}
38+
},
39+
"lint": {
40+
"executor": "@nx/linter:eslint",
41+
"outputs": ["{options.outputFile}"],
42+
"options": {
43+
"lintFilePatterns": [
44+
"packages/automation/**/*.ts",
45+
"packages/automation/package.json",
46+
"packages/automation/generators.json"
47+
]
48+
}
49+
},
50+
"test": {
51+
"executor": "@nx/jest:jest",
52+
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
53+
"options": {
54+
"jestConfig": "packages/automation/jest.config.ts",
55+
"passWithNoTests": true
56+
},
57+
"configurations": {
58+
"ci": {
59+
"ci": true,
60+
"codeCoverage": true
61+
}
62+
}
63+
}
64+
},
65+
"tags": []
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@<%= scope %>/<%= name %>",
3+
"version": "1.0.0",
4+
"description": "<%= description %>",
5+
"main": "./dist/index.js",
6+
"module": "./dist/index.mjs",
7+
"types": "./dist/index.d.ts",
8+
"sideEffects": false,
9+
"files": [
10+
"dist/**"
11+
],
12+
"scripts": {
13+
"build": "tsup src/index.ts --format esm,cjs --dts --external react"
14+
},
15+
"keywords": [],
16+
"author": "",
17+
"license": "ISC",
18+
"devDependencies": {
19+
"@types/react": "^18.2.0",
20+
"@types/react-dom": "^18.2.0"
21+
},
22+
"dependencies": {
23+
"react": "^18.2.0"
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// export your public modules here
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"jsx": "react-jsx",
5+
"lib": ["dom", "ES2015"],
6+
"module": "ESNext",
7+
"target": "es6",
8+
"allowJs": true,
9+
"esModuleInterop": true,
10+
"allowSyntheticDefaultImports": true
11+
},
12+
"include": ["."],
13+
"exclude": ["dist", "build", "node_modules"]
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineConfig } from 'tsup';
2+
3+
export default defineConfig({
4+
entry: {
5+
'.': 'src/index.ts',
6+
},
7+
banner: {
8+
js: "'use client'",
9+
},
10+
format: ['cjs', 'esm'],
11+
external: ['react'],
12+
dts: true,
13+
});

0 commit comments

Comments
 (0)