Skip to content

Commit 791a8ef

Browse files
leosvelperezFrozenPandaz
authored andcommitted
fix(js): handle arbitrary nested ts path mappings when re-mapping them to the outputs (#27429)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> When re-mapping a TS path mapping like `"@foo/lib1/plugins/some-file": ["packages/lib1/src/plugins/some-file.ts"]`, it results in: ```json "@foo/lib1/plugins/some-file": [ "dist/packages/lib1/plugins/some-file", "dist/packages/lib1/src/plugins/some-file.ts" ] ``` The first path is wrong because it's missing the `src` directory, and the second one has a file extension that's not in the output. ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> When re-mapping a TS path mapping like `"@foo/lib1/plugins/some-file": ["packages/lib1/src/plugins/some-file.ts"]`, it should result in: ```json "@foo/lib1/plugins/some-file": [ "dist/packages/lib1/plugins/some-file", "dist/packages/lib1/src/plugins/some-file", "dist/packages/lib1/src/plugins/some-file.ts" ] ``` In this case, the second path would correctly point to the output. It doesn't have an extension, which allows the compiler to pick up the correct one. Note that while the first and third paths are still not valid for this specific use case, they could still be valid for other use cases, and in any case, they're still kept for backward compatibility. The util to re-map these paths is currently very generic and generates potentially valid paths. The invalid paths for a given use case won't throw an error as long as there's one that's valid. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #21699 (cherry picked from commit 89f6ad4)
1 parent b621454 commit 791a8ef

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

packages/js/src/utils/buildable-libs-utils.spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88

99
describe('updatePaths', () => {
1010
const deps: DependentBuildableProjectNode[] = [
11-
{ name: '@proj/lib', node: {} as any, outputs: ['dist/libs/lib'] },
11+
{
12+
name: '@proj/lib',
13+
node: { data: { root: 'libs/lib' } } as any,
14+
outputs: ['dist/libs/lib'],
15+
},
1216
];
1317

1418
it('should add path', () => {
@@ -30,7 +34,11 @@ describe('updatePaths', () => {
3034
updatePaths(deps, paths);
3135
expect(paths).toEqual({
3236
'@proj/lib': ['dist/libs/lib'],
33-
'@proj/lib/sub': ['dist/libs/lib/sub'],
37+
'@proj/lib/sub': [
38+
'dist/libs/lib/sub',
39+
'dist/libs/lib/sub/src/index',
40+
'dist/libs/lib/sub/src/index.ts',
41+
],
3442
});
3543
});
3644
});

packages/js/src/utils/buildable-libs-utils.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { unlinkSync } from 'fs';
1616
import { isNpmProject } from 'nx/src/project-graph/operators';
1717
import { directoryExists, fileExists } from 'nx/src/utils/fileutils';
1818
import { output } from 'nx/src/utils/output';
19-
import { dirname, join, relative, isAbsolute } from 'path';
19+
import { dirname, join, relative, isAbsolute, extname } from 'path';
2020
import type * as ts from 'typescript';
2121
import { readTsConfigPaths } from './typescript/ts-config';
2222

@@ -521,6 +521,10 @@ export function updatePaths(
521521
const pathsKeys = Object.keys(paths);
522522
// For each registered dependency
523523
dependencies.forEach((dep) => {
524+
if (dep.node.type === 'npm') {
525+
return;
526+
}
527+
524528
// If there are outputs
525529
if (dep.outputs && dep.outputs.length > 0) {
526530
// Directly map the dependency name to the output paths (dist/packages/..., etc.)
@@ -535,21 +539,29 @@ export function updatePaths(
535539
if (path.startsWith(nestedName)) {
536540
const nestedPart = path.slice(nestedName.length);
537541

538-
// Bind secondary endpoints for ng-packagr projects
542+
// Bind potential secondary endpoints for ng-packagr projects
539543
let mappedPaths = dep.outputs.map(
540544
(output) => `${output}/${nestedPart}`
541545
);
542546

543-
// Get the dependency's package name
544-
const { root } = (dep.node?.data || {}) as any;
545-
if (root) {
546-
// Update nested mappings to point to the dependency's output paths
547-
mappedPaths = mappedPaths.concat(
548-
paths[path].flatMap((path) =>
549-
dep.outputs.map((output) => path.replace(root, output))
550-
)
551-
);
552-
}
547+
const { root } = dep.node.data;
548+
// Update nested mappings to point to the dependency's output paths
549+
mappedPaths = mappedPaths.concat(
550+
paths[path].flatMap((p) =>
551+
dep.outputs.flatMap((output) => {
552+
const basePath = p.replace(root, output);
553+
return [
554+
// extension-less path to support compiled output
555+
basePath.replace(
556+
new RegExp(`${extname(basePath)}$`, 'gi'),
557+
''
558+
),
559+
// original path with the root re-mapped to the output path
560+
basePath,
561+
];
562+
})
563+
)
564+
);
553565

554566
paths[path] = mappedPaths;
555567
}

0 commit comments

Comments
 (0)