CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-storybook--builder-webpack4

Webpack 4-based builder for Storybook that provides framework-agnostic build engine for preview iframe compilation and bundling.

91

1.01x
Overview
Eval results
Files

builder-operations.mddocs/

Builder Operations

Core build functionality for development and production modes, providing webpack compilation with generator-based interruption support, progress reporting, and Express middleware integration.

Capabilities

Start Function

Starts a development server with hot module replacement and webpack dev middleware.

/**
 * Starts development server with hot reloading and webpack dev middleware
 * @param options - Builder start options including router and Storybook options
 * @returns Promise resolving to build result with bail function and stats
 */
function start(options: BuilderStartOptions): Promise<BuilderStartResult>;

interface BuilderStartOptions {
  /** High-resolution time for performance tracking */
  startTime?: ReturnType<typeof process.hrtime>;
  /** Storybook configuration options */
  options: Options;
  /** Express router for middleware mounting */
  router: any;
}

interface BuilderStartResult {
  /** Function to stop the build process and clean up resources */
  bail: () => Promise<void>;
  /** Webpack compilation statistics */
  stats: Stats;
  /** Total build time measurement */
  totalTime: ReturnType<typeof process.hrtime>;
}

Usage Examples:

import { start } from "@storybook/builder-webpack4";
import express from "express";

const app = express();
const router = express.Router();

const startResult = await start({
  startTime: process.hrtime(),
  options: {
    configType: 'DEVELOPMENT',
    outputDir: './storybook-static',
    configDir: '.storybook',
    presets: presetManager,
    framework: 'react',
    // ... other options
  },
  router,
});

// Mount the router with webpack middleware
app.use(router);

// Later, stop the build process
await startResult.bail();

Build Function

Builds production version with webpack compilation and optimization.

/**
 * Builds production version with webpack compilation and optimization
 * @param options - Builder build options with Storybook configuration
 * @returns Promise resolving to webpack Stats object
 */
function build(options: BuilderBuildOptions): Promise<BuilderBuildResult>;

interface BuilderBuildOptions {
  /** High-resolution time for performance tracking */
  startTime?: ReturnType<typeof process.hrtime>;
  /** Storybook configuration options */
  options: Options;
}

interface BuilderBuildResult extends Stats {
  /** Webpack compilation statistics including errors and warnings */
}

Usage Examples:

import { build } from "@storybook/builder-webpack4";

const buildResult = await build({
  startTime: process.hrtime(),
  options: {
    configType: 'PRODUCTION',
    outputDir: './storybook-static',
    configDir: '.storybook',
    presets: presetManager,
    framework: 'react',
    quiet: false,
    // ... other options
  },
});

if (buildResult.hasErrors()) {
  console.error('Build failed with errors:', buildResult.toJson().errors);
} else {
  console.log('Build completed successfully');
}

Bail Function

Stops the build process and cleans up resources including webpack compilation and middleware.

/**
 * Stops build process and cleans up resources
 * Forces termination of webpack compilation and dev middleware
 * @returns Promise that resolves when cleanup is complete
 */
function bail(): Promise<void>;

Usage Examples:

import { bail } from "@storybook/builder-webpack4";

// Stop any running build process
await bail();
console.log('Build process stopped and resources cleaned up');

Executor System

Webpack instance executor providing access to webpack compiler with version checking.

/**
 * Executor for obtaining webpack instance with version validation
 */
const executor: {
  /**
   * Gets webpack instance from presets or uses default webpack 4
   * @param options - Storybook options with presets
   * @returns Promise resolving to webpack instance
   */
  get(options: Options): Promise<typeof webpack>;
};

Usage Examples:

import { executor } from "@storybook/builder-webpack4";

const webpackInstance = await executor.get(storybookOptions);
const compiler = webpackInstance(webpackConfig);

Utility Functions

Additional utility functions for error handling and stats generation.

/**
 * Creates webpack Stats object from error string for consistent error handling
 * @param err - Error message string
 * @returns Stats object with error information
 */
function makeStatsFromError(err: string): Stats;

Usage Examples:

import { makeStatsFromError } from "@storybook/builder-webpack4";

const errorStats = makeStatsFromError('Webpack compilation failed');
console.log(errorStats.toJson().errors); // ['Webpack compilation failed']

Error Handling

The builder functions implement comprehensive error handling:

  • Compilation Errors: Webpack compilation errors are captured and returned in Stats objects
  • Process Interruption: Generator-based functions can be interrupted via the bail mechanism
  • Resource Cleanup: The bail function ensures proper cleanup of webpack dev middleware and compilation processes
  • Debug Mode: When debugWebpack option is enabled, full webpack stats are available for debugging

Install with Tessl CLI

npx tessl i tessl/npm-storybook--builder-webpack4

docs

builder-operations.md

configuration-system.md

index.md

preset-system.md

tile.json