Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "fix(core): support subpath exports when constructing the project graph" #29762

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1011,52 +1011,6 @@ describe('TargetProjectLocator', () => {
expect(result).toEqual('npm:[email protected]');
});
});

describe('findDependencyInWorkspaceProjects', () => {
it.each`
pkgName | project | exports | dependency
${'@org/pkg1'} | ${'pkg1'} | ${undefined} | ${'@org/pkg1'}
${'@org/pkg1'} | ${'pkg1'} | ${undefined} | ${'@org/pkg1/subpath'}
${'@org/pkg1'} | ${'pkg1'} | ${'dist/index.js'} | ${'@org/pkg1'}
${'@org/pkg1'} | ${'pkg1'} | ${{}} | ${'@org/pkg1'}
${'@org/pkg1'} | ${'pkg1'} | ${{}} | ${'@org/pkg1/subpath'}
${'@org/pkg1'} | ${'pkg1'} | ${{ '.': 'dist/index.js' }} | ${'@org/pkg1'}
${'@org/pkg1'} | ${'pkg1'} | ${{ '.': 'dist/index.js' }} | ${'@org/pkg1/subpath'}
${'@org/pkg1'} | ${'pkg1'} | ${{ './subpath': './dist/subpath.js' }} | ${'@org/pkg1'}
${'@org/pkg1'} | ${'pkg1'} | ${{ './subpath': './dist/subpath.js' }} | ${'@org/pkg1/subpath'}
${'@org/pkg1'} | ${'pkg1'} | ${{ import: './dist/index.js', default: './dist/index.js' }} | ${'@org/pkg1'}
${'@org/pkg1'} | ${'pkg1'} | ${{ import: './dist/index.js', default: './dist/index.js' }} | ${'@org/pkg1/subpath'}
`(
'should find "$dependency" as "$project" when exports="$exports"',
({ pkgName, project, exports, dependency }) => {
let projects: Record<string, ProjectGraphProjectNode> = {
[project]: {
name: project,
type: 'lib' as const,
data: {
root: project,
metadata: {
js: {
packageName: pkgName,
packageExports: exports,
},
},
},
},
};

const targetProjectLocator = new TargetProjectLocator(
projects,
{},
new Map()
);
const result =
targetProjectLocator.findDependencyInWorkspaceProjects(dependency);

expect(result).toEqual(project);
}
);
});
});

describe('isBuiltinModuleImport()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,7 @@ export class TargetProjectLocator {
this.nodes
);

return (
this.packageEntryPointsToProjectMap[dep]?.name ??
// if the package exports do not include ".", look for subpath exports
Object.entries(this.packageEntryPointsToProjectMap).find(([entryPoint]) =>
dep.startsWith(`${entryPoint}/`)
)?.[1]?.name ??
null
);
return this.packageEntryPointsToProjectMap[dep]?.name ?? null;
}

private resolveImportWithTypescript(
Expand Down
19 changes: 2 additions & 17 deletions packages/nx/src/plugins/js/utils/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,13 @@ export function getPackageEntryPointsToProjectMap<
}

const { packageName, packageExports } = metadata.js;
if (
!packageExports ||
typeof packageExports === 'string' ||
!Object.keys(packageExports).length
) {
if (!packageExports || typeof packageExports === 'string') {
// no `exports` or it points to a file, which would be the equivalent of
// an '.' export, in which case the package name is the entry point
result[packageName] = project;
} else {
for (const entryPoint of Object.keys(packageExports)) {
// if entrypoint begins with '.', it is a relative subpath export
// otherwise, it is a conditional export
// https://nodejs.org/api/packages.html#conditional-exports
if (entryPoint.startsWith('.')) {
result[join(packageName, entryPoint)] = project;
} else {
result[packageName] = project;
}
}
// if there was no '.' entrypoint, ensure the package name is matched with the project
if (!result[packageName]) {
result[packageName] = project;
result[join(packageName, entryPoint)] = project;
}
}
}
Expand Down
Loading