diff --git a/docs/shared/configuration/packagejson.md b/docs/shared/configuration/packagejson.md index fcd00208102d6..00ea7dd4d9de1 100644 --- a/docs/shared/configuration/packagejson.md +++ b/docs/shared/configuration/packagejson.md @@ -120,12 +120,49 @@ sources (non-test sources) of its dependencies. In other words, it treats test s ### outputs -`"outputs": ["dist/libs/mylib"]` tells Nx where the `build` target is going to create file artifacts. The provided value -is actually the default, so we can omit it in this case. `"outputs": []` tells Nx that the `test` target doesn't create -any artifacts on disk. +Targets may define outputs to tell Nx where the target is going to create file artifacts that Nx should cache. `"outputs": ["dist/libs/mylib"]` tells Nx where the `build` target is going to create file artifacts. -This configuration is usually not needed. Nx comes with reasonable defaults (imported in `nx.json`) which implement the -configuration above. +This configuration is usually not needed. Nx comes with reasonable defaults (imported in `nx.json`) which implement the configuration above. + +#### Basic Example + +Usually, a target writes to a specific directory or a file. The following instructs Nx to cache `dist/libs/mylib` and `build/libs/mylib/main.js`: + +```json +{ + "build": { + "outputs": ["dist/libs/mylib", "build/libs/mylib/main.js"] + } +} +``` + +#### Specifying Globs + +Sometimes, multiple targets might write to the same directory. When possible it is recommended to direct these targets into separate directories. + +```json +{ + "build-js": { + "outputs": ["dist/libs/mylib/js"] + }, + "build-css": { + "outputs": ["dist/libs/mylib/css"] + } +} +``` + +But if the above is not possible, globs can be specified as outputs to only cache a set of files rather than the whole directory. + +```json +{ + "build-js": { + "outputs": ["dist/libs/mylib/**/*.js"] + }, + "build-css": { + "outputs": ["dist/libs/mylib/**/*.css"] + } +} +``` ### dependsOn diff --git a/docs/shared/configuration/projectjson.md b/docs/shared/configuration/projectjson.md index 07325fba786be..81f1bce9d2eca 100644 --- a/docs/shared/configuration/projectjson.md +++ b/docs/shared/configuration/projectjson.md @@ -271,9 +271,85 @@ sources (non-test sources) of its dependencies. In other words, it treats test s ### Outputs -`"outputs": ["dist/libs/mylib"]` tells Nx where the `build` target is going to create file artifacts. The provided value -is actually the default, so we can omit it in this case. `"outputs": []` tells Nx that the `test` target doesn't create -any artifacts on disk. +Targets may define outputs to tell Nx where the target is going to create file artifacts that Nx should cache. `"outputs": ["dist/libs/mylib"]` tells Nx where the `build` target is going to create file artifacts. + +#### Basic Example + +Usually, a target writes to a specific directory or a file. The following instructs Nx to cache `dist/libs/mylib` and `build/libs/mylib/main.js`: + +```json + { + "build": { + ..., + "outputs": ["dist/libs/mylib", "build/libs/mylib/main.js"], + "options": { + ... + }, + } + } +``` + +#### Referencing Options + +Most commonly, targets have an option for an output file or directory. Rather than duplicating the information as seen above, options can be referenced using the below syntax: + +> When the `outputPath` option is changed, Nx will start caching the new path as well. + +```json +{ + "build": { + ..., + "outputs": ["{options.outputPath}"], + "options": { + "outputPath": "dist/libs/mylib" + } + } +} +``` + +#### Specifying Globs + +Sometimes, multiple targets might write to the same directory. When possible it is recommended to direct these targets into separate directories. + +```json +{ + "build-js": { + ..., + "outputs": ["dist/libs/mylib/js"], + "options": { + "outputPath": "dist/libs/mylib/js" + } + }, + "build-css": { + ..., + "outputs": ["dist/libs/mylib/css"], + "options": { + "outputPath": "dist/libs/mylib/css" + } + } +} +``` + +But if the above is not possible, globs can be specified as outputs to only cache a set of files rather than the whole directory. + +```json +{ + "build-js": { + ..., + "outputs": ["dist/libs/mylib/**/*.js"], + "options": { + "outputPath": "dist/libs/mylib" + } + }, + "build-css": { + ..., + "outputs": ["dist/libs/mylib/**/*.css"], + "options": { + "outputPath": "dist/libs/mylib" + } + } +} +``` ### dependsOn