Skip to content

Commit ab41806

Browse files
author
Ives van Hoorne
committed
Allow resolving of versioned dependencies
1 parent 22912cc commit ab41806

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

src/sandbox/eval/manager.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { Module } from './entities/module';
88
import TranspiledModule from './transpiled-module';
99
import Preset from './presets';
1010
import fetchModule from './npm/fetch-npm-module';
11+
import getDependencyName from './utils/get-dependency-name';
1112
import DependencyNotFoundError from '../errors/dependency-not-found-error';
1213
import ModuleNotFoundError from '../errors/module-not-found-error';
1314

@@ -214,17 +215,8 @@ export default class Manager {
214215
return path;
215216
}
216217

217-
const dependencyParts = path.split('/');
218-
const dependencyName = path.startsWith('@')
219-
? `${dependencyParts[0]}/${dependencyParts[1]}`
220-
: dependencyParts[0];
221-
222-
const previousDependencyParts = currentPath
223-
.replace('/node_modules/', '')
224-
.split('/');
225-
const previousDependencyName = path.startsWith('@')
226-
? `${previousDependencyParts[0]}/${previousDependencyParts[1]}`
227-
: previousDependencyParts[0];
218+
const dependencyName = getDependencyName(path);
219+
const previousDependencyName = getDependencyName(currentPath);
228220

229221
if (
230222
this.manifest.dependencyAliases[previousDependencyName] &&
@@ -280,10 +272,7 @@ export default class Manager {
280272
throw new ModuleNotFoundError(aliasedPath, false);
281273
}
282274

283-
const dependencyParts = path.split('/');
284-
const dependencyName = path.startsWith('@')
285-
? `${dependencyParts[0]}/${dependencyParts[1]}`
286-
: dependencyParts[0];
275+
const dependencyName = getDependencyName(path);
287276

288277
if (
289278
this.manifest.dependencies.find(d => d.name === dependencyName) ||
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default function getDependencyName(path: string) {
2+
const dependencyParts = path.split('/');
3+
let dependencyName = dependencyParts.shift();
4+
5+
if (path.startsWith('@')) {
6+
dependencyName += `/${dependencyParts.shift()}`;
7+
}
8+
if (dependencyParts[0] && /^\d/.test(dependencyParts[0])) {
9+
// Make sure to include the aliased version if it's part of it
10+
dependencyName += `/${dependencyParts.shift()}`;
11+
}
12+
13+
return dependencyName;
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import getDependencyName from './get-dependency-name';
2+
3+
describe('getDependencyName', () => {
4+
it('can find a simple dependency name', () => {
5+
expect(getDependencyName('lodash/test')).toBe('lodash');
6+
});
7+
8+
it('can find a simple dependency name from no path', () => {
9+
expect(getDependencyName('lodash')).toBe('lodash');
10+
});
11+
12+
it('can find a simple dependency name from an organization', () => {
13+
expect(getDependencyName('@angular/core/test')).toBe('@angular/core');
14+
});
15+
16+
it('can find a simple dependency name with a version', () => {
17+
expect(getDependencyName('lodash/4.4.3/test')).toBe('lodash/4.4.3');
18+
});
19+
20+
it('can find a simple dependency name with a version from an organization', () => {
21+
expect(getDependencyName('@angular/core/4.4.3/test')).toBe(
22+
'@angular/core/4.4.3'
23+
);
24+
});
25+
});

0 commit comments

Comments
 (0)