Created
January 1, 2019 02:00
-
-
Save goldhand/b7034b280cecc368d1e0acdeaa808a2b to your computer and use it in GitHub Desktop.
Resolves a module given multiple locations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as R from 'ramda'; | |
const resolveModuleFromLocation = R.curry((location, next) => { | |
try { | |
return require.resolve(location) && require(location); | |
} catch (err) { | |
if (next && err && err.code === 'MODULE_NOT_FOUND') { | |
return next(); | |
} | |
} | |
}); | |
const resolveModuleFromLocations = R.pipe( | |
R.map(resolveModuleFromLocation), | |
R.apply(R.pipe), | |
); | |
export default (locations = [], notFound = () => false) => { | |
const result = resolveModuleFromLocations(locations); | |
if (result) return result; | |
return notFound(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment