or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

base-source.mdcaching-performance.mdindex.mdsource-implementations.mdsource-manipulation.mdutility-classes.md
tile.json

tessl/npm-webpack-sources

Source code handling classes for webpack and other build tools with optional source map support

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/webpack-sources@3.3.x

To install, run

npx @tessl/cli install tessl/npm-webpack-sources@3.3.0

index.mddocs/

webpack-sources

webpack-sources provides a comprehensive set of classes for handling source code in webpack and other build tools. It offers different representations of source code with optional source map support, enabling sophisticated source manipulation while preserving debugging information.

Package Information

  • Package Name: webpack-sources
  • Package Type: npm
  • Language: JavaScript/TypeScript
  • Installation: npm install webpack-sources

Core Imports

const { Source, RawSource, OriginalSource, SourceMapSource, util } = require("webpack-sources");

For ESM:

import { Source, RawSource, OriginalSource, SourceMapSource, util } from "webpack-sources";

For utility functions:

const { util: { stringBufferUtils } } = require("webpack-sources");
// Access optimization utilities:
// stringBufferUtils.enableDualStringBufferCaching()
// stringBufferUtils.internString(str)

Basic Usage

const { RawSource, ConcatSource } = require("webpack-sources");

// Create a simple source
const source = new RawSource("console.log('Hello World!');");

// Get source content
const code = source.source(); // "console.log('Hello World!');"
const size = source.size(); // 27

// Combine sources
const combined = new ConcatSource(
  new RawSource("const greeting = "),
  new RawSource("'Hello World';"),
  new RawSource("\nconsole.log(greeting);")
);

Architecture

webpack-sources is built around several key components:

  • Base Source Class: Abstract foundation providing the core source interface
  • Source Implementations: Concrete classes representing different source types and capabilities
  • Source Manipulation: Classes for combining, modifying, and transforming sources
  • Caching Layer: Performance optimization through memoization of expensive operations
  • Source Map Support: Full integration with source maps for debugging support
  • Memory Optimization: String interning and buffer caching for reduced memory usage

Capabilities

Base Source Interface

Core interface that all source representations implement, providing methods for accessing source code, size, and source maps.

abstract class Source {
  /** Returns the source code as string or Buffer */
  source(): string | Buffer;
  /** Returns the source code as Buffer */
  buffer(): Buffer;
  /** Returns size in bytes */
  size(): number;
  /** Returns source map or null */
  map(options?: MapOptions): RawSourceMap | null;
  /** Returns both source and map together */
  sourceAndMap(options?: MapOptions): SourceAndMap;
  /** Updates hash with source content */
  updateHash(hash: HashLike): void;
}

interface MapOptions {
  /** Include column mappings (default: true) */
  columns?: boolean;
  /** Treat as module */
  module?: boolean;
}

interface SourceAndMap {
  source: string | Buffer;
  map: RawSourceMap | null;
}

interface HashLike {
  update(data: string | Buffer, encoding?: string): HashLike;
  digest(encoding?: string): string | Buffer;
}

Base Source

Source Implementations

Different types of source representations for various use cases, from simple raw text to complex source-mapped content.

class RawSource extends Source {
  constructor(value: string | Buffer, convertToString?: boolean);
  isBuffer(): boolean;
}

class OriginalSource extends Source {
  constructor(value: string | Buffer, name: string);
  getName(): string;
}

class SourceMapSource extends Source {
  constructor(
    value: string | Buffer,
    name: string,
    sourceMap?: string | RawSourceMap | Buffer,
    originalSource?: string | Buffer,
    innerSourceMap?: string | RawSourceMap | Buffer,
    removeOriginalSource?: boolean
  );
  getArgsAsBuffers(): [Buffer, string, Buffer, Buffer?, Buffer?, boolean?];
}

Source Implementations

Source Manipulation

Classes for combining, modifying, and transforming sources while preserving source map information.

class ConcatSource extends Source {
  constructor(...items: ConcatSourceChild[]);
  add(item: ConcatSourceChild): void;
  getChildren(): Source[];
}

class ReplaceSource extends Source {
  constructor(source: Source, name?: string);
  replace(start: number, end: number, newValue: string, name?: string): void;
  insert(pos: number, newValue: string, name?: string): void;
  original(): Source;
}

class PrefixSource extends Source {
  constructor(prefix: string, source: string | Source | Buffer);
  getPrefix(): string;
  original(): Source;
}

type ConcatSourceChild = string | Source | SourceLike;

Source Manipulation

Caching and Performance

Performance optimization through caching and memory management utilities.

class CachedSource extends Source {
  constructor(source: Source | (() => Source), cachedData?: CachedData);
  getCachedData(): CachedData;
  original(): Source;
  originalLazy(): Source | (() => Source);
}

interface CachedData {
  source?: boolean;
  buffer: Buffer;
  size?: number;
  maps: Map<string, BufferEntry>;
  hash?: (string | Buffer)[];
}

Caching and Performance

Utility Classes

Additional utilities for source compatibility and specialized use cases.

class CompatSource extends Source {
  constructor(sourceLike: SourceLike);
  static from(sourceLike: SourceLike): Source;
}

class SizeOnlySource extends Source {
  constructor(size: number);
}

interface SourceLike {
  source(): string | Buffer;
  buffer?(): Buffer;
  size?(): number;
  map?(options?: MapOptions): RawSourceMap | null;
  sourceAndMap?(options?: MapOptions): SourceAndMap;
  updateHash?(hash: HashLike): void;
}

Utility Classes

Types

interface RawSourceMap {
  version: number;
  sources: string[];
  names: string[];
  sourceRoot?: string;
  sourcesContent?: string[];
  mappings: string;
  file: string;
  debugId?: string;
  ignoreList?: number[];
}

interface BufferEntry {
  map?: RawSourceMap | null;
  bufferedMap?: BufferedMap | null;
}

interface BufferedMap {
  version: number;
  sources: string[];
  names: string[];
  sourceRoot?: string;
  sourcesContent?: ("" | Buffer)[];
  mappings?: Buffer;
  file: string;
}

interface GeneratedSourceInfo {
  generatedLine?: number;
  generatedColumn?: number;
  source?: string;
}

interface StreamChunksOptions {
  source?: boolean;
  finalSource?: boolean;
  columns?: boolean;
}

type OnChunk = (
  chunk: string | undefined,
  generatedLine: number,
  generatedColumn: number,
  sourceIndex: number,
  originalLine: number,
  originalColumn: number,
  nameIndex: number
) => void;

type OnSource = (
  sourceIndex: number,
  source: string | null,
  sourceContent?: string
) => void;

type OnName = (nameIndex: number, name: string) => void;