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

Merging complete webpack configs using mergeWithRules empties primitive arrays #151

Closed
dennisoelkers opened this issue Oct 15, 2020 · 5 comments · Fixed by #152
Closed

Comments

@dennisoelkers
Copy link

I am in the process of upgrading to v4, currently trying to replace former usages of merge.smart to merge complete webpack configurations. My current definition using mergeWithRules looks like this:

mergeWithRules({
  module: {
    rules: {
      test: "match",
      use: {
        loader: "match",
        options: "replace"
      }
    }
  },
  resolve: {
    extensions: 'append',
    module: 'match',
    alias: 'match'
  },
  resolveLoader: {
    modules: "match",
    moduleExtensions: "match"
  }
})

There are two issues I am currently having:

  1. When merging two configs, I need to define rules for e.g. resolve.extensions, otherwise I am getting an error
  2. Defining a rule like above for keys which would otherwise result in an error, lead to resolve.extensions (which contains an array of strings) being an empty array in the resulting config.

Am I doing something wrong? How can I merge two complete webpack configurations while still replacing redundant loaders and merging arrays for uniqueness like resolve.extensions?

@bebraw
Copy link
Member

bebraw commented Oct 15, 2020

Can you provide a full example of the configuration you are trying to merge as above? Ideally this should be something I can run and convert as a test. It's possible mergeWithRules is still missing some corner cases as it's a new API so this step will let me validate that.

In my personal work, I always use regular merge and compose from small fragments but I understand if that's not always possible.

@dennisoelkers
Copy link
Author

A simple example would be this:

const module = {
    rules: {
      test: "match",
      use: {
        loader: "match",
        options: "replace",
      },
    },
  };
const _mergeWithoutRule = mergeWithRules({
  module,
});

const _mergeWithExplicitRule = mergeWithRules({
  module,
  resolve: {
    extensions: 'append',
    module: 'match',
    alias: 'match',
  },
});

const config = { resolve: { extensions: ['.js'] } };

expect(() => _mergeWithoutRule(config, config)).not.toThrow();
expect(_mergeWithExplicitRule(config, config)).toEqual(config);

This leaves out all the handling of the module part, which is fine. It merges a config back to itself, which is not really helpful, but works to produce the error/invalid result.

I also added runkit links for easier reproducibility:

@bebraw
Copy link
Member

bebraw commented Oct 16, 2020

Ok, thanks! I'll work against your examples then.

@bebraw
Copy link
Member

bebraw commented Nov 12, 2020

I've fixed the throwing part at 5.4.0 and I have a proposal for the rest at #152.

@dennisoelkers
Copy link
Author

Great, thanks for that! I will try it out and check if it fixes our issue asap.

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.

2 participants