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 reLodash = RegExp('/lodash(?:/(?!fp/)|-es/|-amd/)');
|
28 |
|
29 | var LodashModuleReplacementPlugin = function () {
|
30 | function LodashModuleReplacementPlugin(options) {
|
31 | var _this = this;
|
32 |
|
33 | _classCallCheck(this, LodashModuleReplacementPlugin);
|
34 |
|
35 | options || (options = {});
|
36 | this.matches = [];
|
37 | this.patterns = [];
|
38 |
|
39 | _lodash2.default.forOwn(_mapping.features, function (pairs, key) {
|
40 | if (!options[key]) {
|
41 | _lodash2.default.each(pairs, function (pair) {
|
42 | _this.patterns.push(['/' + pair[0] + '.js', './' + pair[1] + '.js']);
|
43 | });
|
44 | }
|
45 | });
|
46 | }
|
47 |
|
48 | _createClass(LodashModuleReplacementPlugin, [{
|
49 | key: 'apply',
|
50 | value: function apply(compiler) {
|
51 | var _this2 = this;
|
52 |
|
53 | var resolvePath = _lodash2.default.memoize(function (resource) {
|
54 | if (reLodash.test(resource)) {
|
55 | var length = _this2.patterns.length;
|
56 |
|
57 | while (length--) {
|
58 | var pair = _this2.patterns[length];
|
59 | if (_lodash2.default.endsWith(resource, pair[0])) {
|
60 | var result = _path2.default.resolve(_path2.default.dirname(resource), pair[1]);
|
61 | if (_fs2.default.existsSync(result)) {
|
62 | _this2.matches.push([resource, result]);
|
63 | return result;
|
64 | }
|
65 | }
|
66 | }
|
67 | }
|
68 | return resource;
|
69 | });
|
70 |
|
71 | compiler.plugin('normal-module-factory', function (nmf) {
|
72 | nmf.plugin('after-resolve', function (result, callback) {
|
73 | if (!result) {
|
74 | return callback();
|
75 | }
|
76 | result.resource = resolvePath(result.resource);
|
77 | return callback(null, result);
|
78 | });
|
79 | });
|
80 | }
|
81 | }]);
|
82 |
|
83 | return LodashModuleReplacementPlugin;
|
84 | }();
|
85 |
|
86 | exports.default = LodashModuleReplacementPlugin;
|
87 | ;
|
88 | module.exports = exports['default']; |
\ | No newline at end of file |