UNPKG

base-loader

Version:

Base loader for Template-based applications.

71 lines (60 loc) 1.52 kB
/*! * base-loader <https://github.com/jonschlinkert/base-loader> * * Copyright (c) 2015, Jon Schlinkert. * Licensed under the MIT License. */ 'use strict'; var path = require('path'); var relative = require('relative'); var files = require('map-files'); module.exports = function base_(args) { args = arrayify(args); var first = args[1]; var isOptions = typeof first === 'object'; if (isOptions) { first.renameKey = first.renameKey || renameKey; } if (isOptions && first.hasOwnProperty('content')) { return normalize(args); } return files.apply(files, args); }; function normalize(args) { var fp = args[0]; var file = args[1]; var name = basename(fp); file.path = fp; var res = {}; res[name] = file; return res; } /** * Cast `val` to an array. */ function arrayify(val) { var isArray = Array.isArray(val); if (typeof val !== 'string' && !isArray) { throw new Error('base-loader: arrayify() expects a string or array.'); } return isArray ? val : [val]; } /** * Strip a file extension from a filepath. */ function stripExtension(fp) { if (typeof fp !== 'string') { throw new Error('base-loader: stripExtension() expects a string.'); } return fp.substr(0, fp.length - path.extname(fp).length); } /** * Default `renameKey` function. */ function renameKey(fp, acc, opts) { if (typeof fp !== 'string') { throw new Error('base-loader: renameKey() expects a string.'); } fp = relative.toBase(opts.cwd, fp); return stripExtension(fp); }