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 27, 2023
1 parent 836cd34 commit cd6a5fa
Show file tree
Hide file tree
Showing 4 changed files with 421 additions and 59 deletions.
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
54 changes: 43 additions & 11 deletions packages/js/src/utils/buildable-libs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,48 @@ 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, target: string): string {
let buildTargetName: string | undefined = undefined;
// TODO(katerina): Remove @nrwl/* for Nx 17
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;
}
}
if (!buildTargetName) {
return node.data.targets &&
node.data.targets[target] &&
node.data.targets[target].executor !== ''
? target
: undefined;
}
return buildTargetName;
}

export type DependentBuildableProjectNode = {
Expand Down Expand Up @@ -73,7 +104,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, targetName);
if (dependencyTargetName) {
const libPackageJsonPath = join(
root,
depNode.data.root,
Expand All @@ -89,7 +121,7 @@ export function calculateProjectDependencies(
overrides: {},
target: {
project: projectName,
target: targetName,
target: dependencyTargetName,
configuration: configurationName,
},
},
Expand Down
Loading

0 comments on commit cd6a5fa

Please sign in to comment.