UNPKG

webpack-externalize-lodash-fp

Version:

Externalizes lodash from you webpack bundle. Handles all import cases.

342 lines (340 loc) 8.38 kB
const mapLowerCaseToCamelCase = { templatesettings: "templateSettings", after: "after", ary: "ary", assign: "assign", assignin: "assignIn", assigninwith: "assignInWith", assignwith: "assignWith", at: "at", before: "before", bind: "bind", bindall: "bindAll", bindkey: "bindKey", castarray: "castArray", chain: "chain", chunk: "chunk", compact: "compact", concat: "concat", cond: "cond", conforms: "conforms", constant: "constant", countby: "countBy", create: "create", curry: "curry", curryright: "curryRight", debounce: "debounce", defaults: "defaults", defaultsdeep: "defaultsDeep", defer: "defer", delay: "delay", difference: "difference", differenceby: "differenceBy", differencewith: "differenceWith", drop: "drop", dropright: "dropRight", droprightwhile: "dropRightWhile", dropwhile: "dropWhile", fill: "fill", filter: "filter", flatmap: "flatMap", flatmapdeep: "flatMapDeep", flatmapdepth: "flatMapDepth", flatten: "flatten", flattendeep: "flattenDeep", flattendepth: "flattenDepth", flip: "flip", flow: "flow", flowright: "flowRight", frompairs: "fromPairs", functions: "functions", functionsin: "functionsIn", groupby: "groupBy", initial: "initial", intersection: "intersection", intersectionby: "intersectionBy", intersectionwith: "intersectionWith", invert: "invert", invertby: "invertBy", invokemap: "invokeMap", iteratee: "iteratee", keyby: "keyBy", keys: "keys", keysin: "keysIn", map: "map", mapkeys: "mapKeys", mapvalues: "mapValues", matches: "matches", matchesproperty: "matchesProperty", memoize: "memoize", merge: "merge", mergewith: "mergeWith", method: "method", methodof: "methodOf", mixin: "mixin", negate: "negate", ntharg: "nthArg", omit: "omit", omitby: "omitBy", once: "once", orderby: "orderBy", over: "over", overargs: "overArgs", overevery: "overEvery", oversome: "overSome", partial: "partial", partialright: "partialRight", partition: "partition", pick: "pick", pickby: "pickBy", property: "property", propertyof: "propertyOf", pull: "pull", pullall: "pullAll", pullallby: "pullAllBy", pullallwith: "pullAllWith", pullat: "pullAt", range: "range", rangeright: "rangeRight", rearg: "rearg", reject: "reject", remove: "remove", rest: "rest", reverse: "reverse", samplesize: "sampleSize", set: "set", setwith: "setWith", shuffle: "shuffle", slice: "slice", sortby: "sortBy", sorteduniq: "sortedUniq", sorteduniqby: "sortedUniqBy", split: "split", spread: "spread", tail: "tail", take: "take", takeright: "takeRight", takerightwhile: "takeRightWhile", takewhile: "takeWhile", tap: "tap", throttle: "throttle", thru: "thru", toarray: "toArray", topairs: "toPairs", topairsin: "toPairsIn", topath: "toPath", toplainobject: "toPlainObject", transform: "transform", unary: "unary", union: "union", unionby: "unionBy", unionwith: "unionWith", uniq: "uniq", uniqby: "uniqBy", uniqwith: "uniqWith", unset: "unset", unzip: "unzip", unzipwith: "unzipWith", update: "update", updatewith: "updateWith", values: "values", valuesin: "valuesIn", without: "without", words: "words", wrap: "wrap", xor: "xor", xorby: "xorBy", xorwith: "xorWith", zip: "zip", zipobject: "zipObject", zipobjectdeep: "zipObjectDeep", zipwith: "zipWith", entries: "entries", entriesin: "entriesIn", extend: "extend", extendwith: "extendWith", add: "add", attempt: "attempt", camelcase: "camelCase", capitalize: "capitalize", ceil: "ceil", clamp: "clamp", clone: "clone", clonedeep: "cloneDeep", clonedeepwith: "cloneDeepWith", clonewith: "cloneWith", conformsto: "conformsTo", deburr: "deburr", defaultto: "defaultTo", divide: "divide", endswith: "endsWith", eq: "eq", escape: "escape", escaperegexp: "escapeRegExp", every: "every", find: "find", findindex: "findIndex", findkey: "findKey", findlast: "findLast", findlastindex: "findLastIndex", findlastkey: "findLastKey", floor: "floor", foreach: "forEach", foreachright: "forEachRight", forin: "forIn", forinright: "forInRight", forown: "forOwn", forownright: "forOwnRight", get: "get", gt: "gt", gte: "gte", has: "has", hasin: "hasIn", head: "head", identity: "identity", includes: "includes", indexof: "indexOf", inrange: "inRange", invoke: "invoke", isarguments: "isArguments", isarray: "isArray", isarraybuffer: "isArrayBuffer", isarraylike: "isArrayLike", isarraylikeobject: "isArrayLikeObject", isboolean: "isBoolean", isbuffer: "isBuffer", isdate: "isDate", iselement: "isElement", isempty: "isEmpty", isequal: "isEqual", isequalwith: "isEqualWith", iserror: "isError", isfinite: "isFinite", isfunction: "isFunction", isinteger: "isInteger", islength: "isLength", ismap: "isMap", ismatch: "isMatch", ismatchwith: "isMatchWith", isnan: "isNaN", isnative: "isNative", isnil: "isNil", isnull: "isNull", isnumber: "isNumber", isobject: "isObject", isobjectlike: "isObjectLike", isplainobject: "isPlainObject", isregexp: "isRegExp", issafeinteger: "isSafeInteger", isset: "isSet", isstring: "isString", issymbol: "isSymbol", istypedarray: "isTypedArray", isundefined: "isUndefined", isweakmap: "isWeakMap", isweakset: "isWeakSet", join: "join", kebabcase: "kebabCase", last: "last", lastindexof: "lastIndexOf", lowercase: "lowerCase", lowerfirst: "lowerFirst", lt: "lt", lte: "lte", max: "max", maxby: "maxBy", mean: "mean", meanby: "meanBy", min: "min", minby: "minBy", stubarray: "stubArray", stubfalse: "stubFalse", stubobject: "stubObject", stubstring: "stubString", stubtrue: "stubTrue", multiply: "multiply", nth: "nth", noconflict: "noConflict", noop: "noop", now: "now", pad: "pad", padend: "padEnd", padstart: "padStart", parseint: "parseInt", random: "random", reduce: "reduce", reduceright: "reduceRight", repeat: "repeat", replace: "replace", result: "result", round: "round", runincontext: "runInContext", sample: "sample", size: "size", snakecase: "snakeCase", some: "some", sortedindex: "sortedIndex", sortedindexby: "sortedIndexBy", sortedindexof: "sortedIndexOf", sortedlastindex: "sortedLastIndex", sortedlastindexby: "sortedLastIndexBy", sortedlastindexof: "sortedLastIndexOf", startcase: "startCase", startswith: "startsWith", subtract: "subtract", sum: "sum", sumby: "sumBy", template: "template", times: "times", tofinite: "toFinite", tointeger: "toInteger", tolength: "toLength", tolower: "toLower", tonumber: "toNumber", tosafeinteger: "toSafeInteger", tostring: "toString", toupper: "toUpper", trim: "trim", trimend: "trimEnd", trimstart: "trimStart", truncate: "truncate", unescape: "unescape", uniqueid: "uniqueId", uppercase: "upperCase", upperfirst: "upperFirst", each: "each", eachright: "eachRight", first: "first", }; module.exports = /** * * @param {String} context * @param {String} request * @param {Function} callback */ function externalizeLodash(context, request, callback) { if (request.startsWith("lodash")) { if (request.startsWith("lodash/fp")) { // covers import 'lodash/fp' if (request === "lodash/fp") { return callback(null, "fp"); // covers import 'lodash/fp/get' } else { return callback(null, "fp." + request.split("/")[2]); } // covers import/require 'lodash' and import 'lodash-es' } else if (request === "lodash" || request === "lodash-es") { return callback(null, "_"); // covers import/require from 'lodash.get' and other methods } else if (request[6] === ".") { const method = request.split(".")[1]; // we must map methods to camelCase because that's how they are exposed on lodash object, when loaded externally return callback(null, "_." + mapLowerCaseToCamelCase[method] || method); // covers import/require 'lodash/get' and import 'lodash-es/get' } else if (request[6] === "/" || request[9] === "/") { return callback(null, "_." + request.split("/")[1]); } } callback(); };