index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 'use strict';
  2. var _unionWith2 = require('lodash/unionWith');
  3. var _unionWith3 = _interopRequireDefault(_unionWith2);
  4. var _mergeWith2 = require('lodash/mergeWith');
  5. var _mergeWith3 = _interopRequireDefault(_mergeWith2);
  6. var _differenceWith2 = require('lodash/differenceWith');
  7. var _differenceWith3 = _interopRequireDefault(_differenceWith2);
  8. var _joinArrays = require('./join-arrays');
  9. var _joinArrays2 = _interopRequireDefault(_joinArrays);
  10. var _joinArraysSmart = require('./join-arrays-smart');
  11. var _unique = require('./unique');
  12. var _unique2 = _interopRequireDefault(_unique);
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. function _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); } }
  15. function merge() {
  16. for (var _len = arguments.length, sources = Array(_len), _key = 0; _key < _len; _key++) {
  17. sources[_key] = arguments[_key];
  18. }
  19. // This supports
  20. // merge([<object>] | ...<object>)
  21. // merge({ customizeArray: <fn>, customizeObject: <fn>})([<object>] | ...<object>)
  22. // where fn = (a, b, key)
  23. if (sources.length === 1) {
  24. if (Array.isArray(sources[0])) {
  25. return _mergeWith3.default.apply(undefined, [{}].concat(_toConsumableArray(sources[0]), [(0, _joinArrays2.default)(sources[0])]));
  26. }
  27. if (sources[0].customizeArray || sources[0].customizeObject) {
  28. return function () {
  29. for (var _len2 = arguments.length, structures = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  30. structures[_key2] = arguments[_key2];
  31. }
  32. if (Array.isArray(structures[0])) {
  33. return _mergeWith3.default.apply(undefined, [{}].concat(_toConsumableArray(structures[0]), [(0, _joinArrays2.default)(sources[0])]));
  34. }
  35. return _mergeWith3.default.apply(undefined, [{}].concat(structures, [(0, _joinArrays2.default)(sources[0])]));
  36. };
  37. }
  38. return sources[0];
  39. }
  40. return _mergeWith3.default.apply(undefined, [{}].concat(sources, [(0, _joinArrays2.default)()]));
  41. }
  42. var mergeSmart = merge({
  43. customizeArray: function customizeArray(a, b, key) {
  44. if (isRule(key.split('.').slice(-1)[0])) {
  45. return (0, _unionWith3.default)(a, b, _joinArraysSmart.uniteRules.bind(null, {}, key));
  46. }
  47. return null;
  48. }
  49. });
  50. // rules: { <field>: <'append'|'prepend'|'replace'> }
  51. // All default to append but you can override here
  52. var mergeStrategy = function mergeStrategy() {
  53. var rules = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  54. return merge({
  55. customizeArray: _customizeArray(rules),
  56. customizeObject: customizeObject(rules)
  57. });
  58. };
  59. var mergeSmartStrategy = function mergeSmartStrategy() {
  60. var rules = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  61. return merge({
  62. customizeArray: function customizeArray(a, b, key) {
  63. var topKey = key.split('.').slice(-1)[0];
  64. if (isRule(topKey)) {
  65. switch (rules[key]) {
  66. case 'prepend':
  67. return [].concat(_toConsumableArray((0, _differenceWith3.default)(b, a, function (newRule, seenRule) {
  68. return (0, _joinArraysSmart.uniteRules)(rules, key, newRule, seenRule, 'prepend');
  69. })), _toConsumableArray(a));
  70. case 'replace':
  71. return b;
  72. default:
  73. // append
  74. return (0, _unionWith3.default)(a, b, _joinArraysSmart.uniteRules.bind(null, rules, key));
  75. }
  76. }
  77. return _customizeArray(rules)(a, b, key);
  78. },
  79. customizeObject: customizeObject(rules)
  80. });
  81. };
  82. function _customizeArray(rules) {
  83. return function (a, b, key) {
  84. switch (rules[key]) {
  85. case 'prepend':
  86. return [].concat(_toConsumableArray(b), _toConsumableArray(a));
  87. case 'replace':
  88. return b;
  89. default:
  90. // append
  91. return false;
  92. }
  93. };
  94. }
  95. function customizeObject(rules) {
  96. return function (a, b, key) {
  97. switch (rules[key]) {
  98. case 'prepend':
  99. return (0, _mergeWith3.default)({}, b, a, (0, _joinArrays2.default)());
  100. case 'replace':
  101. return b;
  102. default:
  103. // append
  104. return false;
  105. }
  106. };
  107. }
  108. function isRule(key) {
  109. return ['preLoaders', 'loaders', 'postLoaders', 'rules'].indexOf(key) >= 0;
  110. }
  111. module.exports = merge;
  112. module.exports.smart = mergeSmart;
  113. module.exports.strategy = mergeStrategy;
  114. module.exports.smartStrategy = mergeSmartStrategy;
  115. module.exports.unique = _unique2.default;