Skip to content

Commit

Permalink
feat(misc): add alternative implementation to calculate buildable dep…
Browse files Browse the repository at this point in the history
…endencies using the task graph (#18125)
  • Loading branch information
leosvelperez authored Jul 19, 2023
1 parent 4d5509b commit ca5e673
Show file tree
Hide file tree
Showing 15 changed files with 783 additions and 32 deletions.
70 changes: 70 additions & 0 deletions e2e/js/src/js.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
cleanupProject,
createFile,
newProject,
readFile,
readJson,
rmDist,
runCLI,
runCLIAsync,
uniq,
Expand Down Expand Up @@ -160,4 +162,72 @@ export function ${lib}Wildcard() {
runCLI(`build ${nonBuildable}`);
checkFilesExist(`dist/libs/${nonBuildable}/src/index.js`);
});

it('should build buildable libraries using the task graph and handle more scenarios than current implementation', () => {
const lib1 = uniq('lib1');
const lib2 = uniq('lib2');
runCLI(`generate @nx/js:lib ${lib1} --bundler=tsc --no-interactive`);
runCLI(`generate @nx/js:lib ${lib2} --bundler=tsc --no-interactive`);

// add dep between lib1 and lib2
updateFile(
`libs/${lib1}/src/index.ts`,
`export { ${lib2} } from '@${scope}/${lib2}';`
);

// check current implementation
expect(runCLI(`build ${lib1} --skip-nx-cache`)).toContain(
'Done compiling TypeScript files'
);
checkFilesExist(`dist/libs/${lib1}/src/index.js`);
checkFilesExist(`dist/libs/${lib2}/src/index.js`);

// cleanup dist
rmDist();

// check task graph implementation
expect(
runCLI(`build ${lib1} --skip-nx-cache`, {
env: { NX_BUILDABLE_LIBRARIES_TASK_GRAPH: 'true' },
})
).toContain('Done compiling TypeScript files');
checkFilesExist(`dist/libs/${lib1}/src/index.js`);
checkFilesExist(`dist/libs/${lib2}/src/index.js`);

// change build target name of lib2 and update target dependencies
updateJson(`libs/${lib2}/project.json`, (json) => {
json.targets['my-custom-build'] = json.targets.build;
delete json.targets.build;
return json;
});
const originalNxJson = readFile('nx.json');
updateJson('nx.json', (json) => {
json.targetDefaults.build = {
...json.targetDefaults.build,
dependsOn: [...json.targetDefaults.build.dependsOn, '^my-custom-build'],
};
return json;
});

// cleanup dist
rmDist();

// check current implementation, it doesn't support a different build target name
expect(() => runCLI(`build ${lib1} --skip-nx-cache`)).toThrow();

// cleanup dist
rmDist();

// check task graph implementation
expect(
runCLI(`build ${lib1} --skip-nx-cache`, {
env: { NX_BUILDABLE_LIBRARIES_TASK_GRAPH: 'true' },
})
).toContain('Done compiling TypeScript files');
checkFilesExist(`dist/libs/${lib1}/src/index.js`);
checkFilesExist(`dist/libs/${lib2}/src/index.js`);

// restore nx.json
updateFile('nx.json', () => originalNxJson);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ExecutorContext } from '@nx/devkit';
import { joinPathFragments, parseTargetString, runExecutor } from '@nx/devkit';
import {
calculateProjectDependencies,
calculateProjectBuildableDependencies,
checkDependentProjectsHaveBeenBuilt,
createTmpTsConfig,
} from '@nx/js/src/utils/buildable-libs-utils';
Expand All @@ -11,7 +11,8 @@ export async function* delegateBuildExecutor(
options: DelegateBuildExecutorSchema,
context: ExecutorContext
) {
const { target, dependencies } = calculateProjectDependencies(
const { target, dependencies } = calculateProjectBuildableDependencies(
context.taskGraph,
context.projectGraph,
context.root,
context.projectName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('NgPackagrLite executor', () => {

beforeEach(async () => {
(
buildableLibsUtils.calculateProjectDependencies as jest.Mock
buildableLibsUtils.calculateProjectBuildableDependencies as jest.Mock
).mockImplementation(() => ({
target: {},
dependencies: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Package executor', () => {

beforeEach(async () => {
(
buildableLibsUtils.calculateProjectDependencies as jest.Mock
buildableLibsUtils.calculateProjectBuildableDependencies as jest.Mock
).mockImplementation(() => ({
target: {},
dependencies: [],
Expand Down
5 changes: 3 additions & 2 deletions packages/angular/src/executors/package/package.impl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ExecutorContext } from '@nx/devkit';
import { eachValueFrom } from '@nx/devkit/src/utils/rxjs-for-await';
import {
calculateProjectDependencies,
calculateProjectBuildableDependencies,
checkDependentProjectsHaveBeenBuilt,
createTmpTsConfig,
DependentBuildableProjectNode,
Expand Down Expand Up @@ -72,7 +72,8 @@ export function createLibraryExecutor(
context: ExecutorContext
) {
const { target, dependencies, topLevelDependencies } =
calculateProjectDependencies(
calculateProjectBuildableDependencies(
context.taskGraph,
context.projectGraph,
context.root,
context.projectName,
Expand Down
5 changes: 3 additions & 2 deletions packages/js/src/executors/node/node.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as path from 'path';
import { join } from 'path';

import { InspectType, NodeExecutorOptions } from './schema';
import { calculateProjectDependencies } from '../../utils/buildable-libs-utils';
import { calculateProjectBuildableDependencies } from '../../utils/buildable-libs-utils';
import { killTree } from './lib/kill-tree';
import { fileExists } from 'nx/src/utils/fileutils';
import { getMainFileDirRelativeToProjectRoot } from '../../utils/get-main-file-dir';
Expand Down Expand Up @@ -314,7 +314,8 @@ function calculateResolveMappings(
options: NodeExecutorOptions
) {
const parsed = parseTargetString(options.buildTarget, context.projectGraph);
const { dependencies } = calculateProjectDependencies(
const { dependencies } = calculateProjectBuildableDependencies(
context.taskGraph,
context.projectGraph,
context.root,
parsed.project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ExecutorContext } from '@nx/devkit';
import { parseTargetString } from '@nx/devkit';
import { join, relative } from 'path';
import { CopyAssetsHandler } from '../../../../utils/assets/copy-assets-handler';
import { calculateProjectDependencies } from '../../../../utils/buildable-libs-utils';
import { calculateProjectBuildableDependencies } from '../../../../utils/buildable-libs-utils';
import type { NormalizedExecutorOptions } from '../../../../utils/schema';
import { getTaskWithTscExecutorOptions } from '../get-task-options';
import type { TypescriptInMemoryTsConfig } from '../typescript-compilation';
Expand Down Expand Up @@ -97,7 +97,8 @@ function createTaskInfo(
const {
target: projectGraphNode,
dependencies: buildableProjectNodeDependencies,
} = calculateProjectDependencies(
} = calculateProjectBuildableDependencies(
context.taskGraph,
context.projectGraph,
context.root,
context.taskGraph.tasks[taskName].target.project,
Expand Down
Loading

1 comment on commit ca5e673

@vercel
Copy link

@vercel vercel bot commented on ca5e673 Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx.dev
nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.