Skip to content

How to combine unique and stategy merges #119

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

Closed
llaski opened this issue Oct 3, 2019 · 3 comments · Fixed by #135
Closed

How to combine unique and stategy merges #119

llaski opened this issue Oct 3, 2019 · 3 comments · Fixed by #135
Milestone

Comments

@llaski
Copy link

llaski commented Oct 3, 2019

I can't seem to figure out how combine the unique and strategy merge options. I want to be able to prepend the results of a plugins array and then only keep unique plugin instances based on the constructor name. The default behavior of the unique function is to keep the first unique instance and I was the latter unique instance essentially. Example below:

configA = {
   plugins: [
        new MiniCssExtractPlugin({
            filename: 'main.[contenthash].css',
        }),
}

configB = {
   plugins: [
        new MiniCssExtractPlugin({
            filename: 'main.css',
        }),
}

merge({
      customizeArray: merge.unique(
        'plugins',
        ['MiniCssExtractPlugin'],
        plugin => plugin.constructor && plugin.constructor.name
      )
    })(configA, configB)

This will result in the MiniCssExtractPlugin plugin instance from configA being kept, and I want to keep the one from configB basically.

@chinesedfan
Copy link
Contributor

The provided merge.unique always keeps the first one, but you can change it a bit as your customized unique function. Like,

 import { differenceWith } from 'lodash';
 function mergeUnique(key, uniques, getter = a => a) {
   return (a, b, k) => (
     k === key && [
-      ...a,
+      ...b,
       ...differenceWith(
-        b, a, item => uniques.indexOf(getter(item)) >= 0
+        a, b, item => uniques.indexOf(getter(item)) >= 0
       )
     ]
   );

merge({
      customizeArray: mergeUnique( // use your customized version
        'plugins',
        ['MiniCssExtractPlugin'],
        plugin => plugin.constructor && plugin.constructor.name
      )
    })(configA, configB)

@thibaut-pro
Copy link

thibaut-pro commented Mar 3, 2020

I also expected merge unique to apply precedence to the item on the right like the rest of the merge package.

@bebraw bebraw added this to the v5 milestone Jul 3, 2020
bebraw added a commit that referenced this issue Jul 3, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Closes #119.
@bebraw
Copy link
Member

bebraw commented Jul 3, 2020

There's a fix for this in #135 (v5). I changed the precedence there to pick the last first as that's the best option given the way webpack configurations are structured.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants