Skip to content

Commit

Permalink
fix(js): locate npm nodes correctly for aliased packages (#27124)
Browse files Browse the repository at this point in the history
(cherry picked from commit 40d3516)
  • Loading branch information
leosvelperez authored and FrozenPandaz committed Aug 7, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 56228d9 commit 644bd4d
Showing 2 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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)', () => {
Original file line number Diff line number Diff line change
@@ -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(

0 comments on commit 644bd4d

Please sign in to comment.