Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support object/array-formed condition (#212) #213

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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