record
interface RecordBarrelConfig {
type: "record";
name?: string | ((filePath: string) => string);
indentation?: number;
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 record barrel files.
Example
import { defineConfig } from "@monstermann/barrels";
export default defineConfig([
{
type: "record",
name: "Barrel",
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
import { foo } from "./foo"
import { bar } from "./bar";
import { baz } from "./baz";
export const Barrel = {
foo,
bar,
baz,
};
Options
name
interface RecordBarrelConfig {
name?: string | ((filePath: string) => string);
}
Defines the name of the namespace. If omitted, the output file name will be used, or the output directory name if the file name was index
.
indentation
interface RecordBarrelConfig {
indentation?: number;
}
Controls how many spaces to use for indenting, by default 4
.
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.