Skip to content

Instantly share code, notes, and snippets.

@goldhand
Created January 1, 2019 02:00
Show Gist options
  • Save goldhand/b7034b280cecc368d1e0acdeaa808a2b to your computer and use it in GitHub Desktop.
Save goldhand/b7034b280cecc368d1e0acdeaa808a2b to your computer and use it in GitHub Desktop.
Resolves a module given multiple locations.
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