Skip to content

Commit

Permalink
fix: support object/array-formed condition (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
RexSkz committed Oct 14, 2023
1 parent 20b960a commit b58f98d
Show file tree
Hide file tree
Showing 5 changed files with 449 additions and 3 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
],
"dependencies": {
"clone-deep": "^4.0.1",
"flat": "^5.0.2",
"wildcard": "^2.0.0"
},
"devDependencies": {
"@types/estree": "0.0.48",
"@types/flat": "^5.0.3",
"husky": "^6.0.0",
"prettier": "^2.3.1",
"tsdx": "^0.14.1",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ICustomizeOptions,
Key,
} from "./types";
import { isPlainObject, isUndefined } from "./utils";
import { isPlainObject, isSameCondition, isUndefined } from "./utils";

function merge<Configuration extends object>(
firstConfiguration: Configuration | Configuration[],
Expand Down Expand Up @@ -156,7 +156,7 @@ function mergeWithRule({

const bMatches = b.filter((o) => {
const matches = rulesToMatch.every(
(rule) => ao[rule]?.toString() === o[rule]?.toString()
(rule) => isSameCondition(ao[rule], o[rule])
);

if (matches) {
Expand Down
59 changes: 58 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { flatten } from "flat";

function isRegex(o) {
return o instanceof RegExp;
}
Expand All @@ -21,4 +23,59 @@ function isUndefined(a) {
return typeof a === "undefined";
}

export { isRegex, isFunction, isPlainObject, isUndefined };
/**
* According to Webpack docs, a "test" should be the following:
*
* - A string
* - A RegExp
* - A function
* - An array of conditions (may be nested)
* - An object of conditions (may be nested)
*
* https://webpack.js.org/configuration/module/#condition
*/
function isSameCondition(a, b) {
if (!a || !b) {
return a === b;
}
if (
typeof a === 'string' && typeof b === 'string' ||
isRegex(a) && isRegex(b) ||
isFunction(a) && isFunction(b)
) {
return a.toString() === b.toString();
}

const entriesA = Object.entries(flatten<any, object>(a));
const entriesB = Object.entries(flatten<any, object>(b));
if (entriesA.length !== entriesB.length) {
return false;
}

for (let i = 0; i < entriesA.length; i++) {
entriesA[i][0] = entriesA[i][0].replace(/\b\d+\b/g, "[]");
entriesB[i][0] = entriesB[i][0].replace(/\b\d+\b/g, "[]");
}

function cmp([k1, v1], [k2, v2]) {
if (k1 < k2) return -1;
if (k1 > k2) return 1;
if (v1 < v2) return -1;
if (v1 > v2) return 1;
return 0;
};
entriesA.sort(cmp);
entriesB.sort(cmp);

if (entriesA.length !== entriesB.length) {
return false;
}
for (let i = 0; i < entriesA.length; i++) {
if (entriesA[i][0] !== entriesB[i][0] || entriesA[i][1]?.toString() !== entriesB[i][1]?.toString()) {
return false;
}
}
return true;
}

export { isRegex, isFunction, isPlainObject, isUndefined, isSameCondition };
Loading

0 comments on commit b58f98d

Please sign in to comment.