webpack-merge
Advanced tools
Comparing version 5.1.4 to 5.2.0
# Changelog | ||
## 5.2.0 / 2020-10-07 | ||
- Feature - Support advanced merging cases through `mergeWithRules` #146 #149 | ||
## 5.1.4 / 2020-09-09 | ||
@@ -105,5 +109,5 @@ | ||
test: /\.js$/, | ||
loaders: ["babel"], | ||
}, | ||
], | ||
loaders: ["babel"] | ||
} | ||
] | ||
}, | ||
@@ -114,5 +118,5 @@ { | ||
test: /\.js$/, | ||
loaders: ["react-hot", "babel"], | ||
}, | ||
], | ||
loaders: ["react-hot", "babel"] | ||
} | ||
] | ||
} | ||
@@ -126,4 +130,4 @@ ); | ||
// order of second argument is respected | ||
loaders: ["react-hot", "babel"], | ||
}, | ||
loaders: ["react-hot", "babel"] | ||
} | ||
]; | ||
@@ -189,6 +193,6 @@ } | ||
const a = { | ||
entry: ["foo"], | ||
entry: ["foo"] | ||
}; | ||
const b = { | ||
entry: [], | ||
entry: [] | ||
}; | ||
@@ -195,0 +199,0 @@ |
import { Configuration } from "webpack"; | ||
import unique from "./unique"; | ||
import { CustomizeRule, ICustomizeRules, ICustomizeOptions, Key } from "./types"; | ||
import { CustomizeRule, ICustomizeOptions, Key } from "./types"; | ||
declare function merge(firstConfiguration: Configuration | Configuration[], ...configurations: Configuration[]): Configuration; | ||
declare function mergeWithCustomize(options: ICustomizeOptions): (firstConfiguration: Configuration | Configuration[], ...configurations: Configuration[]) => Configuration; | ||
declare function customizeArray(rules: ICustomizeRules): (a: any, b: any, key: Key) => any; | ||
declare function customizeObject(rules: ICustomizeRules): (a: any, b: any, key: Key) => any; | ||
export { merge as default, merge, mergeWithCustomize, unique, customizeArray, customizeObject, CustomizeRule, }; | ||
declare function customizeArray(rules: { | ||
[s: string]: CustomizeRule; | ||
}): (a: any, b: any, key: Key) => any; | ||
declare type Rules = { | ||
[s: string]: CustomizeRule | Rules; | ||
}; | ||
declare function mergeWithRules(rules: Rules): (firstConfiguration: Configuration | Configuration[], ...configurations: Configuration[]) => Configuration; | ||
declare function customizeObject(rules: { | ||
[s: string]: CustomizeRule; | ||
}): (a: any, b: any, key: Key) => any; | ||
export { customizeArray, customizeObject, CustomizeRule, merge, merge as default, mergeWithCustomize, mergeWithRules, unique }; |
@@ -26,3 +26,3 @@ "use strict"; | ||
exports.__esModule = true; | ||
exports.CustomizeRule = exports.customizeObject = exports.customizeArray = exports.unique = exports.mergeWithCustomize = exports.merge = exports["default"] = void 0; | ||
exports.unique = exports.mergeWithRules = exports.mergeWithCustomize = exports["default"] = exports.merge = exports.CustomizeRule = exports.customizeObject = exports.customizeArray = void 0; | ||
var wildcard_1 = __importDefault(require("wildcard")); | ||
@@ -35,2 +35,3 @@ var merge_with_1 = __importDefault(require("./merge-with")); | ||
exports.CustomizeRule = types_1.CustomizeRule; | ||
var utils_1 = require("./utils"); | ||
function merge(firstConfiguration) { | ||
@@ -43,4 +44,4 @@ var configurations = []; | ||
} | ||
exports.merge = merge; | ||
exports["default"] = merge; | ||
exports.merge = merge; | ||
function mergeWithCustomize(options) { | ||
@@ -80,11 +81,13 @@ return function mergeWithOptions(firstConfiguration) { | ||
return function (a, b, key) { | ||
var match = Object.keys(rules).find(function (rule) { return wildcard_1["default"](rule, key); }) || ""; | ||
switch (rules[match]) { | ||
case types_1.CustomizeRule.Prepend: | ||
return __spread(b, a); | ||
case types_1.CustomizeRule.Replace: | ||
return b; | ||
case types_1.CustomizeRule.Append: | ||
default: | ||
return __spread(a, b); | ||
var matchedRule = Object.keys(rules).find(function (rule) { return wildcard_1["default"](rule, key); }) || ""; | ||
if (matchedRule) { | ||
switch (rules[matchedRule]) { | ||
case types_1.CustomizeRule.Prepend: | ||
return __spread(b, a); | ||
case types_1.CustomizeRule.Replace: | ||
return b; | ||
case types_1.CustomizeRule.Append: | ||
default: | ||
return __spread(a, b); | ||
} | ||
} | ||
@@ -94,2 +97,94 @@ }; | ||
exports.customizeArray = customizeArray; | ||
function mergeWithRules(rules) { | ||
return mergeWithCustomize({ | ||
customizeArray: function (a, b, key) { | ||
var currentRule = rules; | ||
key.split(".").forEach(function (k) { | ||
currentRule = currentRule[k]; | ||
}); | ||
if (utils_1.isPlainObject(currentRule)) { | ||
return mergeWithRule({ currentRule: currentRule, a: a, b: b }); | ||
} | ||
return []; | ||
} | ||
}); | ||
} | ||
exports.mergeWithRules = mergeWithRules; | ||
var isArray = Array.isArray; | ||
function mergeWithRule(_a) { | ||
var currentRule = _a.currentRule, a = _a.a, b = _a.b; | ||
if (!isArray(a)) { | ||
return a; | ||
} | ||
var bAllMatches = []; | ||
var ret = a.map(function (ao) { | ||
if (!utils_1.isPlainObject(currentRule)) { | ||
return ao; | ||
} | ||
var ret = {}; | ||
var rulesToMatch = []; | ||
var operations = {}; | ||
Object.entries(currentRule).forEach(function (_a) { | ||
var _b = __read(_a, 2), k = _b[0], v = _b[1]; | ||
if (v === types_1.CustomizeRule.Match) { | ||
rulesToMatch.push(k); | ||
} | ||
else { | ||
operations[k] = v; | ||
} | ||
}); | ||
var bMatches = b.filter(function (o) { | ||
var matches = rulesToMatch.every(function (rule) { var _a, _b; return ((_a = ao[rule]) === null || _a === void 0 ? void 0 : _a.toString()) === ((_b = o[rule]) === null || _b === void 0 ? void 0 : _b.toString()); }); | ||
if (matches) { | ||
bAllMatches.push(o); | ||
} | ||
return matches; | ||
}); | ||
Object.entries(ao).forEach(function (_a) { | ||
var _b = __read(_a, 2), k = _b[0], v = _b[1]; | ||
var rule = currentRule; | ||
switch (currentRule[k]) { | ||
case types_1.CustomizeRule.Match: | ||
ret[k] = v; | ||
Object.entries(rule).forEach(function (_a) { | ||
var _b = __read(_a, 2), k = _b[0], v = _b[1]; | ||
if (v === types_1.CustomizeRule.Replace && bMatches.length > 0) { | ||
var val = last(bMatches)[k]; | ||
if (typeof val !== "undefined") { | ||
ret[k] = val; | ||
} | ||
} | ||
}); | ||
break; | ||
case types_1.CustomizeRule.Append: | ||
ret[k] = | ||
bMatches.length > 0 | ||
? v.concat(last(bMatches)[k]) | ||
: v; | ||
break; | ||
case types_1.CustomizeRule.Prepend: | ||
ret[k] = bMatches.length > 0 ? last(bMatches)[k].concat(v) : v; | ||
break; | ||
case types_1.CustomizeRule.Replace: | ||
ret[k] = bMatches.length > 0 ? last(bMatches)[k] : v; | ||
break; | ||
default: | ||
var currentRule_1 = operations[k]; | ||
// Use .flat(); starting from Node 12 | ||
var b_1 = bMatches | ||
.map(function (o) { return o[k]; }) | ||
.reduce(function (acc, val) { | ||
return isArray(acc) && isArray(val) ? __spread(acc, val) : acc; | ||
}, []); | ||
ret[k] = mergeWithRule({ currentRule: currentRule_1, a: v, b: b_1 }); | ||
break; | ||
} | ||
}); | ||
return ret; | ||
}); | ||
return ret.concat(b.filter(function (o) { return !bAllMatches.includes(o); })); | ||
} | ||
function last(arr) { | ||
return arr[arr.length - 1]; | ||
} | ||
function customizeObject(rules) { | ||
@@ -96,0 +191,0 @@ return function (a, b, key) { |
import { Customize, Key } from "./types"; | ||
export default function joinArrays({ customizeArray, customizeObject, key, }?: { | ||
export default function joinArrays({ customizeArray, customizeObject, key }?: { | ||
customizeArray?: Customize; | ||
@@ -4,0 +4,0 @@ customizeObject?: Customize; |
@@ -28,2 +28,3 @@ "use strict"; | ||
var merge_with_1 = __importDefault(require("./merge-with")); | ||
var utils_1 = require("./utils"); | ||
var isArray = Array.isArray; | ||
@@ -34,3 +35,3 @@ function joinArrays(_a) { | ||
var newKey = key ? key + "." + k : k; | ||
if (isFunction(a) && isFunction(b)) { | ||
if (utils_1.isFunction(a) && utils_1.isFunction(b)) { | ||
return function () { | ||
@@ -48,6 +49,6 @@ var args = []; | ||
} | ||
if (isRegex(b)) { | ||
if (utils_1.isRegex(b)) { | ||
return b; | ||
} | ||
if (isPlainObject(a) && isPlainObject(b)) { | ||
if (utils_1.isPlainObject(a) && utils_1.isPlainObject(b)) { | ||
var customResult = customizeObject && customizeObject(a, b, newKey); | ||
@@ -61,3 +62,3 @@ return (customResult || | ||
} | ||
if (isPlainObject(b)) { | ||
if (utils_1.isPlainObject(b)) { | ||
return clone_deep_1["default"](b); | ||
@@ -72,15 +73,2 @@ } | ||
exports["default"] = joinArrays; | ||
function isRegex(o) { | ||
return o instanceof RegExp; | ||
} | ||
// https://stackoverflow.com/a/7356528/228885 | ||
function isFunction(functionToCheck) { | ||
return (functionToCheck && {}.toString.call(functionToCheck) === "[object Function]"); | ||
} | ||
function isPlainObject(a) { | ||
if (a === null) { | ||
return false; | ||
} | ||
return typeof a === "object"; | ||
} | ||
//# sourceMappingURL=join-arrays.js.map |
@@ -7,6 +7,4 @@ export declare type Key = string; | ||
} | ||
export interface ICustomizeRules { | ||
[s: string]: CustomizeRule; | ||
} | ||
export declare enum CustomizeRule { | ||
Match = "match", | ||
Append = "append", | ||
@@ -13,0 +11,0 @@ Prepend = "prepend", |
@@ -6,2 +6,3 @@ "use strict"; | ||
(function (CustomizeRule) { | ||
CustomizeRule["Match"] = "match"; | ||
CustomizeRule["Append"] = "append"; | ||
@@ -8,0 +9,0 @@ CustomizeRule["Prepend"] = "prepend"; |
@@ -5,3 +5,3 @@ { | ||
"author": "Juho Vepsalainen <bebraw@gmail.com>", | ||
"version": "5.1.4", | ||
"version": "5.2.0", | ||
"scripts": { | ||
@@ -24,8 +24,8 @@ "build": "tsc", | ||
"devDependencies": { | ||
"@types/webpack": "^4.41.21", | ||
"husky": "^4.2.5", | ||
"tsdx": "^0.13.2", | ||
"tslib": "^2.0.0", | ||
"typescript": "^3.9.7", | ||
"webpack": "^4.44.1" | ||
"@types/webpack": "^4.41.22", | ||
"husky": "^4.3.0", | ||
"tsdx": "^0.14.0", | ||
"tslib": "^2.0.1", | ||
"typescript": "^4.0.3", | ||
"webpack": "^4.44.2" | ||
}, | ||
@@ -32,0 +32,0 @@ "repository": { |
@@ -160,10 +160,10 @@ [![Financial Contributors on Open Collective](https://opencollective.com/webpack-merge/all/badge.svg?label=financial+contributors)](https://opencollective.com/webpack-merge) [![build status](https://secure.travis-ci.org/survivejs/webpack-merge.svg)](http://travis-ci.org/survivejs/webpack-merge) [![codecov](https://codecov.io/gh/survivejs/webpack-merge/branch/master/graph/badge.svg)](https://codecov.io/gh/survivejs/webpack-merge) | ||
["HotModuleReplacementPlugin"], | ||
(plugin) => plugin.constructor && plugin.constructor.name | ||
), | ||
plugin => plugin.constructor && plugin.constructor.name | ||
) | ||
})( | ||
{ | ||
plugins: [new webpack.HotModuleReplacementPlugin()], | ||
plugins: [new webpack.HotModuleReplacementPlugin()] | ||
}, | ||
{ | ||
plugins: [new webpack.HotModuleReplacementPlugin()], | ||
plugins: [new webpack.HotModuleReplacementPlugin()] | ||
} | ||
@@ -176,2 +176,71 @@ ); | ||
## **`mergeWithRules`** | ||
To support advanced merging needs (i.e. merging within loaders), `mergeWithRules` includes additional syntax that allows you to match fields and apply strategies to match. Consider the full example below: | ||
```javascript | ||
const a = { | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.css$/, | ||
use: [{ loader: "style-loader" }, { loader: "sass-loader" }] | ||
} | ||
] | ||
} | ||
}; | ||
const b = { | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.css$/, | ||
use: [ | ||
{ | ||
loader: "style-loader", | ||
options: { | ||
modules: true | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
}; | ||
const result = { | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.css$/, | ||
use: [ | ||
{ | ||
loader: "style-loader", | ||
options: { | ||
modules: true | ||
} | ||
}, | ||
{ loader: "sass-loader" } | ||
] | ||
} | ||
] | ||
} | ||
}; | ||
assert.deepStrictEqual( | ||
mergeWithRules({ | ||
module: { | ||
rules: { | ||
test: "match", | ||
use: { | ||
loader: "match", | ||
options: "replace" | ||
} | ||
} | ||
} | ||
})(a, b), | ||
result | ||
); | ||
``` | ||
The way it works is that you should annotate fields to match using `match` (or `CustomizeRule.Match` if you are using TypeScript) matching your configuration structure and then use specific strategies to define how particular fields should be transformed. | ||
## Development | ||
@@ -178,0 +247,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49107
22
412
292