or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdloader-integration.mdplugin-configuration.mdthread-pool.md
tile.json

tessl/npm-happypack

webpack speed booster that parallelizes file transformations across multiple Node.js worker processes

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/happypack@5.0.x

To install, run

npx @tessl/cli install tessl/npm-happypack@5.0.0

index.mddocs/

HappyPack

HappyPack is a webpack performance optimization plugin that significantly speeds up initial builds by parallelizing file transformations across multiple Node.js worker processes. It acts as a parallel processing layer for webpack loaders, distributing transformation work across background threads to reduce build times dramatically.

Package Information

  • Package Name: happypack
  • Package Type: npm
  • Language: JavaScript
  • Installation:
    npm install --save-dev happypack

Core Imports

const HappyPack = require('happypack');

For specific components:

const HappyPack = require('happypack');
const HappyThreadPool = HappyPack.ThreadPool;

For the loader:

// The loader is accessed via the package path
require('happypack/loader')

// In webpack configuration
use: 'happypack/loader'

Basic Usage

// webpack.config.js
const HappyPack = require('happypack');

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        use: 'happypack/loader',
        include: [/* ... */],
        exclude: [/* ... */]
      }
    ]
  },
  plugins: [
    new HappyPack({
      loaders: ['babel-loader?presets[]=es2015']
    })
  ]
};

Architecture

HappyPack consists of several key components:

  • HappyPlugin: Main webpack plugin that orchestrates parallel processing
  • HappyLoader: Webpack loader that delegates work to plugin instances
  • Thread Pool System: Manages worker processes for parallel loader execution
  • RPC Communication: Handles message passing between main and worker processes
  • Serialization Layer: Ensures webpack configuration and results can be passed between processes

Capabilities

Plugin Configuration

Main HappyPack plugin with comprehensive configuration options for customizing parallel processing behavior.

/**
 * Creates a new HappyPack plugin instance
 * @param userConfig - Configuration object
 * @returns HappyPlugin instance
 */
function HappyPlugin(userConfig);

interface HappyPluginConfig {
  /** Unique identifier for this plugin instance */
  id?: string;
  /** Number of background threads to spawn */
  threads?: number;
  /** Pre-defined thread pool to use */
  threadPool?: HappyThreadPool;
  /** Array of loader configurations */
  loaders: LoaderConfig[];
  /** Alias for loaders property */
  use?: LoaderConfig[];
  /** Enable status logging */
  verbose?: boolean;
  /** Enable logging during webpack profiling */
  verboseWhenProfiling?: boolean;
  /** Enable debug logging */
  debug?: boolean;
  /** Compiler identifier for multi-compiler setups */
  compilerId?: string;
  /** Windows compatibility messaging mode */
  bufferedMessaging?: boolean;
}

type LoaderConfig = string | {
  loader?: string;
  path?: string;
  query?: string | object;
  options?: object;
};

Plugin Configuration

Thread Pool Management

Advanced thread pool system for sharing workers across multiple HappyPack instances and optimizing resource usage.

/**
 * Creates a shared thread pool for multiple HappyPack instances
 * @param config - Thread pool configuration
 * @returns Thread pool instance
 */
HappyPack.ThreadPool(config);

interface ThreadPoolConfig {
  /** Number of worker threads to create */
  size: number;
  /** Optional identifier for logging */
  id?: string;
  /** Enable verbose logging */
  verbose?: boolean;
  /** Enable debug logging */
  debug?: boolean;
  /** Enable Windows compatibility mode */
  bufferedMessaging?: boolean;
}

interface ThreadPool {
  /** Number of threads in the pool */
  size: number;
  /** Initialize the thread pool */
  start(compilerId: string, compiler: object, compilerOptions: string, done: function): void;
  /** Check if thread pool is running */
  isRunning(): boolean;
  /** Compile files using the thread pool */
  compile(loaderId: string, loader: object, params: object, done: function): void;
  /** Shutdown the thread pool */
  stop(compilerId: string): void;
}

Thread Pool Management

Loader Integration

HappyPack loader that integrates with webpack's module resolution system to delegate processing to parallel workers.

/**
 * HappyPack webpack loader function
 * @param sourceCode - Source file content
 * @param sourceMap - Source map data
 * @returns Processed source code and source map via callback
 */
function HappyLoader(sourceCode, sourceMap);

The loader is used in webpack configuration with query parameters:

// Basic usage
use: 'happypack/loader'

// With specific plugin ID
use: 'happypack/loader?id=babel'

// With compiler ID for multi-compiler setups
use: 'happypack/loader?id=babel&compilerId=main'

Loader Integration

Types

class HappyPlugin {
  constructor(userConfig: HappyPluginConfig);
  
  /** Plugin name identifier */
  name: string;
  /** Plugin configuration */
  config: HappyPluginConfig;
  /** Plugin ID */
  id: string;
  /** Thread pool instance */
  threadPool: ThreadPool;
  
  /**
   * Webpack plugin interface method - registers plugin hooks
   * @param compiler - Webpack compiler instance
   */
  apply(compiler: object): void;
  
  /**
   * Initialize plugin and start thread pool
   * @param compiler - Webpack compiler instance
   * @param done - Completion callback function
   */
  start(compiler: object, done: function): void;
  
  /** Shutdown thread pool and cleanup resources */
  stop(): void;
  
  /**
   * Compile files using the thread pool
   * @param loaderContext - Webpack loader context
   * @param params - Compilation parameters including sourceCode, sourceMap, resource
   * @param done - Completion callback with (error, result)
   */
  compile(loaderContext: object, params: object, done: function): void;
  
  /**
   * Generate webpack loader request string for a resource
   * @param resource - File resource path
   * @returns Formatted loader request string
   */
  generateRequest(resource: string): string;
}

/** List of webpack options that can be serialized to workers */
HappyPlugin.SERIALIZABLE_OPTIONS: string[];

/** Reference to HappyThreadPool constructor for convenience */
HappyPlugin.ThreadPool: function;