diff --git a/docs/shared/tutorials/npm-workspaces.md b/docs/shared/tutorials/npm-workspaces.md index 5663831a84ab14..3c7a31a9d5fe6e 100644 --- a/docs/shared/tutorials/npm-workspaces.md +++ b/docs/shared/tutorials/npm-workspaces.md @@ -5,12 +5,16 @@ description: In this tutorial you'll add Nx to an existing NPM workspaces repo # NPM Workspaces Tutorial -In this tutorial, you'll learn how to add Nx to a repository with an existing NPM workspaces setup. You'll see how Nx can provide immediate value with very little configuration and then you can gradually enable more features. +In this tutorial, you'll learn how to add Nx to a repository with an existing [NPM workspaces](https://docs.npmjs.com/cli/using-npm/workspaces) setup. -- Add Nx to the repository with a single command -- Configure caching for your existing tasks -- Configure a task pipeline -- Use Nx Plugins to automatically configure caching +What will you learn? + +- how to add Nx to the repository with a single command +- how to configure caching for your tasks +- how to configure a task pipeline +- how to configure projects automatically with Nx Plugins +- how to manage your releases with `nx release` +- how to speed up CI with Nx Cloud ⚡ +{% nx-cloud-section %} + ## Starting Repository -To get started, check out [the sample repository](https://github.com/nrwl/tuskydesign) on your local machine: +To get started, fork [the sample repository](https://github.com/nrwl/tuskydesign/fork) and clone it on your local machine: ```shell -git clone https://github.com/nrwl/tuskydesign.git +git clone https://github.com//tuskydesign.git ``` The repository has two React packages (under `packages/buttons` and `packages/forms`) that are used in a `demo` application (located in `apps/demo`) that was designed to be used with the Vite CLI. The root `package.json` has a `workspaces` property that tells NPM how to find the projects in the repository. @@ -63,10 +69,18 @@ When the `buttons` and `forms` projects are built first, the `demo` app can buil Now that you have a basic understanding of the repository we're working with, let's see how Nx can help us. -## Add Nx +{% /nx-cloud-section %} + +{% nx-cloud-section variant="divider" /%} + +{% nx-cloud-section %} + +## Smart Monorepo Nx offers many features, but at its core, it is a task runner. Out of the box, it can cache your tasks and ensure those tasks are run in the correct order. After the initial set up, you can incrementally add on other features that would be helpful in your organization. +### Add Nx + To enable Nx in your repository, run a single command: ```shell {% path="~/tuskydesigns" %} @@ -85,10 +99,10 @@ Second, the script asks a series of questions to help set up caching for you. - `Which scripts are cacheable?` - Choose `typecheck`, `build` and `lint` - `Does the "typecheck" script create any outputs?` - Enter nothing - `Does the "build" script create any outputs?` - Enter `dist` -- `Does the "lint" script create any outputs?` - Enter nothing +- `Does the "lint" script creggggggate any outputs?` - Enter nothing - `Would you like remote caching to make your build faster?` - Choose `Skip for now` -## Explore Your Workspace +### Explore Your Workspace If you run `nx graph` as instructed, you'll see the dependencies between your projects. @@ -101,7 +115,7 @@ npx nx graph --focus=@tuskdesign/demo Nx uses this graph to determine the order tasks are run and enforce module boundaries. You can also leverage this graph to gain an accurate understanding of the architecture of your codebase. Part of what makes this graph invaluable is that it is derived directly from your codebase, so it will never become out of date. -## Caching Pre-configured +### Caching Pre-configured Nx has been configured to run your `build`, `typecheck` and `lint` tasks. You can run a single task like this: @@ -151,7 +165,7 @@ Nx read the output from the cache instead of running the command for 3 out of 3 You can see the same caching behavior working when you run `npx nx lint` or `npx nx typecheck`. -## Use Task Pipelines +### Use Task Pipelines You may be wondering why the caching message in the previous section mentioned 3 tasks when you only ran the `build` task from the terminal. When we said that `build` tasks must be run in order during the setup script, Nx created a simple task pipeline. You can see the configuration for it in the `nx.json` file: @@ -186,7 +200,7 @@ Nx read the output from the cache instead of running the command for 3 out of 3 Not only does the build complete successfully, but it finishes instantly and the `packages/forms/dist` folder is put back in place thanks to the caching. -## Create a Task Pipeline +### Create a Task Pipeline You may have noticed in the `apps/demo/package.json` file, there is a `prebuild` script that runs `typecheck` before the `build` script in order to catch any type errors. Let's set up this same behavior in the Nx task pipeline as well. @@ -212,7 +226,7 @@ You may have noticed in the `apps/demo/package.json` file, there is a `prebuild` The `dependsOn` line makes Nx run the `typecheck` task for the current project and the `build` task for any dependencies before running the current project's `build` task. Now `nx build` will run the `typecheck` task just like `npm run build` does. -## Use Nx Plugins to Enhance Vite Tasks with Caching +### Use Nx Plugins to Enhance Vite Tasks with Caching You may remember that we defined the `outputs` property in `nx.json` when we were answering questions in the `nx init` script. The value is currently hard-coded so that if you change the output path in your `vite.config.ts`, you have to remember to also change the `outputs` array in the `build` task configuration. This is where plugins can help. They directly infer information from the actual tooling configuration files (`vite.config.ts` in this case). @@ -285,15 +299,21 @@ Now if you look at project details view again, you'll see that the `outputs` pro You can also add the `@nx/eslint` plugin to see how it infers `lint` tasks based on the ESLint configuration files. -## Summary +### Summary -After following this tutorial, the repository is still using all the same tools to run tasks, but now Nx runs those tasks in a smarter way. The tasks are efficiently cached so that there is no repeated work and the cache configuration settings are automatically synced with your tooling configuration files by Nx plugins. Also, any task dependencies are automatically executed whenever needed because we configured task pipelines for the projects. +After adding Nx, the repository is still using all the same tools to run tasks, but now Nx runs those tasks in a smarter way. The tasks are efficiently cached so that there is no repeated work and the cache configuration settings are automatically synced with your tooling configuration files by Nx plugins. Also, any task dependencies are automatically executed whenever needed because we configured task pipelines for the projects. The final task graph for `demo` app's `build` task looks like this: {% graph height="200px" title="Build Task Pipeline" type="task" jsonFile="shared/tutorials/npm-workspaces-build-tasks2.json" %} {% /graph %} +{% /nx-cloud-section %} + +{% nx-cloud-section variant="divider" /%} + +{% nx-cloud-section variant="release" %} + ## Manage Releases If you decide to publish the `forms` or `buttons` packages on NPM, Nx can also help you [manage the release process](/features/manage-releases). Release management involves updating the version of your package, populating a changelog, and publishing the new version to the NPM registry. @@ -323,25 +343,33 @@ nx release --first-release ``` After this first release, you can remove the `--first-release` flag and just run `nx release --dry-run`. There is also a [dedicated feature page](/features/manage-releases) that goes into more detail about how to use the `nx release` command. +{% /nx-cloud-section %} + +{% nx-cloud-section variant="divider" /%} -## Set Up CI for Your NPM Workspace +{% nx-cloud-section variant="nx-cloud" %} -This tutorial walked you through how Nx can improve the local development experience, but the biggest difference Nx makes is in CI. As repositories get bigger, making sure that the CI is fast, reliable and maintainable can get very challenging. Nx provides a solution. +## Fast CI ⚡ {% tableOfContentsColor="green" %} + +{% callout type="check" title="Forked repository with Nx" %} +Make sure you have completed the previous sections of this tutorial before starting this one. If you want a clean starting point, you can fork the [sample repository with Nx already added](/). +{% /callout %} + +So far in this tutorial you've seen how Nx improves the local development experience, but the biggest difference Nx makes is in CI. As repositories get bigger, making sure that the CI is fast, reliable and maintainable can get very challenging. Nx provides a solution. - Nx reduces wasted time in CI with the [`affected` command](/ci/features/affected). - Nx Replay's [remote caching](/ci/features/remote-cache) will reuse task artifacts from different CI executions making sure you will never run the same computation twice. -- Nx Agents [efficiently distribute tasks across machines](/ci/concepts/parallelization-distribution) ensuring constant CI time regardless of the repository size. The right number of machines is allocated for each PR to ensure good performance without wasting compute. +- Nx Agents [efficiently distribute tasks across machines](/ci/features/distribute-task-execution) ensuring constant CI time regardless of the repository size. The right number of machines is allocated for each PR to ensure good performance without wasting compute. - Nx Atomizer [automatically splits](/ci/features/split-e2e-tasks) large e2e tests to distribute them across machines. Nx can also automatically [identify and rerun flaky e2e tests](/ci/features/flaky-tasks). -### Connect to Nx Cloud +### Connect to Nx Cloud {% tableOfContentsColor="green" %} Nx Cloud is a companion app for your CI system that provides remote caching, task distribution, e2e tests deflaking, better DX and more. Now that we're working on the CI pipeline, it is important for your changes to be pushed to a GitHub repository. 1. Commit your existing changes with `git add . && git commit -am "updates"` -2. [Create a new GitHub repository](https://github.com/new) -3. Follow GitHub's instructions to push your existing code to the repository +2. Push your changes to your forked GitHub repository with `git push` Now connect your repository to Nx Cloud with the following command: @@ -365,7 +393,7 @@ git pull You should now have an `nxCloudAccessToken` property specified in the `nx.json` file. -### Create a CI Workflow +### Create a CI Workflow {% tableOfContentsColor="green" %} Use the following command to generate a CI workflow file. @@ -373,7 +401,7 @@ Use the following command to generate a CI workflow file. npx nx generate ci-workflow --ci=github ``` -This generator creates a `.github/workflows/ci.yml` file that contains a CI pipeline that will run the `lint`, `test`, `build` and `e2e` tasks for projects that are affected by any given PR. Since we are using Nx Cloud, the pipeline will also distribute tasks across multiple machines to ensure fast and reliable CI runs. +This generator creates a `.github/workflows/ci.yml` file that contains a CI pipeline that will run the `lint`, `test`, `build` and `e2e` tasks for projects that are affected by any given PR. Since we are using Nx Cloud, the pipeline will also [distribute tasks across multiple machines](/ci/features/distribute-task-execution) to ensure fast and reliable CI runs. The key lines in the CI pipeline are: @@ -402,7 +430,7 @@ jobs: - run: npx nx affected -t lint test build ``` -### Open a Pull Request +### Open a Pull Request {% tableOfContentsColor="green" %} Commit the changes and open a new PR on GitHub. @@ -425,6 +453,8 @@ For more information about how Nx can improve your CI pipeline, check out one of - [Circle CI with Nx](/ci/intro/tutorials/circle) - [GitHub Actions with Nx](/ci/intro/tutorials/github-actions) +{% /nx-cloud-section %} + ## Next Steps Connect with the rest of the Nx community with these resources: diff --git a/nx-dev/feature-doc-viewer/src/lib/table-of-contents.tsx b/nx-dev/feature-doc-viewer/src/lib/table-of-contents.tsx index 364a4c18c572e9..fa76e7c2debea4 100644 --- a/nx-dev/feature-doc-viewer/src/lib/table-of-contents.tsx +++ b/nx-dev/feature-doc-viewer/src/lib/table-of-contents.tsx @@ -7,6 +7,7 @@ interface Heading { id: string; level: number; title: string; + tableOfContentsColor?: string; } export function collectHeadings( @@ -28,8 +29,7 @@ export function collectHeadings( if (typeof title === 'string') { sections.push({ - id: node.attributes['id'], - level: node.attributes['level'], + ...node.attributes, title, }); } @@ -73,6 +73,7 @@ export function TableOfContents({ headings.find((i) => i.level === 1)?.title || null ); + console.log({ items }); return ( <>