Skip to content

Commit 3be9ca3

Browse files
author
Ives van Hoorne
committed
Fix aliasing
1 parent 6874bb0 commit 3be9ca3

File tree

2 files changed

+82
-6
lines changed

2 files changed

+82
-6
lines changed

src/sandbox/eval/presets/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ export default class Preset {
5454
getAliasedPath(path: string): string {
5555
const aliases = Object.keys(this.alias);
5656

57-
// Find matching aliases
58-
const matchingAliases = aliases.filter(a => path.startsWith(a));
59-
const orderedAliases = orderBy(matchingAliases, alias => alias.length, [
60-
'desc',
61-
]);
57+
const pathParts = path.split('/'); // eslint-disable-line prefer-const
6258

63-
const foundAlias = orderedAliases[0];
59+
// Find matching aliases
60+
const foundAlias = orderBy(aliases, a => -a.split('/').length).find(a => {
61+
const parts = a.split('/');
62+
return parts.every((p, i) => pathParts[i] === p);
63+
});
6464

6565
if (foundAlias) {
6666
// if an alias is found we will replace the path with the alias
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import Preset from './';
2+
3+
describe('sandbox', () => {
4+
describe('preset', () => {
5+
describe('alias', () => {
6+
function createPreset(aliases) {
7+
return new Preset('test', [], aliases);
8+
}
9+
10+
it('finds the right simple alias', () => {
11+
const preset = createPreset({
12+
test: 'test2',
13+
});
14+
15+
expect(preset.getAliasedPath('test')).toBe('test2');
16+
});
17+
18+
it('chooses the right simple alias', () => {
19+
const preset = createPreset({
20+
test: 'test2',
21+
testtest: 'test4',
22+
});
23+
24+
expect(preset.getAliasedPath('testtest')).toBe('test4');
25+
});
26+
27+
it('works with paths', () => {
28+
const preset = createPreset({
29+
test: 'test2',
30+
testtest: 'test4',
31+
});
32+
33+
expect(preset.getAliasedPath('test/piano/guitar')).toBe(
34+
'test2/piano/guitar'
35+
);
36+
});
37+
38+
it('works with deeper paths', () => {
39+
const preset = createPreset({
40+
test: 'test4',
41+
'test/piano': 'test2',
42+
});
43+
44+
expect(preset.getAliasedPath('test/piano/guitar')).toBe('test2/guitar');
45+
});
46+
47+
it('works in a real life scenario', () => {
48+
const preset = createPreset({
49+
preact$: 'preact',
50+
// preact-compat aliases for supporting React dependencies:
51+
react: 'preact-compat',
52+
'react-dom': 'preact-compat',
53+
'create-react-class': 'preact-compat/lib/create-react-class',
54+
'react-addons-css-transition-group': 'preact-css-transition-group',
55+
});
56+
57+
expect(preset.getAliasedPath('react/render')).toBe(
58+
'preact-compat/render'
59+
);
60+
});
61+
62+
it("doesn't replace partial paths", () => {
63+
const preset = createPreset({
64+
preact$: 'preact',
65+
// preact-compat aliases for supporting React dependencies:
66+
react: 'preact-compat',
67+
'react-dom': 'preact-compat',
68+
'create-react-class': 'preact-compat/lib/create-react-class',
69+
'react-addons-css-transition-group': 'preact-css-transition-group',
70+
});
71+
72+
expect(preset.getAliasedPath('react-foo')).toBe('react-foo');
73+
});
74+
});
75+
});
76+
});

0 commit comments

Comments
 (0)