Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
support absolute alias + fix extension appending (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 authored and frostney committed Apr 23, 2016
1 parent 4e6c1f3 commit ea0bd01
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import fs from 'fs';
// Helper functions
const noop = () => null;
const startsWith = (needle, haystack) => ! haystack.indexOf(needle);
const endsWith = (needle, haystack) => haystack.slice(-needle.length) === needle;
const isFilePath = id => /^\.?\//.test(id);
const exists = uri => {
try {
return fs.statSync(uri).isFile();
Expand Down Expand Up @@ -38,7 +40,7 @@ export default function alias(options = {}) {

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

if (startsWith('./', updatedId)) {
if (isFilePath(updatedId)) {
const directory = path.dirname(importer);

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

// To keep the previous behaviour we simply return the file path
// with extension
if (endsWith('.js', filePath)) {
return filePath;
}

return filePath + '.js';
}

Expand Down
26 changes: 24 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,32 @@ test(t => {
});

const resolved = result.resolveId('foo', '/src/importer.js');
const resolved2 = result.resolveId('pony', '/src/highly/nested/importer.js');
const resolved2 = result.resolveId('foo/baz', '/src/importer.js');
const resolved3 = result.resolveId('foo/baz.js', '/src/importer.js');
const resolved4 = result.resolveId('pony', '/src/highly/nested/importer.js');

t.is(resolved, '/src/bar.js');
t.is(resolved2, '/src/highly/nested/par/a/di/se.js');
t.is(resolved2, '/src/bar/baz.js');
t.is(resolved3, '/src/bar/baz.js');
t.is(resolved4, '/src/highly/nested/par/a/di/se.js');
});

// Absolute local aliasing
test(t => {
const result = alias({
foo: '/bar',
pony: '/par/a/di/se.js',
});

const resolved = result.resolveId('foo', '/src/importer.js');
const resolved2 = result.resolveId('foo/baz', '/src/importer.js');
const resolved3 = result.resolveId('foo/baz.js', '/src/importer.js');
const resolved4 = result.resolveId('pony', '/src/highly/nested/importer.js');

t.is(resolved, '/bar.js');
t.is(resolved2, '/bar/baz.js');
t.is(resolved3, '/bar/baz.js');
t.is(resolved4, '/par/a/di/se.js');
});

// Test for the resolve property
Expand Down

0 comments on commit ea0bd01

Please sign in to comment.