Analyze and debug space usage through source maps
npx @tessl/cli install tessl/npm-source-map-explorer@2.5.0Source Map Explorer is a TypeScript library and command-line tool for analyzing JavaScript bundle size and composition through source maps. It creates interactive treemap visualizations that help developers understand which files and dependencies contribute most to their minified bundle size, enabling effective debugging of code bloat and optimization opportunities.
npm install source-map-explorernpm install -g source-map-explorerimport { explore } from "source-map-explorer";
// or
import explore from "source-map-explorer";For CommonJS:
const { explore } = require("source-map-explorer");
// or
const explore = require("source-map-explorer").default;import { explore } from "source-map-explorer";
// Analyze a single bundle
const result = await explore("dist/bundle.js");
// Analyze bundle with explicit source map
const result = await explore({
code: "dist/bundle.js",
map: "dist/bundle.js.map"
});
// Analyze with options
const result = await explore("dist/bundle.js", {
output: { format: "json" },
onlyMapped: true
});
console.log(result.bundles[0].files);
// {
// "src/main.js": { size: 2543 },
// "node_modules/lodash/index.js": { size: 15234 },
// "[unmapped]": { size: 156 }
// }Source Map Explorer is built around several key components:
Core functionality for analyzing JavaScript bundles through source maps. Supports single files, glob patterns, and programmatic buffer inputs.
function explore(
bundlesAndFileTokens: BundlesAndFileTokens,
options?: ExploreOptions
): Promise<ExploreResult>;
type BundlesAndFileTokens = (Bundle | string)[] | Bundle | string;
interface Bundle {
code: File;
map?: File;
coverageRanges?: ColumnsRange[][];
}
type File = string | Buffer;Comprehensive configuration system for customizing analysis behavior, output formats, and visualization options.
interface ExploreOptions {
onlyMapped?: boolean;
excludeSourceMapComment?: boolean;
output?: {
format: 'json' | 'tsv' | 'html';
filename?: string;
};
noRoot?: boolean;
noBorderChecks?: boolean;
replaceMap?: Record<string, string>;
coverage?: string;
gzip?: boolean;
sort?: boolean;
}Structured data format for bundle analysis results, including file size breakdowns, error handling, and formatted output.
interface ExploreResult {
bundles: ExploreBundleResult[];
output?: string;
errors: ExploreErrorResult[];
}
interface ExploreBundleResult {
bundleName: string;
files: Record<string, FileData>;
mappedBytes: number;
unmappedBytes?: number;
eolBytes: number;
sourceMapCommentBytes: number;
totalBytes: number;
}Comprehensive error system with specific error codes for different failure scenarios and validation issues.
class AppError extends Error {
code?: ErrorCode;
cause?: Error;
}
type ErrorCode =
| 'NoBundles'
| 'NoSourceMap'
| 'OneSourceSourceMap'
| 'UnmappedBytes'
| 'InvalidMappingLine'
| 'InvalidMappingColumn'
| 'CannotSaveFile'
| 'CannotCreateTempFile'
| 'CannotOpenTempFile'
| 'CannotOpenCoverageFile'
| 'NoCoverageMatches';// Special file keys used in analysis results
const UNMAPPED_KEY = "[unmapped]";
const SOURCE_MAP_COMMENT_KEY = "[sourceMappingURL]";
const NO_SOURCE_KEY = "[no source]";
const EOL_KEY = "[EOLs]";The package provides both source-map-explorer and sme commands for command-line usage:
# Basic usage
source-map-explorer bundle.js
# Multiple bundles
source-map-explorer dist/*.js
# Output formats
source-map-explorer bundle.js --json result.json
source-map-explorer bundle.js --html result.html
source-map-explorer bundle.js --tsv result.tsv
# Options
source-map-explorer bundle.js --only-mapped --gzip --sortThe CLI supports all programmatic options through command-line flags and provides the same analysis capabilities as the programmatic API.