Skip to content

Commit

Permalink
feat: Added extensions option
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Zivolo committed Jul 21, 2017
1 parent b9e1a7f commit ce2c1d5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import localResolve from 'rollup-plugin-local-resolve';
// This will resolve `./files` to `./files/index.js` if the file exists
rollup({
entry: './files',
plugins: [localResolve()],
plugins: [localResolve({
extensions: ['.jsx', '.js'] // default ['.js']
})],
});
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rollup-plugin-local-resolve",
"version": "1.0.7",
"version": "1.1.0",
"description": "Resolves index.js files with Rollup",
"main": "dist/rollup-plugin-local-resolve.js",
"jsnext:main": "dist/rollup-plugin-local-resolve.es2015.js",
Expand Down
33 changes: 20 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { statSync } from 'fs';
import path from 'path';

export default function localResolver() {
export default function localResolver(options = { extensions: ['.js'] }) {
return {
resolveId(importee, importer) {
if (importee.indexOf('./') === -1) {
Expand All @@ -15,22 +15,29 @@ export default function localResolver() {
const basename = path.basename(importer);
const directory = importer.split(basename)[0];

const dirIndexFile = path.join(directory + importee, 'index.js');
let resolved = null;

// TODO: This should be asynchronous
let stats;
// find will stop at the first occurrency
options.extensions.find(extension => {

try {
stats = statSync(dirIndexFile);
} catch (e) {
return null;
}
const dirIndexFile = path.join(directory + importee, `index${extension}`);

if (stats.isFile()) {
return dirIndexFile;
}
// TODO: This should be asynchronous
let stats;

try {
stats = statSync(dirIndexFile);
} catch (e) {
return false;
}

if (stats.isFile()) {
resolved = dirIndexFile;
return true;
}
});

return null;
return resolved;
},
};
}

0 comments on commit ce2c1d5

Please sign in to comment.