UNPKG

4.08 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; }; }();
8
9var _lodash = require('lodash');
10
11var _lodash2 = _interopRequireDefault(_lodash);
12
13var _fs = require('fs');
14
15var _fs2 = _interopRequireDefault(_fs);
16
17var _path = require('path');
18
19var _path2 = _interopRequireDefault(_path);
20
21var _listing = require('./listing');
22
23var _mapping = require('./mapping');
24
25function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26
27function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
28
29function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
30
31var rePath = RegExp('/lodash(?:/(?!fp/)|-es/|-amd/)');
32var reRequest = RegExp('^lodash(?:/|-es/|-amd/)\\w+$');
33
34function getPatterns(options) {
35 var result = [];
36 _lodash2.default.forOwn(_mapping.features, function (pairs, key) {
37 if (!options[key]) {
38 result.push.apply(result, _toConsumableArray(pairs));
39 }
40 });
41 return result;
42}
43
44/*----------------------------------------------------------------------------*/
45
46var LodashModuleReplacementPlugin = function () {
47 function LodashModuleReplacementPlugin(options) {
48 _classCallCheck(this, LodashModuleReplacementPlugin);
49
50 this.matches = [];
51 this.options = _lodash2.default.assign({}, options);
52 this.patterns = getPatterns(this.options);
53 }
54
55 _createClass(LodashModuleReplacementPlugin, [{
56 key: 'apply',
57 value: function apply(compiler) {
58 var _this = this;
59
60 var resolvePath = _lodash2.default.memoize(function (_ref) {
61 var rawRequest = _ref.rawRequest;
62 var resource = _ref.resource;
63
64 var result = resource;
65 if (!rePath.test(resource)) {
66 return result;
67 }
68 if (reRequest.test(rawRequest)) {
69 // Apply any feature set overrides for explicitly requested modules.
70 var override = _mapping.overrides[_path2.default.basename(rawRequest, '.js')];
71 if (!_lodash2.default.isMatch(_this.options, override)) {
72 _this.patterns = getPatterns(_lodash2.default.assign(_this.options, override));
73 }
74 }
75 _lodash2.default.each(_this.patterns, function (pair) {
76 // Replace matches as long as they aren't explicit requests for stubbed modules.
77 if (_path2.default.basename(resource, '.js') != pair[0] || reRequest.test(rawRequest) && _lodash2.default.includes(_listing.stubs, pair[1])) {
78 return;
79 }
80 var modulePath = _path2.default.join(_path2.default.dirname(resource), pair[1] + '.js');
81 if (_fs2.default.existsSync(modulePath)) {
82 result = modulePath;
83 _this.matches.push([resource, result]);
84 return false;
85 }
86 });
87 return result;
88 }, function (_ref2) {
89 var resource = _ref2.resource;
90 return resource;
91 });
92
93 compiler.plugin('normal-module-factory', function (nmf) {
94 nmf.plugin('after-resolve', function (data, callback) {
95 if (data) {
96 data.resource = resolvePath(data);
97 }
98 return data ? callback(null, data) : callback();
99 });
100 });
101 }
102 }]);
103
104 return LodashModuleReplacementPlugin;
105}();
106
107exports.default = LodashModuleReplacementPlugin;
108;
109module.exports = exports['default'];
\No newline at end of file