Skip to content

Commit

Permalink
fix(js): properly identify buildable project
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Jun 26, 2023
1 parent b736992 commit 0c97247
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 77 deletions.
1 change: 0 additions & 1 deletion packages/angular/src/builders/utilities/buildable-libs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export function createTmpTsConfigForBuildableLibs(
options?.projectGraph ?? readCachedProjectGraph(),
context.workspaceRoot,
context.target.project,
options?.target ?? context.target.target,
context.target.configuration
);
dependencies = result.dependencies;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export async function* delegateBuildExecutor(
context.projectGraph,
context.root,
context.projectName,
context.targetName,
context.configurationName
);

Expand Down
1 change: 0 additions & 1 deletion packages/angular/src/executors/package/package.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export function createLibraryExecutor(
context.projectGraph,
context.root,
context.projectName,
context.targetName,
context.configurationName
);
if (
Expand Down
1 change: 0 additions & 1 deletion packages/js/src/executors/node/node.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ function calculateResolveMappings(
context.projectGraph,
context.root,
parsed.project,
parsed.target,
parsed.configuration
);
return dependencies.reduce((m, c) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ function createTaskInfo(
context.projectGraph,
context.root,
context.taskGraph.tasks[taskName].target.project,
context.taskGraph.tasks[taskName].target.target,
context.taskGraph.tasks[taskName].target.configuration
);

Expand Down
159 changes: 119 additions & 40 deletions packages/js/src/utils/buildable-libs-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,80 +83,159 @@ describe('calculateProjectDependencies', () => {
});
});

