Skip to content

Commit ca5e673

Browse files
authored
feat(misc): add alternative implementation to calculate buildable dependencies using the task graph (nrwl#18125)
1 parent 4d5509b commit ca5e673

File tree

15 files changed

+783
-32
lines changed

15 files changed

+783
-32
lines changed

e2e/js/src/js.test.ts

+70
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
cleanupProject,
55
createFile,
66
newProject,
7+
readFile,
78
readJson,
9+
rmDist,
810
runCLI,
911
runCLIAsync,
1012
uniq,
@@ -160,4 +162,72 @@ export function ${lib}Wildcard() {
160162
runCLI(`build ${nonBuildable}`);
161163
checkFilesExist(`dist/libs/${nonBuildable}/src/index.js`);
162164
});
165+
166+
it('should build buildable libraries using the task graph and handle more scenarios than current implementation', () => {
167+
const lib1 = uniq('lib1');
168+
const lib2 = uniq('lib2');
169+
runCLI(`generate @nx/js:lib ${lib1} --bundler=tsc --no-interactive`);
170+
runCLI(`generate @nx/js:lib ${lib2} --bundler=tsc --no-interactive`);
171+
172+
// add dep between lib1 and lib2
173+
updateFile(
174+
`libs/${lib1}/src/index.ts`,
175+
`export { ${lib2} } from '@${scope}/${lib2}';`
176+
);
177+
178+
// check current implementation
179+
expect(runCLI(`build ${lib1} --skip-nx-cache`)).toContain(
180+
'Done compiling TypeScript files'
181+
);
182+
checkFilesExist(`dist/libs/${lib1}/src/index.js`);
183+
checkFilesExist(`dist/libs/${lib2}/src/index.js`);
184+
185+
// cleanup dist
186+
rmDist();
187+
188+
// check task graph implementation
189+
expect(
190+
runCLI(`build ${lib1} --skip-nx-cache`, {
191+
env: { NX_BUILDABLE_LIBRARIES_TASK_GRAPH: 'true' },
192+
})
193+
).toContain('Done compiling TypeScript files');
194+
checkFilesExist(`dist/libs/${lib1}/src/index.js`);
195+
checkFilesExist(`dist/libs/${lib2}/src/index.js`);
196+
197+
// change build target name of lib2 and update target dependencies
198+
updateJson(`libs/${lib2}/project.json`, (json) => {
199+
json.targets['my-custom-build'] = json.targets.build;
200+
delete json.targets.build;
201+
return json;
202+
});
203+
const originalNxJson = readFile('nx.json');
204+
updateJson('nx.json', (json) => {
205+
json.targetDefaults.build = {
206+
...json.targetDefaults.build,
207+
dependsOn: [...json.targetDefaults.build.dependsOn, '^my-custom-build'],
208+
};
209+
return json;
210+
});
211+
212+
// cleanup dist
213+
rmDist();
214+
215+
// check current implementation, it doesn't support a different build target name
216+
expect(() => runCLI(`build ${lib1} --skip-nx-cache`)).toThrow();
217+
218+
// cleanup dist
219+
rmDist();
220+
221+
// check task graph implementation
222+
expect(
223+
runCLI(`build ${lib1} --skip-nx-cache`, {
224+
env: { NX_BUILDABLE_LIBRARIES_TASK_GRAPH: 'true' },
225+
})
226+
).toContain('Done compiling TypeScript files');
227+
checkFilesExist(`dist/libs/${lib1}/src/index.js`);
228+
checkFilesExist(`dist/libs/${lib2}/src/index.js`);
229+
230+
// restore nx.json
231+
updateFile('nx.json', () => originalNxJson);
232+
});
163233
});

packages/angular/src/executors/delegate-build/delegate-build.impl.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ExecutorContext } from '@nx/devkit';
22
import { joinPathFragments, parseTargetString, runExecutor } from '@nx/devkit';
33
import {
4-
calculateProjectDependencies,
4+
calculateProjectBuildableDependencies,
55
checkDependentProjectsHaveBeenBuilt,
66
createTmpTsConfig,
77
} from '@nx/js/src/utils/buildable-libs-utils';
@@ -11,7 +11,8 @@ export async function* delegateBuildExecutor(
1111
options: DelegateBuildExecutorSchema,
1212
context: ExecutorContext
1313
) {
14-
const { target, dependencies } = calculateProjectDependencies(
14+
const { target, dependencies } = calculateProjectBuildableDependencies(
15+
context.taskGraph,
1516
context.projectGraph,
1617
context.root,
1718
context.projectName,

packages/angular/src/executors/ng-packagr-lite/ng-packagr-lite.impl.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('NgPackagrLite executor', () => {
3030

3131
beforeEach(async () => {
3232
(
33-
buildableLibsUtils.calculateProjectDependencies as jest.Mock
33+
buildableLibsUtils.calculateProjectBuildableDependencies as jest.Mock
3434
).mockImplementation(() => ({
3535
target: {},
3636
dependencies: [],

packages/angular/src/executors/package/package.impl.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('Package executor', () => {
3030

3131
beforeEach(async () => {
3232
(
33-
buildableLibsUtils.calculateProjectDependencies as jest.Mock
33+
buildableLibsUtils.calculateProjectBuildableDependencies as jest.Mock
3434
).mockImplementation(() => ({
3535
target: {},
3636
dependencies: [],

packages/angular/src/executors/package/package.impl.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ExecutorContext } from '@nx/devkit';
22
import { eachValueFrom } from '@nx/devkit/src/utils/rxjs-for-await';
33
import {
4-
calculateProjectDependencies,
4+
calculateProjectBuildableDependencies,
55
checkDependentProjectsHaveBeenBuilt,
66
createTmpTsConfig,
77
DependentBuildableProjectNode,
@@ -72,7 +72,8 @@ export function createLibraryExecutor(
7272
context: ExecutorContext
7373
) {
7474
const { target, dependencies, topLevelDependencies } =
75-
calculateProjectDependencies(
75+
calculateProjectBuildableDependencies(
76+
context.taskGraph,
7677
context.projectGraph,
7778
context.root,
7879
context.projectName,

packages/js/src/executors/node/node.impl.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import * as path from 'path';
1717
import { join } from 'path';
1818

1919
import { InspectType, NodeExecutorOptions } from './schema';
20-
import { calculateProjectDependencies } from '../../utils/buildable-libs-utils';
20+
import { calculateProjectBuildableDependencies } from '../../utils/buildable-libs-utils';
2121
import { killTree } from './lib/kill-tree';
2222
import { fileExists } from 'nx/src/utils/fileutils';
2323
import { getMainFileDirRelativeToProjectRoot } from '../../utils/get-main-file-dir';
@@ -314,7 +314,8 @@ function calculateResolveMappings(
314314
options: NodeExecutorOptions
315315
) {
316316
const parsed = parseTargetString(options.buildTarget, context.projectGraph);
317-
const { dependencies } = calculateProjectDependencies(
317+
const { dependencies } = calculateProjectBuildableDependencies(
318+
context.taskGraph,
318319
context.projectGraph,
319320
context.root,
320321
parsed.project,

packages/js/src/executors/tsc/lib/batch/build-task-info-per-tsconfig-map.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ExecutorContext } from '@nx/devkit';
22
import { parseTargetString } from '@nx/devkit';
33
import { join, relative } from 'path';
44
import { CopyAssetsHandler } from '../../../../utils/assets/copy-assets-handler';
5-
import { calculateProjectDependencies } from '../../../../utils/buildable-libs-utils';
5+
import { calculateProjectBuildableDependencies } from '../../../../utils/buildable-libs-utils';
66
import type { NormalizedExecutorOptions } from '../../../../utils/schema';
77
import { getTaskWithTscExecutorOptions } from '../get-task-options';
88
import type { TypescriptInMemoryTsConfig } from '../typescript-compilation';
@@ -97,7 +97,8 @@ function createTaskInfo(
9797
const {
9898
target: projectGraphNode,
9999
dependencies: buildableProjectNodeDependencies,
100-
} = calculateProjectDependencies(
100+
} = calculateProjectBuildableDependencies(
101+
context.taskGraph,
101102
context.projectGraph,
102103
context.root,
103104
context.taskGraph.tasks[taskName].target.project,

0 commit comments

Comments
 (0)