Skip to content

Commit

Permalink
fix(js): locate npm nodes correctly for aliased packages
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Jul 25, 2024
1 parent dd84dab commit 1962ae5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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:[email protected]',
},
},
};

targetProjectLocator = new TargetProjectLocator(projects, npmProjects);
Expand Down Expand Up @@ -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)', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 1962ae5

Please sign in to comment.