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

unique merge array customisation fix #162

Merged
merged 4 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
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;
34 changes: 32 additions & 2 deletions 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 All @@ -69,8 +69,8 @@ describe("Unique", function () {
})(
{
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.DefinePlugin({}),
new webpack.HotModuleReplacementPlugin(),
bebraw marked this conversation as resolved.
Show resolved Hide resolved
],
},
{
Expand All @@ -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