Skip to content

Commit bf206e5

Browse files
authored
feat(misc): non conflicting init/add flow (#22791)
1 parent e97c588 commit bf206e5

File tree

180 files changed

+1815
-1252
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

180 files changed

+1815
-1252
lines changed

docs/shared/migration/adding-to-existing-project.md

Lines changed: 96 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Adding Nx to your Existing Project
22

3-
Nx can be added to any type of project, not just monorepos. The main benefit is to get caching abilities for the package scripts. Each project usually has a set of scripts in the `package.json`:
3+
Nx can be added to any type of project, not just monorepos. The main benefit is to get caching abilities for the package
4+
scripts. Each project usually has a set of scripts in the `package.json`:
45

56
```json {% fileName="package.json" %}
67
{
@@ -16,7 +17,8 @@ Nx can be added to any type of project, not just monorepos. The main benefit is
1617
You can make these scripts faster by leveraging Nx's caching capabilities. For example:
1718

1819
- You change some spec files: in that case the `build` task can be cached and doesn't have to re-run.
19-
- You update your docs, changing a couple of markdown files: then there's no need to re-run builds, tests, linting on your CI. All you might want to do is trigger the Docusaurus build.
20+
- You update your docs, changing a couple of markdown files: then there's no need to re-run builds, tests, linting on
21+
your CI. All you might want to do is trigger the Docusaurus build.
2022

2123
## Install Nx on a Non-Monorepo Project
2224

@@ -26,21 +28,69 @@ Run the following command:
2628
npx nx@latest init
2729
```
2830

29-
This will set up Nx for you - updating the `package.json` file and creating a new `nx.json` file with Nx configuration based on your answers during the set up process. The set up process will suggest installing Nx plugins that might be useful based on your existing repository. The example below is using the `@nx/eslint` and `@nx/next` plugins to run ESLint and Next.js tasks with Nx:
31+
Running this command will ask you a few questions about your workspace and then set up Nx for you accordingly. The setup
32+
process detects tools which are used in your workspace and suggests installing Nx plugins to integrate the tools you use
33+
with Nx. Running those tools through Nx will have caching enabled when possible, providing you with a faster alternative
34+
for running those tools. You can start with a few to see how it works and then add more with
35+
the [`nx add`](/nx-api/nx/documents/add) command later. You can also decide to add them all and get the full experience
36+
right
37+
away because adding plugins will not break your existing workflow.
38+
39+
The first thing you may notice is that Nx updates your `package.json` scripts during the setup process. Nx Plugins setup
40+
Nx commands which run the underlying tool with caching enabled. When a `package.json` script runs a command which can be
41+
run through Nx, Nx will replace that script in the `package.json` scripts with an Nx command that has
42+
caching automatically enabled. Anywhere those `package.json` scripts are used, including your CI, will become faster
43+
when possible. Let's go through an example where the `@nx/next/plugin` and `@nx/eslint/plugin` plugins are added to a
44+
workspace with the
45+
following `package.json`.
46+
47+
```diff {% fileName="package.json" %}
48+
{
49+
"name": "my-workspace",
50+
...
51+
"scripts": {
52+
- "build": "next build && echo 'Build complete'",
53+
+ "build": "nx next:build && echo 'Build complete'",
54+
- "lint": "eslint ./src",
55+
+ "lint": "nx eslint:lint",
56+
"test": "node ./run-tests.js"
57+
},
58+
+ "nx": {}
59+
}
60+
```
61+
62+
The `@nx/next/plugin` plugin adds a `next:build` target which runs `next build` and sets up caching correctly. In other
63+
words, running `nx next:build` is the same as running `next build` with the added benefit of it being cacheable. Hence,
64+
Nx replaces `next build` in the `package.json` `build` script to add caching to anywhere running `npm run build`.
65+
Similarly, `@nx/eslint/plugin` sets up the `nx eslint:lint` command to run `eslint ./src` with caching enabled.
66+
The `test` script was not recognized by any Nx plugin, so it was left as is. After Nx has been setup,
67+
running `npm run build` or `npm run lint` multiple times, will be instant when possible.
68+
69+
You can also run any npm scripts directly through Nx with `nx build` or `nx lint` which will run the `npm run build`
70+
and `npm run lint` scripts respectively. In the later portion of the setup flow, Nx will ask if you would like some of
71+
those npm scripts to be cacheable. By making those cacheable, running `nx build` rather than `npm run build` will add
72+
another layer of cacheability. However, `nx build` must be run instead of `npm run build` to take advantage of the
73+
cache.
74+
75+
## Inferred Tasks
76+
77+
You may have noticed that `@nx/next` provides `dev` and `start` tasks in addition to the `next:build` task. Those tasks
78+
were created by the `@nx/next/plugin` plugin from your existing Next.js configuration. You can see the configuration for
79+
the Nx Plugins in `nx.json`:
3080

3181
```json {% fileName="nx.json" %}
3282
{
3383
"plugins": [
3484
{
3585
"plugin": "@nx/eslint/plugin",
3686
"options": {
37-
"targetName": "lint"
87+
"targetName": "eslint:lint"
3888
}
3989
},
4090
{
4191
"plugin": "@nx/next/plugin",
4292
"options": {
43-
"buildTargetName": "build",
93+
"buildTargetName": "next:build",
4494
"devTargetName": "dev",
4595
"startTargetName": "start"
4696
}
@@ -49,33 +99,11 @@ This will set up Nx for you - updating the `package.json` file and creating a ne
4999
}
50100
```
51101

52-
When Nx updates your `package.json` scripts, it looks for scripts that can be replaced with an Nx command that has caching automatically enabled. The `package.json` defined above would be updated to look like this:
53-
54-
```json {% fileName="package.json" %}
55-
{
56-
"name": "my-workspace",
57-
...
58-
"scripts": {
59-
"build": "nx build",
60-
"lint": "nx lint",
61-
"test": "node ./run-tests.js"
62-
},
63-
...
64-
"nx": {
65-
"includedScripts": []
66-
}
67-
}
68-
```
69-
70-
The `@nx/next` plugin can run `next build` for you and set up caching correctly, so it replaces `next build` with `nx build`. Similarly, `@nx/eslint` can set up caching for `eslint ./src`. When you run `npm run build` or `npm run lint` multiple times, you'll see that caching is enabled. You can also call Nx directly from the terminal with `nx build` or `nx lint`.
71-
72-
The `test` script was not recognized by any Nx plugin, so it was left as is.
73-
74-
The `includedScripts` array allows you to specify `package.json` scripts that can be run with the `nx build` syntax.
75-
76-
## Inferred Tasks
102+
Each plugin can accept options to customize the projects which they create. You can see more information about
103+
configuring the plugins on the [`@nx/next/plugin`](/nx-api/next) and [`@nx/eslint/plugin`](/nx-api/eslint) plugin pages.
77104

78-
You may have noticed that `@nx/next` provides `dev` and `start` tasks in addition to the `build` task. Those tasks were created by the `@nx/next` plugin from your existing Next.js configuration. To view all available tasks, open the Project Details view with Nx Console or use the terminal to launch the project details in a browser window.
105+
To view all available tasks, open the Project Details view with Nx Console or use the terminal to launch the project
106+
details in a browser window.
79107

80108
```shell
81109
nx show project my-workspace --web
@@ -90,7 +118,7 @@ nx show project my-workspace --web
90118
"data": {
91119
"root": ".",
92120
"targets": {
93-
"lint": {
121+
"eslint:lint": {
94122
"cache": true,
95123
"options": {
96124
"cwd": ".",
@@ -107,7 +135,7 @@ nx show project my-workspace --web
107135
"executor": "nx:run-commands",
108136
"configurations": {}
109137
},
110-
"build": {
138+
"next:build": {
111139
"options": {
112140
"cwd": ".",
113141
"command": "next build"
@@ -146,28 +174,27 @@ nx show project my-workspace --web
146174
"sourceRoot": ".",
147175
"name": "my-workspace",
148176
"projectType": "library",
149-
"includedScripts": [],
150177
"implicitDependencies": [],
151178
"tags": []
152179
}
153180
},
154181
"sourceMap": {
155182
"root": ["package.json", "nx/core/package-json-workspaces"],
156183
"targets": ["package.json", "nx/core/package-json-workspaces"],
157-
"targets.lint": ["package.json", "@nx/eslint/plugin"],
158-
"targets.lint.command": ["package.json", "@nx/eslint/plugin"],
159-
"targets.lint.cache": ["package.json", "@nx/eslint/plugin"],
160-
"targets.lint.options": ["package.json", "@nx/eslint/plugin"],
161-
"targets.lint.inputs": ["package.json", "@nx/eslint/plugin"],
162-
"targets.lint.options.cwd": ["package.json", "@nx/eslint/plugin"],
163-
"targets.build": ["next.config.js", "@nx/next/plugin"],
164-
"targets.build.command": ["next.config.js", "@nx/next/plugin"],
165-
"targets.build.options": ["next.config.js", "@nx/next/plugin"],
166-
"targets.build.dependsOn": ["next.config.js", "@nx/next/plugin"],
167-
"targets.build.cache": ["next.config.js", "@nx/next/plugin"],
168-
"targets.build.inputs": ["next.config.js", "@nx/next/plugin"],
169-
"targets.build.outputs": ["next.config.js", "@nx/next/plugin"],
170-
"targets.build.options.cwd": ["next.config.js", "@nx/next/plugin"],
184+
"targets.eslint:lint": [".eslintrc.json", "@nx/eslint/plugin"],
185+
"targets.eslint:lint.command": [".eslintrc.json", "@nx/eslint/plugin"],
186+
"targets.eslint:lint.cache": [".eslintrc.json", "@nx/eslint/plugin"],
187+
"targets.eslint:lint.options": [".eslintrc.json", "@nx/eslint/plugin"],
188+
"targets.eslint:lint.inputs": [".eslintrc.json", "@nx/eslint/plugin"],
189+
"targets.eslint:lint.options.cwd": [".eslintrc.json", "@nx/eslint/plugin"],
190+
"targets.next:build": ["next.config.js", "@nx/next/plugin"],
191+
"targets.next:build.command": ["next.config.js", "@nx/next/plugin"],
192+
"targets.next:build.options": ["next.config.js", "@nx/next/plugin"],
193+
"targets.next:build.dependsOn": ["next.config.js", "@nx/next/plugin"],
194+
"targets.next:build.cache": ["next.config.js", "@nx/next/plugin"],
195+
"targets.next:build.inputs": ["next.config.js", "@nx/next/plugin"],
196+
"targets.next:build.outputs": ["next.config.js", "@nx/next/plugin"],
197+
"targets.next:build.options.cwd": ["next.config.js", "@nx/next/plugin"],
171198
"targets.dev": ["next.config.js", "@nx/next/plugin"],
172199
"targets.dev.command": ["next.config.js", "@nx/next/plugin"],
173200
"targets.dev.options": ["next.config.js", "@nx/next/plugin"],
@@ -180,7 +207,6 @@ nx show project my-workspace --web
180207
"sourceRoot": ["package.json", "nx/core/package-json-workspaces"],
181208
"name": ["package.json", "nx/core/package-json-workspaces"],
182209
"projectType": ["package.json", "nx/core/package-json-workspaces"],
183-
"includedScripts": ["package.json", "nx/core/package-json-workspaces"],
184210
"targets.nx-release-publish": [
185211
"package.json",
186212
"nx/core/package-json-workspaces"
@@ -203,52 +229,61 @@ nx show project my-workspace --web
203229

204230
{% /project-details %}
205231

206-
The project detail view lists all available tasks, the configuration values for those tasks and where those configuration values are being set.
232+
The project detail view lists all available tasks, the configuration values for those tasks and where those
233+
configuration values are being set.
207234

208235
## Configure an Existing Script to Run with Nx
209236

210237
If you want to run one of your existing scripts with Nx, you need to tell Nx about it.
211238

212239
1. Preface the script with `nx exec -- ` to have `npm run test` invoke the command with Nx.
213-
2. Add the script to `includedScripts`.
214-
3. Define caching settings.
240+
2. Define caching settings.
215241

216-
The `nx exec` command allows you to keep using `npm test` or `npm run test` (or other package manager's alternatives) as you're accustomed to. But still get the benefits of making those operations cacheable. Configuring the `test` script from the example above to run with Nx would look something like this:
242+
The `nx exec` command allows you to keep using `npm test` or `npm run test` (or other package manager's alternatives) as
243+
you're accustomed to. But still get the benefits of making those operations cacheable. Configuring the `test` script
244+
from the example above to run with Nx would look something like this:
217245

218246
```json {% fileName="package.json" %}
219247
{
220248
"name": "my-workspace",
221249
...
222250
"scripts": {
223-
"build": "nx build",
224-
"lint": "nx lint",
251+
"build": "nx next:build",
252+
"lint": "nx eslint:lint",
225253
"test": "nx exec -- node ./run-tests.js"
226254
},
227255
...
228256
"nx": {
229-
"includedScripts": ["test"],
230257
"targets": {
231258
"test": {
232259
"cache": "true",
233-
"inputs": ["default", "^default"],
260+
"inputs": [
261+
"default",
262+
"^default"
263+
],
234264
"outputs": []
235265
}
236266
}
237267
}
238268
}
239269
```
240270

241-
Now if you run `npm run test` or `nx test` twice, the results will be retrieved from the cache. The `inputs` used in this example are as cautious as possible, so you can significantly improve the value of the cache by [customizing Nx Inputs](/recipes/running-tasks/configure-inputs) for each task.
271+
Now if you run `npm run test` or `nx test` twice, the results will be retrieved from the cache. The `inputs` used in
272+
this example are as cautious as possible, so you can significantly improve the value of the cache
273+
by [customizing Nx Inputs](/recipes/running-tasks/configure-inputs) for each task.
242274

243275
## Learn More
244276

245277
{% cards %}
246278

247-
{% card title="Customizing Inputs and Named Inputs" description="Learn more about how to fine-tune caching with custom inputs" type="documentation" url="/recipes/running-tasks/configure-inputs" /%}
279+
{% card title="Customizing Inputs and Named Inputs" description="Learn more about how to fine-tune caching with custom
280+
inputs" type="documentation" url="/recipes/running-tasks/configure-inputs" /%}
248281

249-
{% card title="Cache Task Results" description="Learn more about how caching works" type="documentation" url="/features/cache-task-results" /%}
282+
{% card title="Cache Task Results" description="Learn more about how caching works" type="documentation" url="
283+
/features/cache-task-results" /%}
250284

251-
{% card title="Adding Nx to NPM/Yarn/PNPM Workspace" description="Learn more about how to add Nx to an existing monorepo" type="documentation" url="/recipes/adopting-nx/adding-to-monorepo" /%}
285+
{% card title="Adding Nx to NPM/Yarn/PNPM Workspace" description="Learn more about how to add Nx to an existing
286+
monorepo" type="documentation" url="/recipes/adopting-nx/adding-to-monorepo" /%}
252287

253288
{% /cards %}
254289

0 commit comments

Comments
 (0)