File size: 3,842 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# Dependency management
This project uses [yarn v3](https://yarnpkg.com/) to manage its dependencies. It uses workspaces[https://yarnpkg.com/features/workspaces] functionality to manage the monorepo.
## Working with sub-packages
In this context, a 'sub-package' is any package of the monorepo. That includes `./packages/*`, `./client` and `./apps/*`.
With `yarn`, there are two different modes to work with a sub-package:
**Option 1**: Go to the directory that contains the package and run regular yarn commands. Example:
```
cd packages/calypso-analytics
yarn add ...
```
**Option 2**: Run `yarn` commands in teh root of the project, but prepend `workspace <packageName>`. Example
```
yarn workspace @automattic/calypso-analytics add...
```
Both options are equivalent, is a matter of personal preference. For the rest of this guide, the examples will follow Option 1.
## Common tasks
### Add a new dependency
```
cd <package-dir>
yarn add <dependency>
# Example:
# cd packages/calypso-analytics
# yarn add lodash
```
You should add dependencies to the root project _only_ when it will be used to test and/or build other packages. To do this, run:
```
yarn add -w <dependency>
```
#### Unpublished package as a dependency
Sometimes you'll want to [create a package](https://github.com/Automattic/wp-calypso/blob/HEAD/docs/guide/monorepo.md#a-sample-packagejson) and use it as another workspace's dependency before it's published. To add such dependency, make sure you **specify its exact version** because otherwise, `yarn` will try to resolve it from the `npm` registry and throw the `Not found` error.
Let's say you created a polyfill package and want to add it as a dependency to `packages/calypso-polyfills`. Doing this will make `yarn` resolve to your fresh (unpublished) package and install it as a symlink:
```
cd packages/calypso-polyfills
yarn add @automattic/my-awesome-polyfill@1.0.0
```
That's it! Don't forget to [publish the package](https://github.com/Automattic/wp-calypso/blob/HEAD/docs/guide/monorepo.md#publishing) after merging your PR!
### Delete a dependency
```
cd <package-dir>
yarn remove <dependency>
# Example:
# cd packages/calypso-analytics
# yarn remove lodash
```
To delete a dependency of the root project, run:
```
yarn remove -w lodash
```
### Update a dependency
Run
```
yarn up <package>
# Example
# yarn up react-query
```
Note that this won't change the required range of `react-query` (i.e. it won't modify `package.json`). Instead, it will try to update `react-query` and any of its dependencies to the highest version that satisfies the specified range.
For example, if we declare a dependency on `react-query@^2.24.0` it may update react-query to `2.24.1`, but never to `3.0.0`.
### Update a dependency to a new range
Run
```
yarn up <package>@^<semver-range>
# Example
# yarn up react-query@^3.0.0
```
As before, it will update `react-query` and all its dependencies. But in this case, it _will_ change the required range (i.e. it will modify `package.json`)
### List oudated dependencies
Run
```
yarn outdated
```
Note that the output includes which sub-package has the dependency. It is possible that the same dependency is present in many sub-packages (or even in the root project).
### List duplicated dependencies
Run
```
yarn dedupe --check
```
It is recommended to run this command after adding a new dependency and fix potential duplications with `yarn dedupe`
## Differences with `npm`
### Running scripts
When working with `yarn` we don't have to specify `run` in the command line:
```
# Before:
npm run build-client
# After:
yarn build-client
```
### Other
Check the [official documentation](https://classic.yarnpkg.com/en/docs/migrating-from-npm/#toc-cli-commands-comparison) to see more equivalences between `npm` and `yarn` commands.
|