From ce2c1d58c3f76be5c3ed18c52ced8ff165be3087 Mon Sep 17 00:00:00 2001 From: Federico Zivolo Date: Fri, 21 Jul 2017 18:46:14 +0200 Subject: [PATCH] feat: Added extensions option --- README.md | 4 +++- package.json | 2 +- src/index.js | 33 ++++++++++++++++++++------------- 3 files changed, 24 insertions(+), 15 deletions(-) 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..940cb30 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,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; }, }; }