Introduce path variables to project.json files
See original GitHub issueDescription
Introduce 2 variables to be used in projects.json
files:
- One with the absolute path to the monorepo directory;
- Another one with the absolute path to the project directory.
Motivation
The introduction of project.json
files in v12 (and the ability to extract project configurations from workspace.json
to them) was a great improvement. Keeping configuration close to the project it concerns is great. But I feel that those files (project.json
) should be unaware of both the monorepo path and the project path. It’s a lot of repetition and error prone if you’re scoping using subpaths and/or need to use the paths for custom targets.
Using variables allow for cleaner values and more portability.
Suggested Implementation
Suppose there are 2 variables available: NX_ROOT
and NX_PROJECT
. A project.json
file would only need to use those variables to reference the monrepo root and the project path:
{
"root": "$NX_PROJECT",
"sourceRoot": "$NX_PROJECT/src",
"projectType": "application",
"targets": {
"package": {
"executor": "@nrwl/workspace:run-commands",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "$NX_ROOT/dist/serverless/payment.package",
"commands": [
"rm -rf $NX_ROOT/dist/serverless/payment.package",
"pnpx nx run serverless-payment-app:build --with-deps",
"cd $NX_PROJECT && $NX_ROOT/node_modules/.bin/serverless package --config serverless.ts --package $NX_ROOT/dist/serverless/payment.package"
],
"parallel": false
}
}
}
Those variables would automatically be expanded by NX. Also, those variables would be made available as environment variables to any command/shell executed by a target.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:44
- Comments:9 (4 by maintainers)
Seems like an ideal place to ask where the
"{}"
syntax intargets[].outputs[]
comes from and why the syntax for variable expansion would not be consistent?Eg.
Hello.
We have some similar needs, especially because we have some apps in our monorepo, which need to be mirrored into a public repo (community distributions), with their own angular.json. At the moment, we needed to duplicate the content of angular.json files related to those particular apps, but with the current approach (version 2), we would still need to have duplications since the paths are hardcoded into the project.json.
I really like the suggestion from @diegovilar, however I reckon, using a different notation might be better, to avoid those template strings (
$NX_ROOT
,$NX_PROJECT
) to be mistaken with shell environment variables. What about following something similar which I saw in the jest config for example?<nx_root>
/…<nx_project>
/…