Patterns d'exports et fichiers barrel

VérifiéSûr

Définit les conventions pour les exports de modules et les fichiers baril : exports nommés uniquement, pas de wildcards plats (sauf `export * as Name` pour les composants composés), commentaires par section, types exportés avec les valeurs, et une ligne d'export par fichier source. Aide à maintenir des exports cohérents et optimisés pour le tree-shaking dans les applications portal utilisant Turbopack ou des bundlers similaires.

Spar Skills Guide Bot
DeveloppementIntermédiaire
13002/06/2026
Claude CodeCursorWindsurfCopilotCodex
#exports#barrel-files#named-exports#tree-shaking#module-organization

Recommandé pour

Notre avis

Définit des conventions pour les exports de modules et les fichiers barrel dans des projets TypeScript, en mettant l'accent sur les exports nommés et en évitant les ré-exports par wildcard pour un meilleur tree-shaking.

Points forts

  • Optimise le tree-shaking en utilisant des exports nommés.
  • Structure claire des fichiers barrel avec des commentaires de section.
  • Autorise les exports par namespace pour les composants composés comme exception.
  • Assure une cohérence dans toute la base de code.

Limites

  • Nécessite une discipline pour maintenir l'ordre alphabétique dans les sections.
  • Peut être trop strict pour de petits projets où les wildcards sont inoffensifs.
  • Non applicable aux projets utilisant CommonJS ou des bundlers anciens.
Quand l'utiliser

À utiliser lors de l'organisation d'un grand projet TypeScript avec plusieurs modules pour garantir un bundling efficace et des interfaces d'export claires.

Quand l'éviter

Éviter pour les petits projets ou lors de prototypage rapide où la surcharge de maintenance des fichiers barrel n'est pas justifiée.

Analyse de sécurité

Sûr
Score qualité92/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.

Aucun point d'attention détecté

Exemples

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
Skills similaires