| import { TraceMap } from '@jridgewell/trace-mapping'; |
|
|
| import { OriginalSource, MapSource } from './source-map-tree'; |
|
|
| import type { Sources, MapSource as MapSourceType } from './source-map-tree'; |
| import type { SourceMapInput, SourceMapLoader, LoaderContext } from './types'; |
|
|
| function asArray<T>(value: T | T[]): T[] { |
| if (Array.isArray(value)) return value; |
| return [value]; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export default function buildSourceMapTree( |
| input: SourceMapInput | SourceMapInput[], |
| loader: SourceMapLoader, |
| ): MapSourceType { |
| const maps = asArray(input).map((m) => new TraceMap(m, '')); |
| const map = maps.pop()!; |
|
|
| for (let i = 0; i < maps.length; i++) { |
| if (maps[i].sources.length > 1) { |
| throw new Error( |
| `Transformation map ${i} must have exactly one source file.\n` + |
| 'Did you specify these with the most recent transformation maps first?', |
| ); |
| } |
| } |
|
|
| let tree = build(map, loader, '', 0); |
| for (let i = maps.length - 1; i >= 0; i--) { |
| tree = MapSource(maps[i], [tree]); |
| } |
| return tree; |
| } |
|
|
| function build( |
| map: TraceMap, |
| loader: SourceMapLoader, |
| importer: string, |
| importerDepth: number, |
| ): MapSourceType { |
| const { resolvedSources, sourcesContent, ignoreList } = map; |
|
|
| const depth = importerDepth + 1; |
| const children = resolvedSources.map((sourceFile: string | null, i: number): Sources => { |
| |
| |
| |
| |
| const ctx: LoaderContext = { |
| importer, |
| depth, |
| source: sourceFile || '', |
| content: undefined, |
| ignore: undefined, |
| }; |
|
|
| |
| |
| const sourceMap = loader(ctx.source, ctx); |
|
|
| const { source, content, ignore } = ctx; |
|
|
| |
| if (sourceMap) return build(new TraceMap(sourceMap, source), loader, source, depth); |
|
|
| |
| |
| |
| |
| const sourceContent = |
| content !== undefined ? content : sourcesContent ? sourcesContent[i] : null; |
| const ignored = ignore !== undefined ? ignore : ignoreList ? ignoreList.includes(i) : false; |
| return OriginalSource(source, sourceContent, ignored); |
| }); |
|
|
| return MapSource(map, children); |
| } |
|
|