Skip to content

Commit cfedae9

Browse files
authored
Update README.md
1 parent 9bdb0f7 commit cfedae9

File tree

1 file changed

+137
-68
lines changed

1 file changed

+137
-68
lines changed

300/300/README.md

Lines changed: 137 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
# 300 - Creating a New React Monorepo
22

3-
**Warning**: Make sure you have installed and created the directory structure of ```hatch``` already. See [HATCH.md](../../HATCH.md)<br/>
4-
**Warning**: Make sure you have installed ```nx``` already. See [NX.md](../../NX.md).
5-
63
Create a new React monorepo with the following command:
74

85
```
96
$ cd hatch-project/src # navigate to the 'hatch-project/src' sub-directory, previously created by hatch
10-
$ mv hatch_project original_hatch_project # temporarily rename the hatch_project directory so nx will not complain
117
$ npx create-nx-workspace@latest hatch_project --preset=react-monorepo
128
```
139

@@ -105,7 +101,7 @@ This will generate the following file and directory structure underneath the ```
105101
└─ hatch_project
106102
├─ ...
107103
├─ apps
108-
│ ├─ react-store
104+
│ ├─ hatch_project
109105
│ │ ├─ public
110106
│ │ │ └─ ...
111107
│ │ ├─ src
@@ -123,7 +119,7 @@ This will generate the following file and directory structure underneath the ```
123119
│ │ ├─ tsconfig.json
124120
│ │ ├─ tsconfig.spec.json
125121
│ │ └─ vite.config.ts
126-
│ └─ react-store-e2e
122+
│ └─ hatch_project-e2e
127123
│ └─ ...
128124
├─ nx.json
129125
├─ tsconfig.base.json
@@ -149,7 +145,9 @@ This will generate the following file and directory structure underneath the ```
149145
├─ nx.json
150146
```
151147

152-
To support the nested directory structure correctly in your ```/hatch-project/src/hatch_project/nx.json```, you should adjust the paths to reflect the correct locations within the nested workspace. Here’s a revised example:
148+
**IMPORTANT**: Modify **nx.json** so it can connect with Nx Cloud.
149+
150+
To support the nested directory structure correctly in your ```/hatch-project/nx.json```, you should adjust the paths to reflect the correct locations within the nested workspace. Here’s a revised example:
153151

