Skip to content

Commit

Permalink
fix(js): esbuild should not throw when a project depends on non-js de…
Browse files Browse the repository at this point in the history
…pendencies
  • Loading branch information
AgentEnder committed Sep 7, 2023
1 parent 2bc7031 commit 75659e5
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions packages/js/src/utils/package-json/update-package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -117,17 +117,37 @@ 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'
) {
const workspacePackageJson = readJsonFile(
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] ||
Expand All @@ -142,7 +162,7 @@ function addMissingDependencies(

packageJson[propType] ??= {};
packageJson[propType][packageName] = version;
} else {
} else if (isWorkspaceProject(entry.node, projectGraph)) {
const packageName = entry.name;
if (
!packageJson.dependencies?.[packageName] &&
Expand Down

0 comments on commit 75659e5

Please sign in to comment.