CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-storybook--core-common

Deprecated compatibility shim for Storybook's framework-agnostic API utilities

Pending
Overview
Eval results
Files

presets.mddocs/

Preset System

Powerful preset loading and application system for managing Storybook addons and framework configurations. The preset system allows for modular, composable configuration that can be shared across projects.

Capabilities

Core Preset Functions

Create and manage preset application interfaces for dynamic configuration management.

/**
 * Create preset application interface from preset configurations
 * @param presets - Array of preset configurations
 * @param storybookOptions - Storybook options for preset context
 * @returns Promise resolving to Presets interface
 */
function getPresets(
  presets: PresetConfig[],
  storybookOptions: InterPresetOptions
): Promise<Presets>;

/**
 * Load and merge all preset configurations including core, custom, and override presets
 * @param options - Preset loading options
 * @returns Promise resolving to merged Presets interface
 */
function loadAllPresets(options: {
  corePresets?: PresetConfig[];
  overridePresets?: PresetConfig[];
  configDir: string;
  isCritical?: boolean;
  build?: TestBuildConfig;
}): Promise<Presets>;

interface Presets {
  /**
   * Apply preset extension to configuration
   * @param extension - Extension name (e.g., 'babel', 'webpack', 'stories')
   * @param config - Current configuration to extend
   * @param args - Additional arguments for preset functions
   * @returns Promise resolving to extended configuration
   */
  apply<T>(extension: string, config?: T, args?: unknown): Promise<T>;
}

Usage Example:

import { loadAllPresets } from "@storybook/core-common";

// Load all presets for a Storybook project
const presets = await loadAllPresets({
  configDir: '.storybook',
  corePresets: ['@storybook/core-server/presets/common-preset'],
  overridePresets: []
});

// Apply babel preset
const babelConfig = await presets.apply('babel', {}, { configType: 'DEVELOPMENT' });

// Apply stories preset
const stories = await presets.apply('stories', [], {});

Individual Preset Loading

Load and process individual preset configurations.

/**
 * Load individual preset configuration
 * @param input - Preset configuration (string or object)
 * @param level - Nesting level for recursive loading
 * @param storybookOptions - Storybook context options
 * @returns Promise resolving to array of loaded presets
 */
function loadPreset(
  input: PresetConfig,
  level: number,
  storybookOptions: InterPresetOptions
): Promise<LoadedPreset[]>;

interface LoadedPreset {
  name: string;
  preset: any;
  options: any;
}

Addon Resolution

Resolve addon names to preset or virtual addon configurations.

/**
 * Resolve addon name to preset or virtual addon configuration
 * @param configDir - Configuration directory for resolution context
 * @param name - Addon name or path
 * @param options - Addon-specific options
 * @returns Resolved addon configuration or undefined if not found
 */
function resolveAddonName(
  configDir: string,
  name: string,
  options: any
): CoreCommon_ResolvedAddonPreset | CoreCommon_ResolvedAddonVirtual | undefined;

interface CoreCommon_ResolvedAddonPreset {
  type: 'presets';
  name: string;
}

interface CoreCommon_ResolvedAddonVirtual {
  type: 'virtual';
  name: string;
  managerEntries?: string[];
  previewAnnotations?: PreviewAnnotation[];
  presets?: (string | { name: string; options?: any })[];
}

Usage Example:

import { resolveAddonName } from "@storybook/core-common";

// Resolve addon with manager entry
const essentialsAddon = resolveAddonName(
  '.storybook',
  '@storybook/addon-essentials',
  {}
);

// Resolve addon with custom manager
const customAddon = resolveAddonName(
  '.storybook', 
  '@storybook/addon-docs/manager',
  { theme: 'dark' }
);

console.log(essentialsAddon);
// { type: 'presets', name: '/path/to/@storybook/addon-essentials/preset' }

Preset Filtering

Filter and process preset configurations.

/**
 * Filter preset configurations, removing deprecated presets
 * @param presetsConfig - Array of preset configurations
 * @returns Filtered preset configurations
 */
function filterPresetsConfig(presetsConfig: PresetConfig[]): PresetConfig[];

Custom Preset Loading

Load custom presets from main configuration.

/**
 * Load custom presets from main configuration
 * @param options - Options containing main config and preset context
 * @returns Array of custom preset configurations
 */
function loadCustomPresets(options: {
  configDir: string;
  presets?: Presets;
}): PresetConfig[];

Preset Configuration Types

/**
 * Preset configuration - can be string path or object with options
 */
type PresetConfig = string | {
  name: string;
  options?: unknown;
};

/**
 * Preset definition - string path or object with name and options
 */
type Preset = string | {
  name: string;
  options?: any;
};

/**
 * Preview annotation configuration for virtual addons
 */
type PreviewAnnotation = string | {
  bare: string;
  absolute: string;
};

Advanced Usage

Creating Custom Presets

// Custom preset file structure
module.exports = {
  // Extend babel configuration
  babel: (config, { configType }) => ({
    ...config,
    plugins: [
      ...config.plugins,
      configType === 'DEVELOPMENT' && 'babel-plugin-dev-tools'
    ].filter(Boolean)
  }),

  // Add webpack configuration
  webpackFinal: (config, { configType }) => {
    if (configType === 'DEVELOPMENT') {
      config.devtool = 'eval-source-map';
    }
    return config;
  },

  // Define stories
  stories: (entries = []) => [
    ...entries,
    '../src/**/*.stories.@(js|jsx|ts|tsx)'
  ],

  // Add addons
  addons: [
    '@storybook/addon-essentials',
    {
      name: '@storybook/addon-docs',
      options: { transcludeMarkdown: true }
    }
  ]
};

Functional Presets

// Functional preset that receives options
module.exports = (options = {}) => ({
  babel: (config) => ({
    ...config,
    plugins: [
      ...config.plugins,
      options.enableExperimentalFeatures && 'babel-plugin-experimental'
    ].filter(Boolean)
  }),

  addons: [
    '@storybook/addon-essentials',
    ...(options.enableDocs ? ['@storybook/addon-docs'] : [])
  ]
});

Supporting Types

interface InterPresetOptions {
  configDir: string;
  packageJson?: PackageJson;
  outputDir?: string;
  cacheKey?: string;
  ignorePreview?: boolean;
  isCritical?: boolean;
  build?: TestBuildConfig;
}

interface TestBuildConfig {
  test?: {
    disabledAddons?: string[];
    disableBlocks?: boolean;
    disableMDXEntries?: boolean;
    disableAutoDocs?: boolean;
    disableDocgen?: boolean;
    disableSourcemaps?: boolean;
    disableTreeShaking?: boolean;
    esbuildMinify?: boolean;
  };
}

Install with Tessl CLI

npx tessl i tessl/npm-storybook--core-common

docs

caching.md

configuration.md

environment-cli.md

index.md

package-management.md

presets.md

story-processing.md

text-processing.md

tile.json