Skip to content

Commit

Permalink
Add support for importing index.js from aliased directories path (dir…
Browse files Browse the repository at this point in the history
…ectory/index.js), fix rollup#37
  • Loading branch information
soanvig committed Jan 20, 2018
1 parent fc9a33e commit dbd7ca4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 145 deletions.
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default function alias(options = {}) {

// Resolve file names
const filePath = posix.resolve(directory, updatedId);

const match = resolve.map(ext => (endsWith(ext, filePath) ? filePath : `${filePath}${ext}`))
.find(exists);

Expand All @@ -81,6 +82,9 @@ export default function alias(options = {}) {
// with extension
} else if (endsWith('.js', filePath)) {
updatedId = filePath;
// See if filePath + /index.js exists, then use it
} else if (fs.existsSync(posix.join(filePath, 'index.js'))) {
updatedId = posix.join(filePath, 'index.js');
} else {
updatedId = filePath + '.js';
}
Expand Down
1 change: 1 addition & 0 deletions test/files/folder/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 123;
3 changes: 2 additions & 1 deletion test/files/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import fancyNumber from 'fancyNumber';
import anotherFancyNumber from './anotherFancyNumber';
import anotherNumber from './numberFolder/anotherNumber';
import moreNumbers from 'numberFolder/anotherNumber';
import index from './numberFolder';

export default fancyNumber + anotherFancyNumber + nonAliased + anotherNumber + moreNumbers;
export default fancyNumber + anotherFancyNumber + nonAliased + anotherNumber + moreNumbers + index;
15 changes: 13 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ test('Platform path.resolve(\'file-with.ext\') aliasing', t => {
t.is(resolved, path.resolve('./files/folder/hipster.jsx'));
});

test('index.js resolve', t => {
const result = alias({
test: path.resolve('./files/folder'),
});

const resolved = result.resolveId('test', posix.resolve(DIRNAME, './files/index.js'));

t.is(resolved, path.resolve('./files/folder/index.js'));
});

// Tests in Rollup
test(t =>
rollup({
Expand All @@ -179,7 +189,8 @@ test(t =>
t.is(stats.modules[1].id.endsWith(path.normalize('/files/aliasMe.js')), true);
t.is(stats.modules[2].id.endsWith(path.normalize('/files/localAliasMe.js')), true);
t.is(stats.modules[3].id.endsWith(path.normalize('/files/folder/anotherNumber.js')), true);
t.is(stats.modules[4].id.endsWith(path.normalize('/files/index.js')), true);
t.is(stats.modules.length, 5);
t.is(stats.modules[4].id.endsWith(path.normalize('/files/folder/index.js')), true);
t.is(stats.modules[5].id.endsWith(path.normalize('/files/index.js')), true);
t.is(stats.modules.length, 6);
})
);
Loading

0 comments on commit dbd7ca4

Please sign in to comment.