1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | 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; }; }();
|
8 |
|
9 | var _lodash = require('lodash');
|
10 |
|
11 | var _lodash2 = _interopRequireDefault(_lodash);
|
12 |
|
13 | var _fs = require('fs');
|
14 |
|
15 | var _fs2 = _interopRequireDefault(_fs);
|
16 |
|
17 | var _path = require('path');
|
18 |
|
19 | var _path2 = _interopRequireDefault(_path);
|
20 |
|
21 | var _mapping = require('./mapping');
|
22 |
|
23 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
24 |
|
25 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
26 |
|
27 | var rePath = RegExp('/lodash(?:/(?!fp/)|-es/|-amd/)');
|
28 | var reRequest = RegExp('^lodash(?:/|-es/|-amd/)\\w+$');
|
29 |
|
30 | var stubs = ['./identity.js', './noop.js', './stubArray.js', './stubFalse.js', './stubObject.js', './stubString.js', './stubTrue.js'];
|
31 |
|
32 | function getPatterns(options) {
|
33 | var result = [];
|
34 | _lodash2.default.forOwn(_mapping.features, function (pairs, key) {
|
35 | if (!options[key]) {
|
36 | _lodash2.default.each(pairs, function (pair) {
|
37 | result.push(['/' + pair[0] + '.js', './' + pair[1] + '.js']);
|
38 | });
|
39 | }
|
40 | });
|
41 | return result;
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 | var LodashModuleReplacementPlugin = function () {
|
47 | function LodashModuleReplacementPlugin(options) {
|
48 | _classCallCheck(this, LodashModuleReplacementPlugin);
|
49 |
|
50 | this.matches = [];
|
51 | this.options = 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 (data) {
|
61 | var rawRequest = data.rawRequest;
|
62 | var resource = data.resource;
|
63 |
|
64 | var result = resource;
|
65 | if (!rePath.test(resource)) {
|
66 | return result;
|
67 | }
|
68 | _lodash2.default.each(_this.patterns, function (pair) {
|
69 |
|
70 |
|
71 | if (!_lodash2.default.endsWith(resource, pair[0]) || reRequest.test(rawRequest) && _lodash2.default.includes(stubs, pair[1])) {
|
72 | return;
|
73 | }
|
74 | var modulePath = _path2.default.resolve(_path2.default.dirname(resource), pair[1]);
|
75 | if (_fs2.default.existsSync(modulePath)) {
|
76 | result = modulePath;
|
77 | _this.matches.push([resource, result]);
|
78 | return false;
|
79 | }
|
80 | });
|
81 | return result;
|
82 | }, function (data) {
|
83 | return data.resource;
|
84 | });
|
85 |
|
86 | compiler.plugin('normal-module-factory', function (nmf) {
|
87 | nmf.plugin('before-resolve', function (data, callback) {
|
88 | if (!data) {
|
89 | return callback();
|
90 | }
|
91 | var request = data.request;
|
92 |
|
93 | if (reRequest.test(request)) {
|
94 | var override = _mapping.overrides[_path2.default.basename(request, '.js')];
|
95 | if (!_lodash2.default.isMatch(_this.options, override)) {
|
96 | _this.patterns = getPatterns(_lodash2.default.assign(_this.options, override));
|
97 | }
|
98 | }
|
99 | return callback(null, data);
|
100 | });
|
101 |
|
102 | nmf.plugin('after-resolve', function (data, callback) {
|
103 | if (!data) {
|
104 | return callback();
|
105 | }
|
106 | data.resource = resolvePath(data);
|
107 | return callback(null, data);
|
108 | });
|
109 | });
|
110 | }
|
111 | }]);
|
112 |
|
113 | return LodashModuleReplacementPlugin;
|
114 | }();
|
115 |
|
116 | exports.default = LodashModuleReplacementPlugin;
|
117 | ;
|
118 | module.exports = exports['default']; |
\ | No newline at end of file |