Skip to content

barrels

Tools for creating barrel files.

Features

This library exposes a set of utility functions which:

  • Collect files and directories
  • Extract export declarations from files using the Oxidation Compiler toolchain
    • Supports all export declaration types
    • Supports detecting type declarations
    • Supports detection and handling of external dependencies and built-ins
    • Supports various tsconfig settings: paths, references, moduleResolution, allowImportingTsExtensions
    • Supports monorepo setups
  • Handle file extensions
    • Manually remove or remap extensions
    • Automatic handling by considering tsconfig settings
  • Convert sources to export/import declarations
  • CLI
    • Create barrels via a classic config file (barrels.config.*)
    • Queue-based watch mode to keep things up to date
    • Config hot-reloading in watch mode
    • Gitignore integration
    • Infinite loop prevention when barrel files are modified
    • Pretty output with bordered visualization of generated barrel contents

Installation

sh
npm install -D @monstermann/barrels
sh
pnpm add -D @monstermann/barrels
sh
yarn add -D @monstermann/barrels
sh
bun add -D @monstermann/barrels

Setup

barrels.config.ts
ts
import { defineConfig } from "@monstermann/barrels";

export default defineConfig([
    () => {
        // See API/Guide for how to create barrel files,
        // or use one of the presets further below!
    },
]);

Usage

sh
npx barrels
npx barrels --watch
npx barrels --help
sh
pnpm exec barrels
pnpm exec barrels --watch
pnpm exec barrels --help
sh
yarn barrels
yarn barrels --watch
yarn barrels --help
sh
bun run barrels
bun run barrels --watch
bun run barrels --help

barrels-flat

This preset collects files and creates classic flat barrels, for example:

ts
export * from "./foo";
export * from "./bar";

Installation

sh
npm install -D @monstermann/barrels-flat
sh
pnpm add -D @monstermann/barrels-flat
sh
yarn add -D @monstermann/barrels-flat
sh
bun add -D @monstermann/barrels-flat

Setup

barrels.config.ts
ts
import { defineConfig } from "@monstermann/barrels";
import { flat } from "@monstermann/barrels-flat";

export default defineConfig([
    flat(options)
]);
Options
ts
interface FlatBarrelConfig {
    // Glob(s) to directories to create barrel files in.
    // Default: process.cwd()
    entries?: string | string[];
    // Glob(s) to collect files, relative to each entry.
    // Default: *.ts
    include?:
        | string
        | string[]
        | ((ctx: { entry: string; outDir: string }) => string | string[]);
    // Glob(s) or RegExp(s) to exclude matched files.
    // Default: undefined
    exclude?: string | string[] | RegExp | RegExp[];
    // The directory where the barrel files should be created.
    // Will be resolved against the current entry.
    // Default: entry
    outDir?: string | ((ctx: { entry: string }) => string);
    // The file name used to create the barrel file within `outDir`.
    // Default: index.ts
    outFile?: string | ((ctx: { entry: string; outDir: string }) => string);
}

barrels-namespace

This preset collects files and creates TypeScript namespace barrels, for example:

ts
import { foo } from "./foo.js";
import { bar } from "./bar.js";

declare namespace Example {
    export { foo, bar };
}

export { Example };
ts
import { foo } from "./foo.js";
import { bar } from "./bar.js";

export const Example = {
    foo,
    bar,
};

Installation

sh
npm install -D @monstermann/barrels-namespace
sh
pnpm add -D @monstermann/barrels-namespace
sh
yarn add -D @monstermann/barrels-namespace
sh
bun add -D @monstermann/barrels-namespace

Setup

barrels.config.ts
ts
import { defineConfig } from "@monstermann/barrels";
import { namespace } from "@monstermann/barrels-namespace";

export default defineConfig([
    namespace(options)
]);
Options
ts
interface NamespaceBarrelConfig {
    // Glob(s) to directories to create barrel files in.
    // Default: process.cwd()
    entries?: string | string[];
    // Glob(s) to collect files, relative to each entry.
    // Default: *.ts
    include?:
        | string
        | string[]
        | ((ctx: {
              entry: string;
              outDir: string;
              title: string;
          }) => string | string[]);
    // Glob(s) or RegExp(s) to exclude matched files.
    // Default: undefined
    exclude?: string | string[] | RegExp | RegExp[];
    // The title of the namespace.
    // Default: Path.basename(entry)
    title?: string | ((ctx: { entry: string }) => string);
    // The directory where the barrel files should be created.
    // Will be resolved against the current entry.
    // Default: entry
    outDir?: string | ((ctx: { entry: string; title: string }) => string);
    // The name without extension that should be used to
    // create the `.d.ts` and `.js` files.
    // Default: outDir === entry ? "index" : title
    outName?:
        | string
        | ((ctx: { entry: string; outDir: string; title: string }) => string);
}

Tree-shaking

If you would like to use the benefits of eg. TypeScript namespace barrels without losing tree-shaking, you can give tree-shake-import-namespaces a try!

Credits