From ca3fd3cb65b832cc5ca8da1c1d2e3152a5a3ebe8 Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Thu, 7 Sep 2023 16:45:09 -0500 Subject: [PATCH] fix(js): esbuild should not throw when a project depends on non-js dependencies (#19057) --- .../utils/package-json/update-package-json.ts | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/packages/js/src/utils/package-json/update-package-json.ts b/packages/js/src/utils/package-json/update-package-json.ts index bf87cdfff1286..74cd9cb0f2fe1 100644 --- a/packages/js/src/utils/package-json/update-package-json.ts +++ b/packages/js/src/utils/package-json/update-package-json.ts @@ -12,20 +12,20 @@ import { getOutputsForTargetAndConfiguration, joinPathFragments, ProjectFileMap, + ProjectGraph, + ProjectGraphExternalNode, ProjectGraphProjectNode, readJsonFile, workspaceRoot, writeJsonFile, } from '@nx/devkit'; import { DependentBuildableProjectNode } from '../buildable-libs-utils'; -import { basename, join, parse, relative } from 'path'; +import { basename, join, parse } from 'path'; import { writeFileSync } from 'fs-extra'; -import { isNpmProject } from 'nx/src/project-graph/operators'; import { fileExists } from 'nx/src/utils/fileutils'; import type { PackageJson } from 'nx/src/utils/package-json'; import { existsSync } from 'fs'; import { readProjectFileMapCache } from 'nx/src/project-graph/nx-deps-cache'; -import * as fastGlob from 'fast-glob'; import { getRelativeDirectoryToProjectRoot } from '../get-main-file-dir'; @@ -117,9 +117,29 @@ export function updatePackageJson( } } +function isNpmNode( + node: ProjectGraphProjectNode | ProjectGraphExternalNode, + graph: ProjectGraph +): node is ProjectGraphExternalNode { + return !!(graph.externalNodes[node.name]?.type === 'npm'); +} + +function isWorkspaceProject( + node: ProjectGraphProjectNode | ProjectGraphExternalNode, + graph: ProjectGraph +): node is ProjectGraphProjectNode { + return !!graph.nodes[node.name]; +} + function addMissingDependencies( packageJson: PackageJson, - { projectName, targetName, configurationName, root }: ExecutorContext, + { + projectName, + targetName, + configurationName, + root, + projectGraph, + }: ExecutorContext, dependencies: DependentBuildableProjectNode[], propType: 'dependencies' | 'peerDependencies' = 'dependencies' ) { @@ -127,7 +147,7 @@ function addMissingDependencies( joinPathFragments(workspaceRoot, 'package.json') ); dependencies.forEach((entry) => { - if (isNpmProject(entry.node)) { + if (isNpmNode(entry.node, projectGraph)) { const { packageName, version } = entry.node.data; if ( packageJson.dependencies?.[packageName] || @@ -142,7 +162,7 @@ function addMissingDependencies( packageJson[propType] ??= {}; packageJson[propType][packageName] = version; - } else { + } else if (isWorkspaceProject(entry.node, projectGraph)) { const packageName = entry.name; if (!!workspacePackageJson.devDependencies?.[packageName]) { return;