diff --git a/README.md b/README.md index 3688009..9c7a5ea 100644 --- a/README.md +++ b/README.md @@ -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'] + })], }); ``` diff --git a/package.json b/package.json index 117e5a5..5a986d3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.js b/src/index.js index 0bf8793..f7f843a 100644 --- a/src/index.js +++ b/src/index.js @@ -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) { @@ -15,22 +15,30 @@ 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 occurrence + options.extensions.find(extension => { + const dirIndexFile = path.join(directory + importee, `index${extension}`); - try { - stats = statSync(dirIndexFile); - } catch (e) { - return null; - } + // TODO: This should be asynchronous + let stats; - if (stats.isFile()) { - return dirIndexFile; - } + try { + stats = statSync(dirIndexFile); + } catch (e) { + return false; + } + + if (stats.isFile()) { + resolved = dirIndexFile; + return true; + } + + return false; + }); - return null; + return resolved; }, }; }