{"id":13396665,"url":"https://github.com/survivejs/webpack-merge","last_synced_at":"2025-05-12T13:32:46.867Z","repository":{"id":38237526,"uuid":"38106307","full_name":"survivejs/webpack-merge","owner":"survivejs","description":"Merge designed for webpack","archived":false,"fork":false,"pushed_at":"2024-07-12T06:35:58.000Z","size":1746,"stargazers_count":2688,"open_issues_count":6,"forks_count":116,"subscribers_count":17,"default_branch":"develop","last_synced_at":"2025-05-01T05:29:17.596Z","etag":null,"topics":["merging-loaders","webpack","webpack-specifics"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/webpack-merge","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/survivejs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":null,"patreon":null,"open_collective":"webpack-merge","ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2015-06-26T10:53:49.000Z","updated_at":"2025-04-24T06:32:51.000Z","dependencies_parsed_at":"2023-09-23T14:35:24.461Z","dependency_job_id":"a0e295c0-dc25-4296-bfd9-e9ba4ee21db4","html_url":"https://github.com/survivejs/webpack-merge","commit_stats":{"total_commits":523,"total_committers":36,"mean_commits":"14.527777777777779","dds":"0.15869980879541112","last_synced_commit":"400ba410bf881673d3d0d165b8ab74a68c3e7681"},"previous_names":[],"tags_count":94,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/survivejs%2Fwebpack-merge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/survivejs%2Fwebpack-merge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/survivejs%2Fwebpack-merge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/survivejs%2Fwebpack-merge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/survivejs","download_url":"https://codeload.github.com/survivejs/webpack-merge/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252299425,"owners_count":21725715,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["merging-loaders","webpack","webpack-specifics"],"created_at":"2024-07-30T18:00:59.358Z","updated_at":"2025-05-12T13:32:46.798Z","avatar_url":"https://github.com/survivejs.png","language":"TypeScript","readme":"[![Financial Contributors on Open Collective](https://opencollective.com/webpack-merge/all/badge.svg?label=financial+contributors)](https://opencollective.com/webpack-merge) [![Test](https://github.com/survivejs/webpack-merge/actions/workflows/test.yml/badge.svg?branch=develop\u0026event=push)](https://github.com/survivejs/webpack-merge/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/survivejs/webpack-merge/branch/master/graph/badge.svg)](https://codecov.io/gh/survivejs/webpack-merge)\n\n# webpack-merge - Merge designed for Webpack\n\n**webpack-merge** provides a `merge` function that concatenates arrays and merges objects creating a new object. If functions are encountered, it will execute them, run the results through the algorithm, and then wrap the returned values within a function again.\n\nThis behavior is particularly useful in configuring webpack although it has uses beyond it. Whenever you need to merge configuration objects, **webpack-merge** can come in handy.\n\n## **`merge(...configuration | [...configuration])`**\n\n`merge` is the core, and the most important idea, of the API. Often this is all you need unless you want further customization.\n\n```javascript\nconst { merge } = require('webpack-merge');\n\n// Default API\nconst output = merge(object1, object2, object3, ...);\n\n// You can pass an array of objects directly.\n// This works with all available functions.\nconst output = merge([object1, object2, object3]);\n\n// Keys matching to the right take precedence:\nconst output = merge(\n  { fruit: \"apple\", color: \"red\" },\n  { fruit: \"strawberries\" }\n);\nconsole.log(output);\n// { color: \"red\", fruit: \"strawberries\"}\n```\n\n### Limitations\n\nNote that `Promise`s are not supported! If you want to return a configuration wrapped within a `Promise`, `merge` inside one. Example: `Promise.resolve(merge({ ... }, { ... }))`.\n\nThe same goes for configuration level functions as in the example below:\n\n**webpack.config.js**\n\n```javascript\nconst commonConfig = { ... };\n\nconst productionConfig = { ... };\n\nconst developmentConfig = { ... };\n\nmodule.exports = (env, args) =\u003e {\n  switch(args.mode) {\n    case 'development':\n      return merge(commonConfig, developmentConfig);\n    case 'production':\n      return merge(commonConfig, productionConfig);\n    default:\n      throw new Error('No matching configuration was found!');\n  }\n}\n```\n\nYou can choose the configuration you want by using `webpack --mode development` assuming you are using _webpack-cli_.\n\n## **`mergeWithCustomize({ customizeArray, customizeObject })(...configuration | [...configuration])`**\n\nIn case you need more flexibility, `merge` behavior can be customized per field as below:\n\n```javascript\nconst { mergeWithCustomize } = require('webpack-merge');\n\nconst output = mergeWithCustomize(\n  {\n    customizeArray(a, b, key) {\n      if (key === 'extensions') {\n        return _.uniq([...a, ...b]);\n      }\n\n      // Fall back to default merging\n      return undefined;\n    },\n    customizeObject(a, b, key) {\n      if (key === 'module') {\n        // Custom merging\n        return _.merge({}, a, b);\n      }\n\n      // Fall back to default merging\n      return undefined;\n    }\n  }\n)(object1, object2, object3, ...);\n```\n\nFor example, if the previous code was invoked with only `object1` and `object2`\nwith `object1` as:\n\n```javascript\n{\n    foo1: ['object1'],\n    foo2: ['object1'],\n    bar1: { object1: {} },\n    bar2: { object1: {} },\n}\n```\n\nand `object2` as:\n\n```javascript\n{\n    foo1: ['object2'],\n    foo2: ['object2'],\n    bar1: { object2: {} },\n    bar2: { object2: {} },\n}\n```\n\nthen `customizeArray` will be invoked for each property of `Array` type, i.e:\n\n```javascript\ncustomizeArray([\"object1\"], [\"object2\"], \"foo1\");\ncustomizeArray([\"object1\"], [\"object2\"], \"foo2\");\n```\n\nand `customizeObject` will be invoked for each property of `Object` type, i.e:\n\n```javascript\ncustomizeObject({ object1: {} }, { object2: {} }, bar1);\ncustomizeObject({ object1: {} }, { object2: {} }, bar2);\n```\n\n## **`customizeArray`** and **`customizeObject`**\n\n`customizeArray` and `customizeObject` provide small strategies to for `mergeWithCustomize`. They support `append`, `prepend`, `replace`, and wildcards for field names.\n\n```javascript\nconst { mergeWithCustomize, customizeArray, customizeObject } = require('webpack-merge');\n\nconst output = mergeWithCustomize({\n  customizeArray: customizeArray({\n    'entry.*': 'prepend'\n  }),\n  customizeObject: customizeObject({\n    entry: 'prepend'\n  })\n})(object1, object2, object3, ...);\n```\n\n## **`unique(\u003cfield\u003e, \u003cfields\u003e, field =\u003e field)`**\n\n`unique` is a strategy used for forcing uniqueness within configuration. It's most useful with plugins when you want to make sure there's only one in place.\n\nThe first `\u003cfield\u003e` is the config property to look through for duplicates.\n\n`\u003cfields\u003e` represents the values that should be unique when you run the field =\u003e field function on each duplicate.\n\nWhen the order of elements of the `\u003cfield\u003e` in the first configuration differs from the order in the second configuration, the latter is preserved.\n\n```javascript\nconst { mergeWithCustomize, unique } = require(\"webpack-merge\");\n\nconst output = mergeWithCustomize({\n  customizeArray: unique(\n    \"plugins\",\n    [\"HotModuleReplacementPlugin\"],\n    (plugin) =\u003e plugin.constructor \u0026\u0026 plugin.constructor.name,\n  ),\n})(\n  {\n    plugins: [new webpack.HotModuleReplacementPlugin()],\n  },\n  {\n    plugins: [new webpack.HotModuleReplacementPlugin()],\n  },\n);\n\n// Output contains only single HotModuleReplacementPlugin now and it's\n// going to be the last plugin instance.\n```\n\n## **`mergeWithRules`**\n\nTo support advanced merging needs (i.e. merging within loaders), `mergeWithRules` includes additional syntax that allows you to match fields and apply strategies to match. Consider the full example below:\n\n```javascript\nconst a = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/,\n        use: [{ loader: \"style-loader\" }, { loader: \"sass-loader\" }],\n      },\n    ],\n  },\n};\nconst b = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/,\n        use: [\n          {\n            loader: \"style-loader\",\n            options: {\n              modules: true,\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\nconst result = {\n  module: {\n    rules: [\n      {\n        test: /\\.css$/,\n        use: [\n          {\n            loader: \"style-loader\",\n            options: {\n              modules: true,\n            },\n          },\n          { loader: \"sass-loader\" },\n        ],\n      },\n    ],\n  },\n};\n\nassert.deepStrictEqual(\n  mergeWithRules({\n    module: {\n      rules: {\n        test: \"match\",\n        use: {\n          loader: \"match\",\n          options: \"replace\",\n        },\n      },\n    },\n  })(a, b),\n  result,\n);\n```\n\nThe way it works is that you should annotate fields to match using `match` (or `CustomizeRule.Match` if you are using TypeScript) matching your configuration structure and then use specific strategies to define how particular fields should be transformed. If a match doesn't exist above a rule, then it will apply the rule automatically.\n\n**Supported annotations:**\n\n- `match` (`CustomizeRule.Match`) - Optional matcher that scopes merging behavior to a specific part based on similarity (think DOM or jQuery selectors)\n- `append` (`CustomizeRule.Append`) - Appends items\n- `prepend` (`CustomizeRule.Prepend`) - Prepends items\n- `replace` (`CustomizeRule.Replace`) - Replaces items\n- `merge` (`CustomizeRule.Merge`) - Merges objects (shallow merge)\n\n## Using with TypeScript\n\n**webpack-merge** supports TypeScript out of the box. You should pass `Configuration` type from webpack to it as follows:\n\n```typescript\nimport { Configuration } from \"webpack\";\nimport { merge } from \"webpack-merge\";\n\nconst config = merge\u003cConfiguration\u003e({...}, {...});\n\n...\n```\n\n## Development\n\n1. `nvm use`\n1. `npm i`\n1. `npm run build -- --watch` in one terminal\n1. `npm t -- --watch` in another one\n\nBefore contributing, please [open an issue](https://github.com/survivejs/webpack-merge/issues/new) where to discuss.\n\n## Further Information and Support\n\nCheck out [SurviveJS - Webpack 5](http://survivejs.com/) to dig deeper into webpack. The free book uses **webpack-merge** extensively and shows you how to compose your configuration to keep it maintainable.\n\nI am also available as a consultant in case you require specific assistance. I can contribute particularly in terms of improving maintainability of the setup while speeding it up and pointing out better practices. In addition to improving developer productivity, the work has impact on the end users of the product in terms of reduced application size and loading times.\n\n## Contributors\n\n### Code Contributors\n\nThis project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)].\n\u003ca href=\"https://github.com/survivejs/webpack-merge/graphs/contributors\"\u003e\u003cimg src=\"https://opencollective.com/webpack-merge/contributors.svg?width=890\u0026button=false\" /\u003e\u003c/a\u003e\n\n### Financial Contributors\n\nBecome a financial contributor and help us sustain our community. [[Contribute](https://opencollective.com/webpack-merge/contribute)]\n\n#### Individuals\n\n\u003ca href=\"https://opencollective.com/webpack-merge\"\u003e\u003cimg src=\"https://opencollective.com/webpack-merge/individuals.svg?width=890\"\u003e\u003c/a\u003e\n\n#### Organizations\n\nSupport this project with your organization. Your logo will show up here with a link to your website. [[Contribute](https://opencollective.com/webpack-merge/contribute)]\n\n\u003ca href=\"https://opencollective.com/webpack-merge/organization/0/website\"\u003e\u003cimg src=\"https://opencollective.com/webpack-merge/organization/0/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/webpack-merge/organization/1/website\"\u003e\u003cimg src=\"https://opencollective.com/webpack-merge/organization/1/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/webpack-merge/organization/2/website\"\u003e\u003cimg src=\"https://opencollective.com/webpack-merge/organization/2/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/webpack-merge/organization/3/website\"\u003e\u003cimg src=\"https://opencollective.com/webpack-merge/organization/3/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/webpack-merge/organization/4/website\"\u003e\u003cimg src=\"https://opencollective.com/webpack-merge/organization/4/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/webpack-merge/organization/5/website\"\u003e\u003cimg src=\"https://opencollective.com/webpack-merge/organization/5/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/webpack-merge/organization/6/website\"\u003e\u003cimg src=\"https://opencollective.com/webpack-merge/organization/6/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/webpack-merge/organization/7/website\"\u003e\u003cimg src=\"https://opencollective.com/webpack-merge/organization/7/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/webpack-merge/organization/8/website\"\u003e\u003cimg src=\"https://opencollective.com/webpack-merge/organization/8/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/webpack-merge/organization/9/website\"\u003e\u003cimg src=\"https://opencollective.com/webpack-merge/organization/9/avatar.svg\"\u003e\u003c/a\u003e\n\n## License\n\n**webpack-merge** is available under MIT. See LICENSE for more details.\n","funding_links":["https://opencollective.com/webpack-merge","https://opencollective.com/webpack-merge/contribute","https://opencollective.com/webpack-merge/organization/0/website","https://opencollective.com/webpack-merge/organization/1/website","https://opencollective.com/webpack-merge/organization/2/website","https://opencollective.com/webpack-merge/organization/3/website","https://opencollective.com/webpack-merge/organization/4/website","https://opencollective.com/webpack-merge/organization/5/website","https://opencollective.com/webpack-merge/organization/6/website","https://opencollective.com/webpack-merge/organization/7/website","https://opencollective.com/webpack-merge/organization/8/website","https://opencollective.com/webpack-merge/organization/9/website"],"categories":["webpack","TypeScript","JavaScript","打包工具"],"sub_categories":["webpack 辅助工具、Loader 和插件"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsurvivejs%2Fwebpack-merge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsurvivejs%2Fwebpack-merge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsurvivejs%2Fwebpack-merge/lists"}