rollup-plugin-flatten-dir

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

Rollup and Vite plugin that flattens a directory's file contents into a `Record<string, string>` object. Version 1.0.1 is the current stable release, with no active development observable. It resolves a directory, reads file contents, filters by include/exclude glob patterns, and generates an ESM module with a default export mapping relative file paths to their raw string contents. Optionally generates TypeScript declaration files. Differentiates from similar plugins by focusing on raw string contents rather than module transforms.

error Error: Could not resolve 'rollup-plugin-flatten-dir' from '...'
cause Package not installed or not in node_modules.
fix
Run npm install rollup-plugin-flatten-dir
error TypeError: flattenDirPlugin is not a function
cause Importing as a named export instead of default.
fix
Change import { flattenDirPlugin } from ... to import flattenDirPlugin from ...
error Error: ENOENT: no such file or directory, scandir '...'
cause The resolve directory does not exist.
fix
Ensure the directory specified in resolve exists and is accessible.
gotcha The plugin does not automatically exclude node_modules; you must add an exclude pattern or use a separate plugin to avoid bundling entire node_modules.
fix Add exclude: ['**/node_modules/**'] to plugin options.
gotcha If no include pattern is provided, all files (including binary files) are read as UTF-8 text, which may cause runtime errors for non-text files.
fix Use include or exclude patterns to limit to text files (e.g., include: ['**/*.json', '**/*.txt']).
gotcha The resolve option with a string resolves relative to process.cwd(); using false disables resolution, which may cause confusing absolute paths.
fix Always provide an absolute path or a relative path from process.cwd() to avoid unexpected behavior.
breaking Version 1.0.0 changed the default export from a named object to a function. Existing code using import { flatten } from 'rollup-plugin-flatten-dir' will break.
fix Use default import: import flattenDirPlugin from 'rollup-plugin-flatten-dir'
npm install rollup-plugin-flatten-dir
yarn add rollup-plugin-flatten-dir
pnpm add rollup-plugin-flatten-dir

Basic Rollup configuration using flattenDirPlugin to include JSON files from src/data, excluding node_modules.

// rollup.config.js
import flattenDirPlugin from 'rollup-plugin-flatten-dir'

export default {
  input: 'src/index.js',
  output: { file: 'dist/bundle.js', format: 'esm' },
  plugins: [flattenDirPlugin({
    include: ['**/*.json'],
    exclude: ['**/node_modules/**'],
    resolve: 'src/data'
  })]
}