lodash-webpack-plugin
Advanced tools
Comparing version 0.10.7 to 0.11.0
155
lib/index.js
@@ -7,8 +7,26 @@ 'use strict'; | ||
var _includes2 = require('lodash/includes'); | ||
var _includes3 = _interopRequireDefault(_includes2); | ||
var _isMatch2 = require('lodash/isMatch'); | ||
var _isMatch3 = _interopRequireDefault(_isMatch2); | ||
var _memoize2 = require('lodash/memoize'); | ||
var _memoize3 = _interopRequireDefault(_memoize2); | ||
var _forOwn2 = require('lodash/forOwn'); | ||
var _forOwn3 = _interopRequireDefault(_forOwn2); | ||
var _escapeRegExp2 = require('lodash/escapeRegExp'); | ||
var _escapeRegExp3 = _interopRequireDefault(_escapeRegExp2); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _lodash = require('lodash'); | ||
exports.default = Plugin; | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _fs = require('fs'); | ||
@@ -32,4 +50,3 @@ | ||
var reFwdSep = /\//g; | ||
var rsSysSep = _lodash2.default.escapeRegExp(_path2.default.sep); | ||
var lodashRoot = _path2.default.dirname(require.resolve('lodash')); | ||
var normalize = function normalize(string) { | ||
@@ -39,10 +56,10 @@ return string.replace(reFwdSep, rsSysSep); | ||
var reFwdSep = /\//g; | ||
var rsSysSep = (0, _escapeRegExp3.default)(_path2.default.sep); | ||
var reLodashRes = RegExp(normalize('lodash(?:/(?!fp/)|-amd/|-es/|\\.\\w+/)')); | ||
var reExplicitReq = RegExp('^lodash(?:/|-amd/|-es/|\\.\\w+/)\\w+$'); | ||
var lodashRoot = _path2.default.dirname(require.resolve('lodash')); | ||
function getPatterns(options) { | ||
var result = []; | ||
_lodash2.default.forOwn(_mapping.features, function (pairs, key) { | ||
(0, _forOwn3.default)(_mapping.features, function (pairs, key) { | ||
if (!options[key]) { | ||
@@ -62,4 +79,5 @@ result.push.apply(result, _toConsumableArray(pairs)); | ||
this.matches = []; | ||
this.options = _lodash2.default.assign({}, options); | ||
this.options = Object.assign({}, options); | ||
this.patterns = getPatterns(this.options); | ||
this.resolve = this.resolve.bind(this); | ||
} | ||
@@ -70,53 +88,10 @@ | ||
value: function apply(compiler) { | ||
var _this = this; | ||
var resolvePath = _lodash2.default.memoize(function (_ref) { | ||
var rawRequest = _ref.rawRequest, | ||
resource = _ref.resource; | ||
var result = resource; | ||
if (!reLodashRes.test(resource)) { | ||
return result; | ||
} | ||
var isExplicit = reExplicitReq.test(rawRequest); | ||
var resName = _path2.default.basename(resource, '.js'); | ||
var resRoot = _path2.default.dirname(resource); | ||
if (isExplicit) { | ||
// Apply any feature set overrides for explicitly requested modules. | ||
var override = _mapping.overrides[_path2.default.basename(rawRequest, '.js')]; | ||
if (!_lodash2.default.isMatch(_this.options, override)) { | ||
_this.patterns = getPatterns(_lodash2.default.assign(_this.options, override)); | ||
} | ||
} | ||
_lodash2.default.each(_this.patterns, function (pair) { | ||
// Replace matches as long as they aren't explicit requests for stubbed modules. | ||
var isStubbed = _lodash2.default.includes(_listing.stubs, pair[1]); | ||
if (resName != pair[0] || isExplicit && isStubbed) { | ||
return; | ||
} | ||
var moduleFilename = `${ pair[1] }.js`; | ||
var modulePath = _path2.default.join(resRoot, moduleFilename); | ||
var exists = _fs2.default.existsSync(modulePath); | ||
if (isStubbed && !exists) { | ||
exists = true; | ||
modulePath = _path2.default.join(lodashRoot, moduleFilename); | ||
} | ||
if (exists) { | ||
result = modulePath; | ||
_this.matches.push([resource, result]); | ||
return false; | ||
} | ||
}); | ||
return result; | ||
}, function (_ref2) { | ||
var resource = _ref2.resource; | ||
var resolve = (0, _memoize3.default)(this.resolve, function (_ref) { | ||
var resource = _ref.resource; | ||
return resource; | ||
}); | ||
compiler.plugin('normal-module-factory', function (nmf) { | ||
nmf.plugin('after-resolve', function (data, callback) { | ||
if (data) { | ||
data.resource = resolvePath(data); | ||
data.resource = resolve(data); | ||
return callback(null, data); | ||
@@ -128,2 +103,47 @@ } | ||
} | ||
}, { | ||
key: 'resolve', | ||
value: function resolve(_ref2) { | ||
var _this = this; | ||
var rawRequest = _ref2.rawRequest, | ||
resource = _ref2.resource; | ||
var result = resource; | ||
if (!reLodashRes.test(resource)) { | ||
return result; | ||
} | ||
var isExplicit = reExplicitReq.test(rawRequest); | ||
var resName = _path2.default.basename(resource, '.js'); | ||
var resRoot = _path2.default.dirname(resource); | ||
if (isExplicit) { | ||
// Apply any feature set overrides for explicitly requested modules. | ||
var override = _mapping.overrides[_path2.default.basename(rawRequest, '.js')]; | ||
if (!(0, _isMatch3.default)(this.options, override)) { | ||
this.patterns = getPatterns(Object.assign(this.options, override)); | ||
} | ||
} | ||
this.patterns.forEach(function (pair) { | ||
// Replace matches as long as they aren't explicit requests for stubbed modules. | ||
var isStubbed = (0, _includes3.default)(_listing.stubs, pair[1]); | ||
if (resName != pair[0] || isExplicit && isStubbed) { | ||
return; | ||
} | ||
var moduleFilename = `${ pair[1] }.js`; | ||
var modulePath = _path2.default.join(resRoot, moduleFilename); | ||
var exists = _fs2.default.existsSync(modulePath); | ||
if (isStubbed && !exists) { | ||
exists = true; | ||
modulePath = _path2.default.join(lodashRoot, moduleFilename); | ||
} | ||
if (exists) { | ||
result = modulePath; | ||
_this.matches.push([resource, result]); | ||
return false; | ||
} | ||
}); | ||
return result; | ||
} | ||
}]); | ||
@@ -134,4 +154,27 @@ | ||
exports.default = LodashModuleReplacementPlugin; | ||
; | ||
/*----------------------------------------------------------------------------*/ | ||
function Plugin(nodeResolve, options) { | ||
// For Webpack. | ||
if (this instanceof Plugin) { | ||
return new LodashModuleReplacementPlugin(nodeResolve); | ||
} | ||
// For Rollup. | ||
var _resolveId = nodeResolve.resolveId; | ||
var resolver = new LodashModuleReplacementPlugin(options); | ||
var resolve = (0, _memoize3.default)(resolver.resolve, function (_ref3) { | ||
var resource = _ref3.resource; | ||
return resource; | ||
}); | ||
return Object.assign({}, nodeResolve, { | ||
resolveId(importee, importer) { | ||
return _resolveId(importee, importer).then(function (id) { | ||
return resolve({ 'rawRequest': importee, 'resource': id }); | ||
}); | ||
} | ||
}); | ||
}; | ||
module.exports = exports['default']; |
{ | ||
"name": "lodash-webpack-plugin", | ||
"version": "0.10.7", | ||
"version": "0.11.0", | ||
"description": "Smaller modular Lodash builds.", | ||
@@ -28,3 +28,3 @@ "keywords": "lodash, modules, webpack-plugin", | ||
"babel-plugin-lodash": "^3.2.11", | ||
"babel-preset-env": "^1.1.5", | ||
"babel-preset-env": "^1.1.8", | ||
"chai": "^3.5.0", | ||
@@ -31,0 +31,0 @@ "glob": "^7.1.1", |
@@ -1,2 +0,2 @@ | ||
# lodash-webpack-plugin v0.10.7 | ||
# lodash-webpack-plugin v0.11.0 | ||
@@ -3,0 +3,0 @@ Create smaller Lodash builds by replacing [feature sets](#feature-sets) of modules |
16258
196