Skip to content

Commit

Permalink
fix(js): handle arbitrary nested ts path mappings when re-mapping the…
Browse files Browse the repository at this point in the history
…m 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
  • Loading branch information
leosvelperez authored Aug 16, 2024
1 parent 396a5de commit 89f6ad4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
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

0 comments on commit 89f6ad4

Please sign in to comment.