From 4de367417954b2198d52a986443cdcd9a89a57fb Mon Sep 17 00:00:00 2001 From: Anish Visaria Date: Fri, 9 Aug 2019 13:13:29 -0400 Subject: [PATCH] only include needed files --- src/app-error.ts | 25 +++++++++++++ src/explore.ts | 93 +++++++++++++++++++++++------------------------- src/index.ts | 2 ++ 3 files changed, 72 insertions(+), 48 deletions(-) diff --git a/src/app-error.ts b/src/app-error.ts index 898b953..d44eb50 100644 --- a/src/app-error.ts +++ b/src/app-error.ts @@ -36,6 +36,19 @@ interface UnmappedBytesErrorContext { unmappedBytes: number; } +interface InvalidMappingLineErrorContext { + code: 'InvalidMappingLine'; + generatedLine: number; + maxLine: number; +} + +interface InvalidMappingColumnErrorContext { + code: 'InvalidMappingColumn'; + generatedLine: number; + generatedColumn: number; + maxColumn: number; +} + interface CannotOpenTempFileErrorContext { code: 'CannotOpenTempFile'; error: Buffer; @@ -46,6 +59,8 @@ export type ErrorContext = | CommonErrorContext | OneSourceSourceMapErrorContext | UnmappedBytesErrorContext + | InvalidMappingLineErrorContext + | InvalidMappingColumnErrorContext | CannotOpenTempFileErrorContext; export function getErrorMessage(context: ErrorContext): string { @@ -71,6 +86,16 @@ See ${SOURCE_MAP_INFO_URL}`; return `Unable to map ${unmappedBytes}/${totalBytes} bytes (${bytesString}%)`; } + case 'InvalidMappingLine': { + const { generatedLine, maxLine } = context; + return `Your source map refers to generated line ${generatedLine}, but the source only contains ${maxLine} line(s).\nCheck that you are using the correct source map.`; + } + + case 'InvalidMappingColumn': { + const { generatedLine, generatedColumn, maxColumn } = context; + return `Your source map refers to generated column ${generatedColumn} on line ${generatedLine}, but the source only contains ${maxColumn} column(s) on that line.\nCheck that you are using the correct source map.`; + } + case 'CannotSaveFile': return 'Unable to save HTML to file'; diff --git a/src/explore.ts b/src/explore.ts index fdf5948..ff046d2 100644 --- a/src/explore.ts +++ b/src/explore.ts @@ -21,7 +21,7 @@ export async function exploreBundle( const sourceMapData = await loadSourceMap(code, map); - const sizes = computeGeneratedFileSizes(sourceMapData); + const sizes = computeFileSizeMapOptimized(sourceMapData); const files = adjustSourcePaths(sizes.files, options); @@ -85,26 +85,56 @@ async function loadSourceMap(codeFile: File, sourceMapFile?: File): Promise { + // lines are 1-based + const line = lines[m.generatedLine - 1]; + if (line == null) { + throw new AppError({ + code: 'InvalidMappingLine', + generatedLine: m.generatedLine, + maxLine: lines.length, + }); + } - totalBytes += numChars; + // columns are 0-based + if (m.generatedColumn >= line.length) { + throw new AppError({ + code: 'InvalidMappingColumn', + generatedLine: m.generatedLine, + generatedColumn: m.generatedColumn, + maxColumn: line.length, + }); + } - if (source === null) { - unmappedBytes += numChars; + let mappingLength: number = 0; + if (m.lastGeneratedColumn != null) { + if (m.lastGeneratedColumn >= line.length) { + throw new AppError({ + code: 'InvalidMappingColumn', + generatedLine: m.generatedLine, + generatedColumn: m.lastGeneratedColumn, + maxColumn: line.length, + }); + } + mappingLength = m.lastGeneratedColumn - m.generatedColumn + 1; } else { - files[source] = (files[source] || 0) + numChars; + mappingLength = line.length - m.generatedColumn; } - } + files[m.source] = (files[m.source] || 0) + mappingLength; + mappedBytes += mappingLength; + }); + // Doesn't count newlines as original version didn't count newlines + let totalBytes = codeFileContent.length - lines.length + 1; + const unmappedBytes = totalBytes - mappedBytes; return { files, unmappedBytes, @@ -112,39 +142,6 @@ function computeGeneratedFileSizes(sourceMapData: SourceMapData): FileSizes { }; } -interface Span { - source: string | null; - numChars: number; -} - -function computeSpans(sourceMapData: SourceMapData): Span[] { - const { consumer, codeFileContent } = sourceMapData; - - const lines = codeFileContent.split('\n'); - const spans: Span[] = []; - let numChars = 0; - - let lastSource: string | null | undefined = undefined; // not a string, not null - - for (let line = 1; line <= lines.length; line++) { - const lineText = lines[line - 1]; - const numCols = lineText.length; - - for (let column = 0; column < numCols; column++, numChars++) { - const { source } = consumer.originalPositionFor({ line, column }); - - if (source !== lastSource) { - lastSource = source; - spans.push({ source, numChars: 1 }); - } else { - spans[spans.length - 1].numChars += 1; - } - } - } - - return spans; -} - export function adjustSourcePaths(fileSizeMap: FileSizeMap, options: ExploreOptions): FileSizeMap { if (!options.noRoot) { const prefix = getCommonPathPrefix(Object.keys(fileSizeMap)); diff --git a/src/index.ts b/src/index.ts index 414c8a7..242abe3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,6 +20,8 @@ export type ErrorCode = | 'NoSourceMap' | 'OneSourceSourceMap' | 'UnmappedBytes' + | 'InvalidMappingLine' + | 'InvalidMappingColumn' | 'CannotSaveFile' | 'CannotCreateTempFile' | 'CannotOpenTempFile';