Skip to content

Commit

Permalink
unique merge array customisation fix (#162)
Browse files Browse the repository at this point in the history
Closes #161.
  • Loading branch information
trombka authored Dec 8, 2020
1 parent 57ec295 commit 927e049
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ The first `<field>` is the config property to look through for duplicates.

`<fields>` represents the values that should be unique when you run the field => field function on each duplicate.

When the order of elements of the `<field>` in the first configuration differs from the order in the second configuration, the latter is preserved.

```javascript
const { mergeWithCustomize, unique } = require("webpack-merge");

Expand Down
31 changes: 12 additions & 19 deletions src/unique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,19 @@ function mergeUnique(
uniques: string[],
getter: (a: object) => string
) {
let uniquesSet = new Set(uniques)
return (a: [], b: [], k: string) =>
k === key && [
...difference(a, b, (item) => uniques.indexOf(getter(item))),
...b,
];
}

function difference(a: object[], b: object[], cb: (v: object) => number) {
const ret = a.filter((v, i) => {
const foundA = cb(v);
const foundB = cb(b[i] || {});

if (foundA >= 0 && foundB >= 0) {
return foundA !== foundB;
}

return true;
});

return ret;
(k === key) && Array.from(
[...a, ...b]
.map((it: object) => ({ key: getter(it), value: it }))
.map(({ key, value }) => ({ key: (uniquesSet.has(key) ? key : value), value: value}))
.reduce(
(m, { key, value}) => {
m.delete(key); // This is required to preserve backward compatible order of elements after a merge.
return m.set(key, value)
},
new Map<any, any>())
.values())
}

export default mergeUnique;
32 changes: 31 additions & 1 deletion test/unique.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe("Unique", function () {
assert.deepStrictEqual(output, expected);
});

it("should not lose any plugins", function () {
it("should not lose any trailing plugins", function () {
const output = mergeWithCustomize({
customizeArray: unique(
"plugins",
Expand Down Expand Up @@ -89,6 +89,36 @@ describe("Unique", function () {
assert.deepStrictEqual(output, expected);
});

it("should not lose any leading plugins", function () {
const output = mergeWithCustomize({
customizeArray: unique(
"plugins",
["HotModuleReplacementPlugin"],
(plugin) => plugin.constructor && plugin.constructor.name
),
})(
{
plugins: [
new webpack.DefinePlugin({}),
new webpack.HotModuleReplacementPlugin(),
],
},
{
plugins: [new webpack.HotModuleReplacementPlugin()],
}
);
// The HMR plugin is picked from the last one due to
// default ordering!
const expected = {
plugins: [
new webpack.DefinePlugin({}),
new webpack.HotModuleReplacementPlugin(),
],
};

assert.deepStrictEqual(output, expected);
});

it("should check only against named plugins (#125)", function () {
const output = mergeWithCustomize({
customizeArray: unique(
Expand Down

0 comments on commit 927e049

Please sign in to comment.