Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

merge-json-webpack-plugin

sibiraj-s2.5kMIT6.2.0TypeScript support: included

Webpack plugin to merge multiple json files into one

json-merge, merge-json-webpack-plugin, webpack-plugin

readme

merge-json-webpack-plugin

Tests License Version Node Version Webpack Version

Webpack plugin to merge multiple json files into one

Getting Started

Installation

npm i -D merge-json-webpack-plugin
# or
yarn add --dev merge-json-webpack-plugin

Usage

// webpack.config.js
const MergeJsonPlugin = require('merge-json-webpack-plugin');

module.exports = {
  plugins: [
    new MergeJsonPlugin({
      force: false,
      groups: [
        {
          files: [
            'common-manifest.json',
            'firefox-manifest.json'
          ],
          transform: (outputJson) => outputJson,
          to: 'manifest.json',
        },
        {
          pattern: '*.json', // glob. see https://github.com/mrmlnc/fast-glob
          to: 'merged-[contenthash].json',
        },
      ],
    }),
  ],
};

Options

  • cwd[string] - The directory, an absolute path, for resolving files. Defaults to webpack context

  • groups[array] - Files to merge and destination path

    • files[string[]] - Array of files, path resolved relative to cwd.
    • pattern[string[]] or [string] - [Fast-Glob] pattern matching. The order of merge is not guarenteed.
    • transform[function] - A function to modify the merged json contents. The return json value is written to the output file. If the function returns a promise, it will be awaited.
    • to[string]: Destination path to write the files to.
    • globOptions[GlobOptions] - Options to foward to fast-glob when glob is used otherwise ignored. See https://github.com/mrmlnc/fast-glob#options-3.
  • minify[boolean] - Minify the output json. Enabled by default in production mode.

  • mergeFn[function] - A function used to merge two objects. Defaults to Object.assign.

// webpack.config.js
const MergeJsonPlugin = require('merge-json-webpack-plugin');
const _ = require('loadsh');

const customizer = (objValue, srcValue) => {
  if (_.isArray(objValue)) {
    return objValue.concat(srcValue);
  }
};

const merge = (object, other) => {
  return _.mergeWith(object, other, customizer);
};

module.exports = {
  plugins: [
    new MergeJsonPlugin({
      mergeFn: merge,
    }),
    new MergeJsonPlugin({
      mergeFn: (prev, current) => Object.assign(prev, current),
    }),
  ],
};
  • force[boolean] - Overwrites files already in compilation.assets (usually added by other plugins/loaders). Disabled by default.

  • globOptions[GlobOptions] - Options to foward to fast-glob when glob is used otherwise ignored. See https://github.com/mrmlnc/fast-glob#options-3.

changelog

CHANGELOG

All notable changes to this project will be documented in this file.

Tags

  • Features
  • Bug Fixes
  • Performance Improvements
  • Enhancements
  • Dependency Updates
  • Breaking Changes
  • Documentation
  • Internal
  • Refactor
  • Unreleased

v6.2.0 (2023-07-01)

Features

  • add transformFile option (8656c58)

Bug Fixes

  • fix use array pattern for glob ignore (da8de5b)

Internal

v6.1.0 (2021-08-18)

Bug Fixes

Dependency Updates

  • update schema-utils to v4 (e6e3b4c)

v6.0.0 (2021-08-18)

Features

  • support array of globs via groups[]pattern (8ffbb15)

Dependency Updates

  • update fast-glob to v3.2.7 (b25ab9b)
  • update schema-utils to v3.1.1 (b25ab9b)

Breaking Changes

  • renamed group to groups (709351d)
  • groups[x]files no longer support glob, use pattern instead (8ffbb15)
  • drop nodejs v10, minimum required version nodejs >=12.20.0 (e214a52)

v5.1.0 (2021-03-06)

Dependency Updates

  • update fast-glob to v3.2.5 (63fcaf9)

Refactor

  • drop loader-utils dependency (27d95e3)
  • use modules provided by compiler (7ec61db)

v5.0.3 (2020-12-14)

Internal

v5.0.2 (2020-12-01)

Bug Fixes

  • update glob ignore pattern (2ffff1f)

v5.0.1 (2020-11-10)

Bug Fixes

  • allow concating arrays in json (32d2d8c)

v5.0.0 (2020-11-09)

Refactor

  • prefer processAssets hook (17df72a)

Breaking Changes

  • rename beforeEmit option to transform (b4ed32d)

v4.2.1 (2020-10-29)

Bug Fixes

v4.2.0 (2020-10-21)

Features

  • add force option to overwrites files already in compilation.assets (7a31a42)
  • add immutable and minimized properties to output asset info (c61e2c0)

Bug Fixes

  • don't overrite files already in compilation.assets (7a31a42)

v4.1.0 (2020-10-19)

Features

  • support outputname interpolation (112cb45)

v4.0.0 (2020-10-11)

Features

Enhancements

  • remove webpack-sources dependency (a59e062)

Breaking Changes

  • drop webpack 4 support

v3.0.0 (2020-10-06)

Dependency Updates

  • update schema-utils to v3 (39f2164)
  • update devDependencies (7479e2b)

Breaking Changes

  • minimum required Node.js version is v10.13.0
  • minimum webpack version is v4.40.0

v2.0.0 (2020-10-04)

Enhancements

Dependency Updates

Breaking Changes

  • rename option root to cwd (e23f210)

v1.2.0 (2020-07-19)

Features

v1.1.1 (2020-07-14)

Bug Fixes

  • use correct json returned from beforeEmit function (87ad4da)

v1.1.0 (2020-07-14)

Features

v1.0.0 (2020-07-13)

  • Initial Release: Webpack plugin to merge multiple json files into one