diff --git a/src/rollup/types.d.ts b/src/rollup/types.d.ts index 987c08f3e59..c24a7dded59 100644 --- a/src/rollup/types.d.ts +++ b/src/rollup/types.d.ts @@ -38,9 +38,14 @@ export interface RollupLogProps { url?: string; } +export type SourceMapSegment = + | [number] + | [number, number, number, number] + | [number, number, number, number, number]; + export interface ExistingDecodedSourceMap { file?: string; - mappings: number[][][]; + mappings: SourceMapSegment[][]; names: string[]; sourceRoot?: string; sources: string[]; diff --git a/src/utils/collapseSourcemaps.ts b/src/utils/collapseSourcemaps.ts index 5face4b5eb1..a240190b501 100644 --- a/src/utils/collapseSourcemaps.ts +++ b/src/utils/collapseSourcemaps.ts @@ -1,8 +1,11 @@ import { DecodedSourceMap, SourceMap } from 'magic-string'; -import { SourceMapLine, SourceMapMappings, SourceMapSegment } from 'sourcemap-codec'; import Chunk from '../Chunk'; import Module from '../Module'; -import { DecodedSourceMapOrMissing } from '../rollup/types'; +import { + DecodedSourceMapOrMissing, + ExistingDecodedSourceMap, + SourceMapSegment +} from '../rollup/types'; import { error } from './error'; import { basename, dirname, relative, resolve } from './path'; @@ -30,14 +33,17 @@ interface SourceMapSegmentObject { } class Link { - mappings: SourceMapMappings; + mappings: SourceMapSegment[][]; names: string[]; sources: (Source | Link)[]; - constructor(map: { mappings: number[][][]; names: string[] }, sources: (Source | Link)[]) { + constructor( + map: { mappings: SourceMapSegment[][]; names: string[] }, + sources: (Source | Link)[] + ) { this.sources = sources; this.names = map.names; - this.mappings = map.mappings as SourceMapMappings; + this.mappings = map.mappings; } traceMappings() { @@ -48,7 +54,7 @@ class Link { const mappings = []; for (const line of this.mappings) { - const tracedLine: SourceMapLine = []; + const tracedLine: SourceMapSegment[] = []; for (const segment of line) { if (segment.length == 1) continue; @@ -58,7 +64,7 @@ class Link { const traced = source.traceSegment( segment[2], segment[3], - this.names[segment[4] as number] + segment.length === 5 ? this.names[segment[4]] : '' ); if (traced) { @@ -125,7 +131,7 @@ class Link { return source.traceSegment( segment[2], segment[3], - this.names[segment[4] as number] || name + segment.length === 5 ? this.names[segment[4]] : name ); } if (segment[0] > column) { @@ -197,7 +203,9 @@ export default function collapseSourcemaps( return source; }); - let source = new Link(map, moduleSources); + // DecodedSourceMap (from magic-string) uses a number[] instead of the more + // correct SourceMapSegment tuples. Cast it here to gain type safety. + let source = new Link(map as ExistingDecodedSourceMap, moduleSources); source = bundleSourcemapChain.reduce(linkMap, source); diff --git a/src/utils/decodedSourcemap.ts b/src/utils/decodedSourcemap.ts index ecc54f17cab..3b4acfcf6a4 100644 --- a/src/utils/decodedSourcemap.ts +++ b/src/utils/decodedSourcemap.ts @@ -18,7 +18,7 @@ export function decodedSourcemap(map: Input): ExistingDecodedSourceMap | null { }; } - let mappings: number[][][]; + let mappings; if (typeof map.mappings === 'string') { mappings = decode(map.mappings); } else { diff --git a/src/utils/getOriginalLocation.ts b/src/utils/getOriginalLocation.ts index 0a84fe71184..1da1d7aa7e5 100644 --- a/src/utils/getOriginalLocation.ts +++ b/src/utils/getOriginalLocation.ts @@ -11,17 +11,17 @@ export function getOriginalLocation( while (filteredSourcemapChain.length > 0) { const sourcemap = filteredSourcemapChain.pop()!; - const line: any = sourcemap.mappings[location.line - 1]; + const line = sourcemap.mappings[location.line - 1]; let locationFound = false; if (line !== undefined) { for (const segment of line) { if (segment[0] >= location.column) { - if (segment.length < 4) break; + if (segment.length === 1) break; location = { column: segment[3], line: segment[2] + 1, - name: sourcemap.names[segment[4]], + name: segment.length === 5 ? sourcemap.names[segment[4]] : undefined, source: sourcemap.sources[segment[1]] }; locationFound = true;