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

Allow appending objects in mergeWithRules #163

Closed
just-jeb opened this issue Dec 10, 2020 · 6 comments
Closed

Allow appending objects in mergeWithRules #163

just-jeb opened this issue Dec 10, 2020 · 6 comments

Comments

@just-jeb
Copy link
Contributor

Consider the following example:

const a = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [{ loader: "style-loader", options: { someOption: true } }, { loader: "sass-loader" }],
      },
    ],
  },
};
const b = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          {
            loader: "style-loader",
            options: {
              modules: true,
            },
          },
        ],
      },
    ],
  },
};
const result = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          {
            loader: "style-loader",
            options: {
              someOptions: true,
              modules: true,
            },
          },
          { loader: "sass-loader" },
        ],
      },
    ],
  },
};

assert.deepStrictEqual(
  mergeWithRules({
    module: {
      rules: {
        test: "match",
        use: {
          loader: "match",
          options: "append",
        },
      },
    },
  })(a, b),
  result
);

Currently this will fail, since append only supports arrays. However, merging loaders options is a perfectly valid scenario and should be pretty easy to support.
Any chance we can get that?

@bebraw
Copy link
Member

bebraw commented Dec 10, 2020 via email

@just-jeb
Copy link
Contributor Author

just-jeb commented Dec 10, 2020

Sure I thought about it too. It's either this or make append and prepend work excatly the same for objects. But merge sounds good as well.
Also would be nice to get proper exception for certain option not being allowed for objects/arrays. Today if I use append on object I'd get “concat is not a function".

@bebraw
Copy link
Member

bebraw commented Dec 10, 2020

Also would be nice to get proper exception for certain option not being allowed for objects/arrays. Today if I use append on object I'd get “concat is not a function".

Yeah, I can add a type check and throw for sure.

@bebraw bebraw closed this as completed in 76ecea7 Dec 11, 2020
@bebraw
Copy link
Member

bebraw commented Dec 11, 2020

@just-jeb I added the merge feature in 5.6.0.

When looking at the code, I realized I cannot add type check against objects/arrays for the case you had in mind as webpack-merge doesn't know anything about webpack's typings. In technical terms, the name webpack-merge is misnomer now that in reality it's a generic merge that happens to be useful with webpack. Occasionally I use it for merging other configurations as well.

It's likely best to use something like TypeScript for checking general webpack configuration as it's definitely beyond the scope of webpack-merge.

@just-jeb
Copy link
Contributor Author

just-jeb commented Dec 12, 2020

I understand. Instead of type checking Webpack types though you could check for object/array. Regardless of the application (whether you merge webpack config or something else) you can limit the append/prepend to arrays only and merge to objects only. This way you'll have proper warnings/errors while avoiding things like "concat is not a function". And it also will be pretty easy to document. WDYT?

@bebraw
Copy link
Member

bebraw commented Dec 12, 2020

I understand. Instead of type checking Webpack types though you could check for object/array. Regardless of the application (whether you merge webpack config or something else) you can limit the append/prepend to arrays only and merge to objects only. This way you'll have proper warnings/errors while avoiding things like "concat is not a function". And it also will be pretty easy to document. WDYT?

Yeah, I see what you mean now. I added simple type checks to guard against append/prepend/merge. I think that covers it well and now it's easier to notice if you are trying to merge types that the system doesn't expect.

If you notice missing checks, open separate issues with small test cases for them and I'll add logic based on that.

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

No branches or pull requests

2 participants