-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): infer build-deps and watch-deps targets for incremental bui…
…lds (#29609) This PR adds `build-deps` and `watch-deps` targets to buildable JS projects to help with incremental builds. A use-case for this is if an app (e.g. Vite React app) has buildable dependencies that need to be rebuilt when they change. Say, you create a React app and lib as follows: ``` nx g @nx/react:app apps/react-app --bundler vite nx g @nx/react:lib packages/react-lib --bundler vite ``` And import `react-lib` inside the app. ```jsx import { ReactLib } from '@acme/react-lib'; //... return <ReactLib /> ``` The user can then run: ``` nx watch-deps react-app ``` And then serve the app in another terminal: ``` nx serve react-app ``` Then whenever code is updated for a buildable dependency, it'll be rebuilt and then reloaded in the app.
- Loading branch information
Showing
38 changed files
with
634 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { readJsonFile, type TargetConfiguration } from '@nx/devkit'; | ||
import { existsSync } from 'node:fs'; | ||
import { type PackageManagerCommands } from 'nx/src/utils/package-manager'; | ||
import { join } from 'path'; | ||
|
||
/** | ||
* Allow uses that use incremental builds to run `nx watch-deps` to continuously build all dependencies. | ||
*/ | ||
export function addBuildAndWatchDepsTargets( | ||
workspaceRoot: string, | ||
projectRoot: string, | ||
targets: Record<string, TargetConfiguration>, | ||
options: { buildDepsTargetName?: string; watchDepsTargetName?: string }, | ||
pmc: PackageManagerCommands | ||
): void { | ||
let projectName: string; | ||
|
||
const projectJsonPath = join(workspaceRoot, projectRoot, 'project.json'); | ||
const packageJsonPath = join(workspaceRoot, projectRoot, 'package.json'); | ||
|
||
if (existsSync(projectJsonPath)) { | ||
const projectJson = readJsonFile(projectJsonPath); | ||
projectName = projectJson.name; | ||
} else if (existsSync(packageJsonPath)) { | ||
const packageJson = readJsonFile(packageJsonPath); | ||
projectName = packageJson.nx?.name ?? packageJson.name; | ||
} | ||
|
||
if (!projectName) return; | ||
|
||
if (projectName) { | ||
const buildDepsTargetName = options.buildDepsTargetName ?? 'build-deps'; | ||
targets[buildDepsTargetName] = { | ||
dependsOn: ['^build'], | ||
}; | ||
targets[options.watchDepsTargetName ?? 'watch-deps'] = { | ||
dependsOn: [buildDepsTargetName], | ||
command: `${pmc.exec} nx watch --projects ${projectName} --includeDependentProjects -- ${pmc.exec} nx ${buildDepsTargetName} ${projectName}`, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.