Exports & Barrel Files

VerifiedSafe

Defines conventions for module exports and barrel files: named exports only, no flat wildcards (except `export * as Name` for compound components), sectioned comments, types alongside values, and one export line per source file. Helps maintain consistent, tree-shakeable exports in portal apps using Turbopack or similar bundlers.

Sby Skills Guide Bot
DevelopmentIntermediate
1406/2/2026
Claude CodeCursorWindsurfCopilotCodex
#exports#barrel-files#named-exports#tree-shaking#module-organization

Recommended for

Our review

Defines conventions for module exports and barrel files in TypeScript projects, emphasizing named exports and avoiding wildcard re-exports for better tree-shaking.

Strengths

  • Optimizes tree-shaking by using named exports.
  • Clear barrel file structure with section comments.
  • Allows namespace exports for compound components as an exception.
  • Enforces consistency across the codebase.

Limitations

  • Requires discipline to maintain alphabetical order within sections.
  • May be overly strict for small projects where wildcard exports are harmless.
  • Not applicable to projects using CommonJS or older bundlers.
When to use it

Use when organizing a large TypeScript project with multiple modules to ensure efficient bundling and clear export interfaces.

When not to use it

Avoid for small projects or when rapid prototyping where the overhead of maintaining barrel files is not justified.

Security analysis

Safe
Quality score92/100

This skill is a static style guide for TypeScript export patterns. It contains no executable commands, network operations, or file system modifications. There is no risk of executing harmful actions.

No concerns found

Examples

Create a barrel file for components
Create a barrel index.ts file in the components directory following the named exports convention. Group exports by sections: Components, Hooks, Types. Each export should be on its own line, sorted alphabetically within sections. Do not use wildcard exports. If there are compound components like Dialog or Menu, use export * as Name for them.
Refactor default exports to named exports
Refactor the barrel file at src/ui/index.ts to remove all default exports and replace them with named exports. Ensure that source files use named exports instead of default exports. Also replace any export * from wildcards with explicit named exports. Maintain section comments and alphabetical order.
Add a new component export
Add an export for a new component named 'Button' with its props type 'ButtonProps' in the existing barrel file at src/components/index.ts. Insert it in the Components section in alphabetical order.

name: exports description: Export patterns and barrel file conventions. Use when creating/modifying index.ts files or organizing module exports.

Exports & Barrel Files

Standards for module exports and barrel files in the portal app.

Core Principle

Named exports only. No flat wildcards. No default exports in barrels.

Turbopack and modern bundlers handle named exports better for tree-shaking. Flat wildcard export * forces bundlers to include everything and breaks cache invalidation.

Exception: export * as Name is allowed for compound components (Dialog, Menu, etc.) to create namespaces.

Barrel File Structure

Every barrel file follows this structure:

/* eslint-disable */

// Section Name

export { Thing, type ThingProps } from "./thing";
export { Other } from "./other";

// Another Section

export { More } from "./more";

Rules

  1. ESLint disable at top - Barrel files intentionally re-export
  2. Section comments - Group related exports with // Section Name
  3. Alphabetical within sections - Sort exports alphabetically within each group
  4. Types with values - Export types alongside their values: export { Foo, type FooProps }
  5. One line per file - Each source file gets one export line

Section Naming

Use these section names consistently:

| Section | Contents | | ------------ | --------------------------------- | | Components | React components | | Hooks | Custom React hooks | | Types | Type-only exports | | Constants | Const values, enums, config | | Utilities | Pure functions | | Context | React context providers/consumers | | Mutations | Data mutation hooks (React Query) | | Queries | Data fetching hooks (React Query) | | Store | Zustand/state stores |

Examples

Component Barrel

/* eslint-disable */

// Components

export { ActionCard } from "./action-card";
export { ActionList } from "./action-list";
export { ActionPicker } from "./action-picker";

State/Domain Barrel

/* eslint-disable */

// Store

export { useNodesSelection, useNodesSelectionArray } from "./store";

// Types

export {
  type NodeOwner,
  type NodePermissionRow,
  type NodeRow,
  type NodeWithMeta,
  transformNode,
} from "./types";

UI Library Barrel

For compound components (Dialog, Menu, etc.), export as namespaces using export * as:

/* eslint-disable */

// Simple Components

export { Badge, type BadgeProps } from "./badge";
export { Button, type ButtonProps } from "./button";
export { Input, type InputProps } from "./input";

// Compound Components (namespace exports)

export * as Accordion from "./accordion";
export * as Alert from "./alert";
export * as Dialog from "./dialog";
export * as Menu from "./menu";

// Layout

export { AbsoluteCenter } from "./absolute-center";
export { Group } from "./group";

Note: Compound components use export * as Name to create namespaces (e.g., Dialog.Root, Dialog.Content). This is the ONE exception to the "no wildcards" rule - it creates a namespace, not a flat re-export.

What NOT to Do

No Flat Wildcards

// WRONG - flat wildcard re-exports everything
export * from "./button";
export * from "./card";

// RIGHT - named exports
export { Button, type ButtonProps } from "./button";

// RIGHT - namespace export for compound components
export * as Card from "./card";

No Default Exports in Barrels

// WRONG
export { default as Button } from "./button";

// RIGHT - source file should use named export
export { Button } from "./button";

No Aliasing (Unless Necessary)

// WRONG - unnecessary alias
export { Button as ButtonComponent } from "./button";

// RIGHT
export { Button } from "./button";

// OK - resolving actual name conflict
export { ActionList as ActionListComponent } from "./action-list";

No Mixed Concerns

// WRONG - implementations don't belong in barrel
export { Button } from "./button";
export const DEFAULT_SIZE = "md"; // NO - this belongs in a constants file

// RIGHT - barrel only re-exports
export { Button } from "./button";
export { DEFAULT_SIZE } from "./constants";

When to Create a Barrel

Always. Every directory with exportable code gets an index.ts.

Index Files Are ONLY Barrels

An index.ts file must ONLY contain re-exports. No implementations.

If you find an index.ts with actual code (functions, constants, types defined inline), split it up:

  1. Move implementations to separate files (e.g., utils.ts, types.ts, constants.ts)
  2. Make index.ts a pure barrel that re-exports from those files
// WRONG - index.ts with implementation
export const FOO = "bar";
export function doThing() { ... }
export interface MyType { ... }

// RIGHT - index.ts as pure barrel
export { FOO } from "./constants";
export { doThing } from "./utils";
export type { MyType } from "./types";

Import Conventions

When importing from barrels:

// GOOD - import from barrel
import { Button, Card, Dialog } from "@/components/ui";
import { useNodes, useNodeMutations } from "@/lib/state";

// AVOID - bypassing barrel for public exports
import { Button } from "@/components/ui/button";

// OK - importing internal/non-exported utilities
import { internalHelper } from "@/components/ui/button/helpers";

Instructions

When creating or modifying barrel files:

  1. Add /* eslint-disable */ at top
  2. Group exports into logical sections with // Section Name
  3. Use named exports only - no flat export * (exception: export * as Name for compound component namespaces)
  4. Sort exports alphabetically within sections
  5. Include types with their values on same line
  6. One export statement per source file
Related skills