Source code handling classes for webpack and other build tools with optional source map support
npx @tessl/cli install tessl/npm-webpack-sources@3.3.0webpack-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.
npm install webpack-sourcesconst { 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)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);")
);webpack-sources is built around several key components:
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;
}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?];
}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;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)[];
}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;
}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;