Source code handling classes for webpack and other build tools with optional source map support
—
Core interface that all source representations implement, providing methods for accessing source code, size, source maps, and hash operations.
Abstract base class that defines the common interface for all source types.
/**
* Abstract base class for all source representations
* Provides core methods for accessing source content, size, and source maps
*/
abstract class Source {
/** Returns the represented source code as string or Buffer */
source(): string | Buffer;
/** Returns the represented source code as Buffer */
buffer(): Buffer;
/** Returns size in bytes of the represented source code */
size(): number;
/** Returns the SourceMap as JSON or null if not available */
map(options?: MapOptions): RawSourceMap | null;
/** Returns both source code and source map together */
sourceAndMap(options?: MapOptions): SourceAndMap;
/** Updates the provided Hash object with the content */
updateHash(hash: HashLike): void;
}Usage Example:
const { RawSource } = require("webpack-sources");
class CustomSource extends Source {
constructor(content) {
super();
this.content = content;
}
source() {
return this.content;
}
updateHash(hash) {
hash.update("CustomSource");
hash.update(this.content);
}
}
const source = new CustomSource("console.log('test');");
console.log(source.source()); // "console.log('test');"
console.log(source.size()); // 19Configuration options for source map generation.
/**
* Options for source map generation
*/
interface MapOptions {
/**
* If false, implementation may omit mappings for columns
* Default: true
*/
columns?: boolean;
/** Indicates if this is a module source */
module?: boolean;
}Combined result containing both source content and source map.
/**
* Combined result of source content and source map
*/
interface SourceAndMap {
/** The source code */
source: string | Buffer;
/** The source map or null if not available */
map: RawSourceMap | null;
}Usage Example:
const { OriginalSource } = require("webpack-sources");
const source = new OriginalSource("const x = 1;", "example.js");
const result = source.sourceAndMap({ columns: true });
console.log(result.source); // "const x = 1;"
console.log(result.map); // SourceMap object or nullInterface for hash objects used in updateHash method.
/**
* Interface for hash objects compatible with updateHash
*/
interface HashLike {
/** Update hash with data */
update(data: string | Buffer, inputEncoding?: string): HashLike;
/** Get hash digest */
digest(encoding?: string): string | Buffer;
}Usage Example:
const crypto = require("crypto");
const { RawSource } = require("webpack-sources");
const hash = crypto.createHash("sha256");
const source = new RawSource("console.log('hello');");
source.updateHash(hash);
const digest = hash.digest("hex");
console.log(digest); // Hash of the source contentStandard source map format as defined by the Source Map specification.
/**
* Standard source map format
*/
interface RawSourceMap {
/** Source map format version (typically 3) */
version: number;
/** Array of source file names */
sources: string[];
/** Array of identifier names used in mappings */
names: string[];
/** Optional root path for source files */
sourceRoot?: string;
/** Optional array of source file contents */
sourcesContent?: string[];
/** Base64 VLQ encoded mappings */
mappings: string;
/** Generated file name */
file: string;
/** Optional debug identifier */
debugId?: string;
/** Optional array of source indices to ignore */
ignoreList?: number[];
}Install with Tessl CLI
npx tessl i tessl/npm-webpack-sources