rollup-plugin-flatten-dir
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Flatten Dir

This Rollup plugin exports the contents of a given directory into a Record<string, string> object. The property keys are the relative paths of the files, and the values are the raw contents of the files.

Installation

To use the flatten-dir plugin, first install it using npm:

npm install rollup-plugin-flatten-dir # or pnpm, yarn, bun ...

Usage

Add the plugin to your rollup/vite config file:

import flattenDirPlugin from "rollup-plugin-flatten-dir"

export default {
  // Your other configuration options...
  plugins: [flattenDirPlugin()],
}

Plugin Options

  • include: A pattern or array of patterns that specify exactly what file paths to include. Omitted or an empty array means include all files by default.

  • exclude: A pattern or array of patterns that specify exactly what file paths to exclude.

  • resolve: Optional. An optional base directory to resolve patterns against. If a string is specified, the value will be used as the base directory. Relative paths will be resolved against process.cwd() first. If false, patterns will not be resolved against any directory.

How it Works

The flatten-dir plugin works by:

  1. Resolving the directory path to be flattened.
  2. Reading the contents of the directory and its subdirectories.
  3. Filtering files according to include and exclude patterns.
  4. Creating an export object with file names as keys and their contents as values.
  5. Optionally generating a index.d.ts TypeScript declaration file for type checking.

Example

Given a src/data directory with JSON files, the plugin can be used to create a single entry file that exposes them:

src/
├── foo/
│   ├── bar/
│   │   ├── a.json
│   │   └── b.txt
│   └── c.js

Then import files from "path/to/src" will get a files object. This is equivalent to having an index.js with the following content:

export default {
  "foo/bar/a.json": "{}",
  "foo/bar/b.txt": "...",
  "foo/c.js": "",
}

Package Sidebar

Install

npm i rollup-plugin-flatten-dir

Weekly Downloads

141

Version

1.0.1

License

MIT

Unpacked Size

9.23 kB

Total Files

5

Last publish

Collaborators

  • muspi-merol