flat
interface FlatBarrelConfig {
type: "flat";
banner?: boolean | string;
directories?: string | string[];
files: string | string[];
include?: string | RegExp | readonly (string | RegExp)[];
exclude?: string | RegExp | readonly (string | RegExp)[];
output?: string | ((directory: string) => string);
extensions?: "auto" | false | Record<string, string>;
resolveExports?: boolean;
transformSources?: (sources: Source[]) => Source[] | Promise<Source[]>;
transformBarrel?: (barrel: string) => string | Promise<string>;
}
This config creates classic barrel files, re-exporting all sources as-is.
Example
import { defineConfig } from "@monstermann/barrels";
export default defineConfig([
{
type: "flat",
files: "./src/utils/*.ts",
output: "./src/utils/index.ts",
removeExtensions: true,
},
]);
// Generated by @monstermann/barrels
/* eslint-disable */
/* prettier-ignore */
// dprint-ignore-file
// biome-ignore lint: disable
export * from "./foo";
export * from "./bar";
export * from "./baz";
Options
banner
interface Config {
banner?: boolean | string;
}
When enabled (as is by default), prepends a banner:
// Generated by @monstermann/barrels
/* eslint-disable */
/* prettier-ignore */
// dprint-ignore-file
// biome-ignore lint: disable
You can turn this off by setting it to false
:
export default defineConfig([
{
banner: false,
},
]);
Or define your own:
export default defineConfig([
{
banner: "// My custom banner",
},
]);
directories
interface Config {
directories?: string | string[];
}
Paths or globs to directories to create the barrel files in, by default set to process.cwd()
.
export default defineConfig([
{
// For each directory in "./src/utils/":
directories: "./src/utils/*",
// Find all immediate ".ts" files:
files: "*.ts",
// And create an "index.ts" in the respective directory:
output: "index.ts",
},
]);
files
interface Config {
files: string | string[];
}
Paths or globs to include files from, relative to each directory.
include
interface Config {
include?: string | RegExp | readonly (string | RegExp)[];
}
RegExps or globs to quickly filter files. The checks are made against resolved absolute paths.
exclude
interface Config {
exclude?: string | RegExp | readonly (string | RegExp)[];
}
RegExps or globs to quickly exclude files. The checks are made against resolved absolute paths, for example to kick out all external dependencies:
export * from "external-dependency";
export const foo = true;
export default defineConfig([
{
type: "flat",
files: "./source.ts",
output: "./index.ts",
// Extract all exports from "source.ts":
resolveExports: true,
// Kick out re-exported modules coming from node_modules:
exclude: "**/node_modules/**",
},
]);
export { foo } from "./source.ts";
output
interface Config {
output?: string | ((directory: string) => string);
}
The path to the file the barrel contents should be written to, relative to each directory, index.ts
by default.
extensions
interface Config {
extensions?: "auto" | false | Record<string, string>;
}
Controls how file extensions are handled when building import/export declarations, by default "auto"
.
export default defineConfig([
{
// Remove all extensions:
extensions: false,
// Remap specific extensions:
extensions: {
ts: "js",
},
// Decide based on `tsconfig` settings:
// If `allowImportingTsExtensions` is turned on, leaves extensions as-is.
// If `moduleResolution` is set to `bundler`, removes all extensions.
// Otherwise eg. `ts` gets remapped to `js`.
extensions: "auto",
},
]);
removeExtensions
interface Config {
removeExtensions?: boolean;
}
This allows you to remove all extensions, useful when tsconfig.compilerOptions.moduleResolution
is set to bundler
.
resolveExports
interface Config {
resolveExports?: boolean;
}
Enabled by default when using { type: "namespace" }
or { type: "record" }
.
When enabled, will recursively extract individual export declarations from encountered files. This is necessary for some barrel types, but you can also use this to control what exactly is being exported instead of re-exporting all encountered files as export * from "file.ts"
.
export const foo = true;
Before:
export default defineConfig([
{
type: "flat",
files: "./source.ts",
output: "./index.ts",
},
]);
export * from "./source.ts";
After:
export default defineConfig([
{
type: "flat",
files: "./source.ts",
output: "./index.ts",
resolveExports: true,
},
]);
export { foo } from "./source.ts";
transformSources
interface Config {
transformSources?: (sources: Source[]) => Source[] | Promise<Source[]>;
}
This allows you to modify the encountered files or exports before constructing barrel files.
See API/Guide
for an overview!
transformBarrel
interface Config {
transformBarrel?: (barrel: string) => string | Promise<string>;
}
This allows you to modify the created barrel contents before writing to disk.