154152
```json
155153
{
@@ -230,24 +228,24 @@ To support the nested directory structure correctly in your ```/hatch-project/sr
230228
}
231229
}
232230
```
233-
/nx.json
231+
repository-name/nx.json
234232

235233
### Key Adjustments:
236234
- **`projects` section**: Explicitly defines the project structure, setting the `root` and `sourceRoot` to the correct paths within the nested directory.
237235
- Ensure that all paths reflect the actual structure of your workspace.
238236

239237
This configuration will help Nx Cloud properly identify and manage your nested workspace.
240238

241-
Notice that it prepends paths with ```src/``` (e.g., ```"root": "src/hatch_project",```) to allow for our **nested** directory structure.
239+
Notice that it prepends paths with ```hatch-project/src/``` (e.g., ```"root": "hatch-project/src/hatch_project",```) to allow for our **nested** directory structure.
242240

243241
The path for `root` in the `projects` section should be specified relative to the workspace root, which is typically the directory where your `nx.json` file is located.
244242

245-
Since your `nx.json` is at `repository-name/hatch-project/src/hatch_project/nx.json`, the paths are relative to the `src/hatch_project` directory. Thus:
243+
Since your `nx.json` is at `repository-name/nx.json`, the paths are relative to the `hatch-project/src/hatch_project` directory. Thus:
246244

247-
- **`root`**: Should be `"src/hatch_project"` because it indicates the base directory for the project relative to the workspace's root.
248-
- **`sourceRoot`**: Should be `"src/hatch_project/src"` for the same reason.
245+
- **`root`**: Should be `"hatch-project/src/hatch_project"` because it indicates the base directory for the project relative to the workspace's root.
246+
- **`sourceRoot`**: Should be `"hatch-project/src/hatch_project/src"` for the same reason.
249247

250-
If you were to use the absolute path `hatch-project/src/hatch_project`, it would not be correct in the context of how Nx expects paths to be defined. Nx uses paths relative to the workspace root to maintain consistency across different environments and setups.
248+
If you were to use the absolute path `hatch-project/src/hatch_project`, it would not be correct in the context of how Nx expects paths to be defined. Nx uses paths relative to the workspace root to maintain consistency across different environments and setups. **Note**: In our case the path is from the root of the repository so there is no difference in relative or absolute path.
251249

252250
### Key Sections:
253251
- **`npmScope`**: Defines the scope for your packages.
@@ -257,54 +255,56 @@ If you were to use the absolute path `hatch-project/src/hatch_project`, it would
257255

258256
Adjust paths and options as necessary to fit your specific project structure. This configuration will help Nx Cloud identify and manage your workspace correctly.
259257

260-
Make sure to run the **build** command from the `/hatch-project/src/hatch_project` directory - which contains the ```nx.json``` file - to ensure it recognizes the workspace correctly:
258+
Make sure to run the **build** command from the root of the repository - which contains the ```nx.json``` file - to ensure it recognizes the workspace correctly:
259+
261260
```
262-
$ cd /hatch-project/src/hatch_project
261+
$ cd / # Go to the root of the repository
263262
$ nx build hatch_project
264263
```
265264

266265
**Important**:
267266

268267
If you don't have `workspace.json` or `project.json`, and instead have `tsconfig.base.json`, you can adjust your setup as follows:
269268

270-
* Option: Single Application: **Create a `workspace.json`**: If your project is a single application, you can create a `workspace.json` file in the `hatch_project` directory. Here’s a basic example:
271-
272-
```json
273-
{
274-
"version": 1,
275-
"projects": {
276-
"hatch_project": {
277-
"root": "src/hatch_project",
278-
"sourceRoot": "src/hatch_project/src",
279-
"projectType": "application"
280-
}
281-
}
282-
}
283-
```
269+
* Option: Single Application: **Create a `workspace.json`**: If your project is a single application, you can create a `workspace.json` file in the root of the repository. Here’s a basic example:
270+
271+
```json
272+
{
273+
"version": 1,
274+
"projects": {
275+
"hatch_project": {
276+
"root": "hatch-project/src/hatch_project",
277+
"sourceRoot": "hatch-project/src/hatch_project/src",
278+
"projectType": "application"
279+
}
280+
}
281+
}
282+
```
283+
repository-name/workspace.json
284284

285285
* Option: Multiple Applications: If your Nx workspace contains multiple applications, you should structure your `workspace.json` (or `project.json`) to reflect each application. Here’s how to set it up:
286286

287287
### Example `workspace.json`
288288

289-
Create a `workspace.json` file in the `hatch_project` directory with the following structure:
289+
Create a `workspace.json` file in the root of the repository with the following structure:
290290

291291
```json
292292
{
293293
"version": 1,
294294
"projects": {
295295
"app1": {
296-
"root": "src/hatch_project/app1",
297-
"sourceRoot": "src/hatch_project/app1/src",
296+
"root": "hatch-project/src/hatch_project/app1",
297+
"sourceRoot": "hatch-project/src/hatch_project/app1/src",
298298
"projectType": "application"
299299
},
300300
"app2": {
301-
"root": "src/hatch_project/app2",
302-
"sourceRoot": "src/hatch_project/app2/src",
301+
"root": "hatch-project/src/hatch_project/app2",
302+
"sourceRoot": "hatch-project/src/hatch_project/app2/src",
303303
"projectType": "application"
304304
},
305305
"hatch_project": {
306-
"root": "src/hatch_project",
307-
"sourceRoot": "src/hatch_project/src",
306+
"root": "hatch-project/src/hatch_project",
307+
"sourceRoot": "hatch-project/src/hatch_project/src",
308308
"projectType": "application"
309309
}
310310
}
@@ -324,19 +324,49 @@ Your directory structure might look like this:
324324

325325
```
326326
/
327-
└── hatch-project/
328-
└── src/
329-
└── hatch_project/
330-
├── nx.json
331-
├── workspace.json
332-
├── tsconfig.base.json
333-
├── app1/
334-
│ └── src/
335-
│ └── main.tsx
336-
├── app2/
337-
│ └── src/
338-
│ └── main.tsx
327+
├── hatch-project/
328+
| ├── tsconfig.base.json
329+
│ └── src/
330+
│ └── hatch_project/
331+
│ ├── REMOVE: nx.json
332+
│ ├── REMOVE: workspace.json
333+
│ ├── REMOVE: tsconfig.base.json
334+
│ ├── app1/
335+
│ │ └── src/
336+
│ │ └── main.tsx
337+
│ ├── app2/
338+
│ │ └── src/
339+
│ │ └── main.tsx
340+
├── nx.json
341+
├── workspace.json
342+
```
343+
344+
Where ```tsconfig.base.json``` contains:
345+
346+
```
347+
{
348+
"compileOnSave": false,
349+
"compilerOptions": {
350+
"rootDir": ".",
351+
"sourceMap": true,
352+
"declaration": false,
353+
"moduleResolution": "node",
354+
"emitDecoratorMetadata": true,
355+
"experimentalDecorators": true,
356+
"importHelpers": true,
357+
"target": "es2015",
358+
"module": "esnext",
359+
"lib": ["es2020", "dom"],
360+
"skipLibCheck": true,
361+
"skipDefaultLibCheck": true,
362+
"baseUrl": ".",
363+
"paths": {}
364+
},
365+
"exclude": ["node_modules", "tmp"]
366+
}
339367
```
368+
/hatch-project/src/hatch_project/tsconfig.base.json
369+
340370

341371
### Running Commands
342372
After setting up `workspace.json`, you can run commands like:
@@ -346,43 +376,70 @@ nx build app1
346376
nx build app2
347377
```
348378

349-
**Note**: `workspace.json` and `project.json` serve different purposes in an Nx workspace:
379+
This structure will help Nx Cloud recognize and manage multiple applications effectively.
350380

351-
### `workspace.json`
352-
- **Purpose**: It defines the overall structure of the Nx workspace.
353-
- **Content**: Contains configurations for all projects within the workspace, including their paths, types, and any shared settings.
354-
- **Usage**: Typically used in monorepos with multiple projects to manage and organize them centrally.
381+
This structure should allow Nx Cloud to detect the workspace properly.
355382

356-
### `project.json`
357-
- **Purpose**: It defines the configuration for an individual project within the workspace.
358-
- **Content**: Contains specific settings, targets (like build, test, lint), and options for that particular project.
359-
- **Usage**: Used when you want to modularize project configurations, allowing each project to have its own settings while still being part of the larger workspace.
383+
Run the command to **connect** your workspace to Nx Cloud from the root of the repository, specifically:
360384

361-
### Summary
362-
- **`workspace.json`**: Central configuration for the entire workspace.
363-
- **`project.json`**: Specific configuration for individual projects.
385+
```
386+
$ cd /../../../
387+
$ npm init -y # If no package.json exists
388+
# Go through the initialization steps
389+
$ npm install -g nx@latest # If not already installed
390+
$ npm install --save-dev nx @nrwl/workspace
391+
$ npm install --save-dev @nx/webpack
392+
$ npm install --save-dev webpack-cli
393+
$ npm install --save-dev @nx/react @nx/eslint @nx/playwright @nx/jest
394+
```
364395

365-
In practice, you might use one or both depending on how you structure your Nx workspace. In newer versions of Nx, using `project.json` for individual projects is becoming more common.
396+
Now commit these changes to GitHub repository before continuing!
366397

367-
This structure will help Nx Cloud recognize and manage multiple applications effectively.
398+
The command to connect to Nx Cloud is:
368399

369-
This structure should allow Nx Cloud to detect the workspace properly.
400+
```
401+
$ nx connect-to-nx-cloud
402+
```
403+
404+
This will initiate the configuration process for Nx Cloud within your workspace.
370405

371-
Run the command to **connect** your workspace to Nx Cloud from the root directory of the nx monorepo `hatch project`, specifically:
406+
You will be prompted as follows:
372407

373408
```
374-
$ cd /hatch-project/src/hatch_project/
409+
NX ✔ This workspace already has Nx Cloud set up
410+
411+
If you have not done so already, connect your workspace to your Nx Cloud account with the following URL:
412+
413+
https://cloud.nx.app/connect/Ehf8PFoDWR
375414
```
376415

377-
The command to connect to Nx Cloud is:
416+
Finish the CI setup by visiting: https://cloud.nx.app/connect/eXwFUcpdBt # **Note**: the URL will differ per creation. See [Enable GitHub PR Integration](https://nx.dev/ci/recipes/source-control-integration/github) and/or watch [PNPM-CI: Connect Your Workspace to Nx Cloud for Enhanced Collaboration](https://www.youtube.com/watch?v=8mqHXYIl_qI).
417+
418+
419+
The message "A workspace has already been assigned to this Nx Cloud connection" means that:
420+
421+
1. Your workspace is already configured with an Nx Cloud ID
422+
2. That ID is already in use by another workspace
423+
424+
To fix this:
425+
426+
1. Check your current nx.json for the existing nxCloudId:
427+
- Look for a line like: "nxCloudId": "67a3783761d0514ff26bf202"
428+
- This ID needs to be unique for each workspace
429+
430+
2. Generate a new connection:
378431

379432
```
433+
$ nx generate @nx/workspace:disconnect-cloud
380434
$ nx connect-to-nx-cloud
381435
```
382436

383-
This will initiate the configuration process for Nx Cloud within your workspace.
437+
This will:
438+
- Remove the existing cloud connection
439+
- Generate a new, unique connection
440+
- Provide you with a fresh URL to connect
441+
384442

385-
Finish the CI setup by visiting: https://cloud.nx.app/connect/lvaFjW0bDV # **Note**: the URL will differ per creation. See [Enable GitHub PR Integration](https://nx.dev/ci/recipes/source-control-integration/github) and/or watch [PNPM-CI: Connect Your Workspace to Nx Cloud for Enhanced Collaboration](https://www.youtube.com/watch?v=8mqHXYIl_qI).
386443

387444
## Nested app directories
388445

@@ -399,3 +456,15 @@ For example to ignore any files in ```.next```:
399456
```
400457
.nxignore
401458

459+
460+
Now to run a build, run the following command from the root of the repository:
461+
462+
```
463+
npx nx run-many -t build
464+
```
465+
466+
To run a build for all applications, run the following command from the root of the repository:
467+
468+
```
469+
npx nx run-many -t build --all
470+
```

0 commit comments

Comments
 (0)