fix(js): locate npm nodes correctly for aliased packages (#27124)

This commit is contained in:
Leosvel Pérez Espinosa 2024-08-07 19:29:27 +02:00 committed by GitHub
parent 0f193e21ce
commit 40d3516020
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 2 deletions

View File

@ -96,6 +96,14 @@ describe('TargetProjectLocator', () => {
name: '@proj/proj123-base',
version: '1.0.0',
}),
'./node_modules/lodash/package.json': JSON.stringify({
name: 'lodash',
version: '3.0.0',
}),
'./node_modules/lodash-4/package.json': JSON.stringify({
name: 'lodash',
version: '4.0.0',
}),
};
vol.fromJSON(fsJson, '/root');
projects = {
@ -263,6 +271,22 @@ describe('TargetProjectLocator', () => {
packageName: '@proj/proj123-base',
},
},
'npm:lodash': {
name: 'npm:lodash',
type: 'npm',
data: {
version: '3.0.0',
packageName: 'lodash',
},
},
'npm:lodash-4': {
name: 'npm:lodash-4',
type: 'npm',
data: {
packageName: 'lodash-4',
version: 'npm:lodash@4.0.0',
},
},
};
targetProjectLocator = new TargetProjectLocator(projects, npmProjects);
@ -454,6 +478,20 @@ describe('TargetProjectLocator', () => {
);
expect(proj5).toEqual('proj5');
});
it('should be able to resolve packages alises', () => {
const lodash = targetProjectLocator.findProjectFromImport(
'lodash',
'libs/proj/index.ts'
);
expect(lodash).toEqual('npm:lodash');
const lodash4 = targetProjectLocator.findProjectFromImport(
'lodash-4',
'libs/proj/index.ts'
);
expect(lodash4).toEqual('npm:lodash-4');
});
});
describe('findTargetProjectWithImport (without tsconfig.json)', () => {

View File

@ -188,9 +188,14 @@ export class TargetProjectLocator {
const version = clean(externalPackageJson.version);
const npmProjectKey = `npm:${externalPackageJson.name}@${version}`;
const matchingExternalNode = this.npmProjects[npmProjectKey];
let matchingExternalNode = this.npmProjects[npmProjectKey];
if (!matchingExternalNode) {
return null;
// check if it's a package alias, where the resolved package key is used as the version
const aliasNpmProjectKey = `npm:${packageName}@${npmProjectKey}`;
matchingExternalNode = this.npmProjects[aliasNpmProjectKey];
if (!matchingExternalNode) {
return null;
}
}
this.npmResolutionCache.set(