it('should include npm packages in dependency list and sort them correctly', async () => {
it('should identify buildable libraries correctly', () => {
const graph: ProjectGraph = {
nodes: {
example: {
somejslib1: {
name: 'somejslib1',
type: 'lib',
name: 'example',
data: {
root: '/root/example',
},
},
},
externalNodes: {
'npm:some-lib': {
type: 'npm',
name: 'npm:some-lib',
data: {
packageName: 'some-lib',
version: '0.0.0',
name: 'somejslib1',
sourceRoot: 'libs/somejslib1/src',
projectType: 'library',
targets: {
'my-custom-build-target': {
executor: '@nx/js:tsc',
outputs: ['{options.outputPath}'],
options: {
outputPath: 'dist/libs/somejslib1',
main: 'libs/somejslib1/src/index.ts',
},
},
},
tags: [],
root: 'libs/somejslib1',
implicitDependencies: [],
},
},
'npm:formik': {
type: 'npm',
name: 'npm:formik',
somejslib2: {
name: 'somejslib2',
type: 'lib',
data: {
packageName: 'formik',
version: '0.0.0',
},
},
'npm:@prefixed-lib': {
type: 'npm',
name: 'npm:@prefixed-lib',
data: {
packageName: '@prefixed-lib',
version: '0.0.0',
name: 'somejslib2',
sourceRoot: 'libs/somejslib2/src',
projectType: 'library',
targets: {
'my-other-build-target': {
executor: '@nx/js:swc',
outputs: ['{options.outputPath}'],
options: {
outputPath: 'dist/libs/somejslib2',
main: 'libs/somejslib2/src/index.ts',
},
},
},
tags: [],
root: 'libs/somejslib2',
implicitDependencies: [],
},
},
},
externalNodes: {},
dependencies: {
example: [
{
source: 'example',
target: 'npm:some-lib',
type: DependencyType.static,
},
],
somejslib1: [
{
source: 'example',
target: 'npm:formik',
type: DependencyType.static,
},
{
source: 'example',
target: 'npm:@prefixed-lib',
source: 'somejslib2',
target: 'somejslib2',
type: DependencyType.static,
},
],
},
};

const results = await calculateProjectDependencies(
const results = calculateProjectDependencies(
graph,
'root',
'example',
'somejslib1',
'build',
undefined
);
expect(results).toMatchObject({
target: {
name: 'somejslib1',
type: 'lib',
name: 'example',
data: {
name: 'somejslib1',
sourceRoot: 'libs/somejslib1/src',
projectType: 'library',
targets: {
'my-custom-build-target': {
executor: '@nx/js:tsc',
outputs: ['{options.outputPath}'],
options: {
outputPath: 'dist/libs/somejslib1',
main: 'libs/somejslib1/src/index.ts',
},
},
},
tags: [],
root: 'libs/somejslib1',
implicitDependencies: [],
},
},
dependencies: [
{ name: '@prefixed-lib' },
{ name: 'formik' },
{ name: 'some-lib' },
{
name: 'somejslib2',
outputs: ['dist/libs/somejslib2'],
node: {
name: 'somejslib2',
type: 'lib',
data: {
name: 'somejslib2',
sourceRoot: 'libs/somejslib2/src',
projectType: 'library',
targets: {
'my-other-build-target': {
executor: '@nx/js:swc',
outputs: ['{options.outputPath}'],
options: {
outputPath: 'dist/libs/somejslib2',
main: 'libs/somejslib2/src/index.ts',
},
},
},
tags: [],
root: 'libs/somejslib2',
implicitDependencies: [],
},
},
},
],
nonBuildableDependencies: [],
topLevelDependencies: [
{
name: 'somejslib2',
outputs: ['dist/libs/somejslib2'],
node: {
name: 'somejslib2',
type: 'lib',
data: {
name: 'somejslib2',
sourceRoot: 'libs/somejslib2/src',
projectType: 'library',
targets: {
'my-other-build-target': {
executor: '@nx/js:swc',
outputs: ['{options.outputPath}'],
options: {
outputPath: 'dist/libs/somejslib2',
main: 'libs/somejslib2/src/index.ts',
},
},
},
tags: [],
root: 'libs/somejslib2',
implicitDependencies: [],
},
},
},
],
});
});
Expand Down
47 changes: 35 additions & 12 deletions packages/js/src/utils/buildable-libs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,40 @@ import type * as ts from 'typescript';
import { unlinkSync } from 'fs';
import { output } from 'nx/src/utils/output';
import { isNpmProject } from 'nx/src/project-graph/operators';
import { ensureTypescript } from './typescript/ensure-typescript';
import { readTsConfigPaths } from './typescript/ts-config';

let tsModule: typeof import('typescript');

function isBuildable(target: string, node: ProjectGraphProjectNode): boolean {
return (
node.data.targets &&
node.data.targets[target] &&
node.data.targets[target].executor !== ''
);
function isBuildable(node: ProjectGraphProjectNode): string {
let buildTargetName: string | undefined = undefined;
const listOfBuildExecutors = [
'@nx/js:swc',
'@nx/js:tsc',
'@nx/rollup:rollup',
'@nx/rspack:rspack',
'@nx/vite:build',
'@nx/esbuild:esbuild',
'@nx/webpack:webpack',
'@nx/angular:ng-packagr-lite',
'@nx/angular:package',
'@nx/next:build',
'@nrwl/js:swc',
'@nrwl/js:tsc',
'@nrwl/rollup:rollup',
'@nrwl/rspack:rspack',
'@nrwl/vite:build',
'@nrwl/esbuild:esbuild',
'@nrwl/webpack:webpack',
'@nrwl/angular:ng-packagr-lite',
'@nrwl/angular:package',
'@nrwl/next:build',
];
for (const targetName of Object.keys(node.data.targets ?? {})) {
const target = node.data.targets[targetName];
if (listOfBuildExecutors.includes(target.executor)) {
buildTargetName = targetName;
break;
}
}
return buildTargetName;
}

export type DependentBuildableProjectNode = {
Expand All @@ -35,7 +58,6 @@ export function calculateProjectDependencies(
projGraph: ProjectGraph,
root: string,
projectName: string,
targetName: string,
configurationName: string,
shallow?: boolean
): {
Expand Down Expand Up @@ -73,7 +95,8 @@ export function calculateProjectDependencies(
let project: DependentBuildableProjectNode = null;
const depNode = projGraph.nodes[dep] || projGraph.externalNodes[dep];
if (depNode.type === 'lib') {
if (isBuildable(targetName, depNode)) {
const dependencyTargetName = isBuildable(depNode);
if (dependencyTargetName) {
const libPackageJsonPath = join(
root,
depNode.data.root,
Expand All @@ -89,7 +112,7 @@ export function calculateProjectDependencies(
overrides: {},
target: {
project: projectName,
target: targetName,
target: dependencyTargetName,
configuration: configurationName,
},
},
Expand Down
1 change: 0 additions & 1 deletion packages/js/src/utils/check-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export function checkDependencies(
context.projectGraph,
context.root,
context.projectName,
context.targetName,
context.configurationName
);
const projectRoot = target.data.root;
Expand Down
1 change: 0 additions & 1 deletion packages/next/plugins/with-nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ function withNx(
graph,
workspaceRoot,
project,
targetName,
configurationName
);
dependencies = result.dependencies;
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/executors/export/export.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export default async function exportExecutor(
context.projectGraph,
context.root,
context.projectName,
'build', // this should be generalized
context.configurationName
);
dependencies = result.dependencies;
Expand Down
1 change: 0 additions & 1 deletion packages/rollup/src/executors/rollup/rollup.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export async function* rollupExecutor(
context.projectGraph,
context.root,
context.projectName,
context.targetName,
context.configurationName,
true
);
Expand Down
1 change: 0 additions & 1 deletion packages/vite/src/utils/executor-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export function registerPaths(
context.projectGraph,
context.root,
context.projectName,
context.targetName,
context.configurationName
);
const tmpTsConfig = createTmpTsConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export async function* devServerExecutor(
context.projectGraph,
context.root,
context.projectName,
'build', // should be generalized
context.configurationName
);
buildOptions.tsConfig = createTmpTsConfig(
Expand Down
1 change: 0 additions & 1 deletion packages/webpack/src/executors/webpack/webpack.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ export async function* webpackExecutor(
context.projectGraph,
context.root,
context.projectName,
context.targetName,
context.configurationName
);
options.tsConfig = createTmpTsConfig(
Expand Down
Loading

0 comments on commit 0c97247

Please sign in to comment.