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

fix(js): handle arbitrary nested ts path mappings when re-mapping them to the outputs #27429

Merged
merged 1 commit into from
Aug 16, 2024
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
12 changes: 10 additions & 2 deletions packages/js/src/utils/buildable-libs-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {

describe('updatePaths', () => {
const deps: DependentBuildableProjectNode[] = [
{ name: '@proj/lib', node: {} as any, outputs: ['dist/libs/lib'] },
{
name: '@proj/lib',
node: { data: { root: 'libs/lib' } } as any,
outputs: ['dist/libs/lib'],
},
];

it('should add path', () => {
Expand All @@ -30,7 +34,11 @@ describe('updatePaths', () => {
updatePaths(deps, paths);
expect(paths).toEqual({
'@proj/lib': ['dist/libs/lib'],
'@proj/lib/sub': ['dist/libs/lib/sub'],
'@proj/lib/sub': [
'dist/libs/lib/sub',
'dist/libs/lib/sub/src/index',
'dist/libs/lib/sub/src/index.ts',
],
});
});
});
Expand Down
36 changes: 24 additions & 12 deletions packages/js/src/utils/buildable-libs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { unlinkSync } from 'fs';
import { isNpmProject } from 'nx/src/project-graph/operators';
import { directoryExists, fileExists } from 'nx/src/utils/fileutils';
import { output } from 'nx/src/utils/output';
import { dirname, join, relative, isAbsolute } from 'path';
import { dirname, join, relative, isAbsolute, extname } from 'path';
import type * as ts from 'typescript';
import { readTsConfigPaths } from './typescript/ts-config';

Expand Down Expand Up @@ -521,6 +521,10 @@ export function updatePaths(
const pathsKeys = Object.keys(paths);
// For each registered dependency
dependencies.forEach((dep) => {
if (dep.node.type === 'npm') {
return;
}

// If there are outputs
if (dep.outputs && dep.outputs.length > 0) {
// Directly map the dependency name to the output paths (dist/packages/..., etc.)
Expand All @@ -535,21 +539,29 @@ export function updatePaths(
if (path.startsWith(nestedName)) {
const nestedPart = path.slice(nestedName.length);

// Bind secondary endpoints for ng-packagr projects
// Bind potential secondary endpoints for ng-packagr projects
let mappedPaths = dep.outputs.map(
(output) => `${output}/${nestedPart}`
);

// Get the dependency's package name
const { root } = (dep.node?.data || {}) as any;
if (root) {
// Update nested mappings to point to the dependency's output paths
mappedPaths = mappedPaths.concat(
paths[path].flatMap((path) =>
dep.outputs.map((output) => path.replace(root, output))
)
);
}
const { root } = dep.node.data;
// Update nested mappings to point to the dependency's output paths
mappedPaths = mappedPaths.concat(
paths[path].flatMap((p) =>
dep.outputs.flatMap((output) => {
const basePath = p.replace(root, output);
return [
// extension-less path to support compiled output
basePath.replace(
new RegExp(`${extname(basePath)}$`, 'gi'),
''
),
// original path with the root re-mapped to the output path
basePath,
];
})
)
);

paths[path] = mappedPaths;
}
Expand Down