Skip to content

Commit ea0bd01

Browse files
yyx990803frostney
authored andcommitted
support absolute alias + fix extension appending (rollup#2)
1 parent 4e6c1f3 commit ea0bd01

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import fs from 'fs';
44
// Helper functions
55
const noop = () => null;
66
const startsWith = (needle, haystack) => ! haystack.indexOf(needle);
7+
const endsWith = (needle, haystack) => haystack.slice(-needle.length) === needle;
8+
const isFilePath = id => /^\.?\//.test(id);
79
const exists = uri => {
810
try {
911
return fs.statSync(uri).isFile();
@@ -38,7 +40,7 @@ export default function alias(options = {}) {
3840

3941
const updatedId = importee.replace(toReplace, entry);
4042

41-
if (startsWith('./', updatedId)) {
43+
if (isFilePath(updatedId)) {
4244
const directory = path.dirname(importer);
4345

4446
// Resolve file names
@@ -52,6 +54,10 @@ export default function alias(options = {}) {
5254

5355
// To keep the previous behaviour we simply return the file path
5456
// with extension
57+
if (endsWith('.js', filePath)) {
58+
return filePath;
59+
}
60+
5561
return filePath + '.js';
5662
}
5763

test/index.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,32 @@ test(t => {
4141
});
4242

4343
const resolved = result.resolveId('foo', '/src/importer.js');
44-
const resolved2 = result.resolveId('pony', '/src/highly/nested/importer.js');
44+
const resolved2 = result.resolveId('foo/baz', '/src/importer.js');
45+
const resolved3 = result.resolveId('foo/baz.js', '/src/importer.js');
46+
const resolved4 = result.resolveId('pony', '/src/highly/nested/importer.js');
4547

4648
t.is(resolved, '/src/bar.js');
47-
t.is(resolved2, '/src/highly/nested/par/a/di/se.js');
49+
t.is(resolved2, '/src/bar/baz.js');
50+
t.is(resolved3, '/src/bar/baz.js');
51+
t.is(resolved4, '/src/highly/nested/par/a/di/se.js');
52+
});
53+
54+
// Absolute local aliasing
55+
test(t => {
56+
const result = alias({
57+
foo: '/bar',
58+
pony: '/par/a/di/se.js',
59+
});
60+
61+
const resolved = result.resolveId('foo', '/src/importer.js');
62+
const resolved2 = result.resolveId('foo/baz', '/src/importer.js');
63+
const resolved3 = result.resolveId('foo/baz.js', '/src/importer.js');
64+
const resolved4 = result.resolveId('pony', '/src/highly/nested/importer.js');
65+
66+
t.is(resolved, '/bar.js');
67+
t.is(resolved2, '/bar/baz.js');
68+
t.is(resolved3, '/bar/baz.js');
69+
t.is(resolved4, '/par/a/di/se.js');
4870
});
4971

5072
// Test for the resolve property

0 commit comments

Comments
 (0)