rollup-plugin-json-combine

raw JSON →
1.0.7 verified Mon Apr 27 auth: no javascript

Rollup plugin that combines multiple JSON files from a directory into a single output file. Current stable version is 1.0.7, with no release cadence documented. It differs from other JSON bundling plugins by providing a custom merge callback, allowing users to define how individual file contents are merged. Requires Node ^14.18.0 or >=16. Ships TypeScript types for better developer experience.

error Error: No files matched for input pattern: path/*.json
cause Input pattern does not match any files or the path is incorrect relative to the project root.
fix
Verify the pattern is correct and files exist. Use absolute paths or correct relative paths from process.cwd().
error TypeError: items.forEach is not a function
cause The merge callback is called with undefined or non-array data, possibly because input pattern matched no files.
fix
Ensure at least one JSON file matches the input pattern. Add error handling in the merge callback.
error Error: [plugin: json-combine] Cannot read properties of undefined (reading 'forEach')
cause Same as above; merge callback receives undefined when no files are matched.
fix
Check the input pattern and file existence.
gotcha The merge callback receives items as an array of { name, res } where 'res' is a string (raw content), not a parsed JSON object.
fix If you need the parsed value, call JSON.parse(item.res) inside the merge callback.
breaking Input option must be an array of glob patterns. Providing a single string (e.g., 'path/*.json') is not supported and may cause no files to be matched.
fix Always pass an array: ['path/*.json']
gotcha The fileName option is required and must be relative to your Rollup output directory, not the project root.
fix Ensure the path in fileName is correct relative to your output directory. For example, if output.file is 'dist/bundle.js', use 'dist/locales.json'.
deprecated No known deprecations in current version.
npm install rollup-plugin-json-combine
yarn add rollup-plugin-json-combine
pnpm add rollup-plugin-json-combine

Combines all JSON files in locales/ into a single dist/locales.json with keys derived from file names.

import jsonCombine from 'rollup-plugin-json-combine';

export default {
  input: 'src/index.js',
  output: {
    file: 'dist/bundle.js',
    format: 'esm'
  },
  plugins: [
    jsonCombine({
      input: ['locales/*.json'],
      fileName: 'dist/locales.json',
      merge: (items) => {
        const output = {};
        items.forEach((item) => {
          output[item.name.replace('.json', '')] = item.res;
        });
        return output;
      },
    }),
  ],
};