Skip to content

Commit 3531f17

Browse files
committed
Adding support for indexed source maps.
Indexed source maps, described in https://sourcemaps.info/spec.html#h.535es3xeprgt this should allow users to debug bundled output, like that produced by the closure-compiler when producing bundled output.
1 parent 0d435a1 commit 3531f17

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

src/adapter/sources.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ export class SourceContainer {
10671067
const fileUrl = absolutePath && utils.absolutePathToFileUrl(absolutePath);
10681068
const content = this.sourceMapFactory.guardSourceMapFn(
10691069
map,
1070-
() => map.sourceContentFor(url),
1070+
() => map.sourceContentFor(url, true),
10711071
() => null,
10721072
);
10731073

src/common/sourceMaps/sourceMap.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
import {
66
BasicSourceMapConsumer,
7+
IndexedSourceMapConsumer,
78
MappedPosition,
89
MappingItem,
910
NullableMappedPosition,
1011
NullablePosition,
1112
Position,
13+
SourceMapConsumer,
1214
} from 'source-map';
1315
import { fixDriveLetterAndSlashes } from '../pathUtils';
1416
import { completeUrlEscapingRoot } from '../urlUtils';
@@ -22,7 +24,7 @@ export interface ISourceMapMetadata {
2224
/**
2325
* Wrapper for a parsed sourcemap.
2426
*/
25-
export class SourceMap implements BasicSourceMapConsumer {
27+
export class SourceMap implements SourceMapConsumer {
2628
private static idCounter = 0;
2729

2830
/**
@@ -37,7 +39,7 @@ export class SourceMap implements BasicSourceMapConsumer {
3739
public readonly id = SourceMap.idCounter++;
3840

3941
constructor(
40-
private readonly original: BasicSourceMapConsumer,
42+
private readonly original: BasicSourceMapConsumer | IndexedSourceMapConsumer,
4143
public readonly metadata: Readonly<ISourceMapMetadata>,
4244
private readonly actualRoot: string,
4345
public readonly actualSources: ReadonlyArray<string>,
@@ -64,13 +66,6 @@ export class SourceMap implements BasicSourceMapConsumer {
6466
return this.actualSources.slice();
6567
}
6668

67-
/**
68-
* Gets the optional name of the generated code that this source map is associated with
69-
*/
70-
public get file() {
71-
return this.metadata.compiledPath ?? this.original.file;
72-
}
73-
7469
/**
7570
* Gets the source root of the sourcemap.
7671
*/
@@ -79,13 +74,6 @@ export class SourceMap implements BasicSourceMapConsumer {
7974
return this.actualRoot;
8075
}
8176

82-
/**
83-
* Gets the sources content.
84-
*/
85-
public get sourcesContent() {
86-
return this.original.sourcesContent;
87-
}
88-
8977
/**
9078
* Gets the source URL computed from the compiled path and the source root.
9179
*/

src/common/sourceMaps/sourceMapFactory.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*--------------------------------------------------------*/
44

55
import { inject, injectable } from 'inversify';
6-
import { BasicSourceMapConsumer, RawSourceMap, SourceMapConsumer } from 'source-map';
6+
import { RawIndexMap, RawSourceMap, SourceMapConsumer } from 'source-map';
77
import { IResourceProvider } from '../../adapter/resourceProvider';
88
import Dap from '../../dap/api';
99
import { IRootDapApi } from '../../dap/connection';
@@ -57,7 +57,7 @@ export class SourceMapFactory implements ISourceMapFactory {
5757
* @inheritdoc
5858
*/
5959
public async load(metadata: ISourceMapMetadata): Promise<SourceMap> {
60-
let basic: RawSourceMap | undefined;
60+
let basic: RawSourceMap | RawIndexMap | undefined;
6161
try {
6262
basic = await this.parseSourceMap(metadata.sourceMapUrl);
6363
} catch (e) {
@@ -75,18 +75,32 @@ export class SourceMapFactory implements ISourceMapFactory {
7575
const actualRoot = basic.sourceRoot;
7676
basic.sourceRoot = undefined;
7777

78+
let hasNames = false;
79+
7880
// The source map library (also) "helpfully" normalizes source URLs, so
7981
// preserve them in the same way. Then, rename the sources to prevent any
8082
// of their names colliding (e.g. "webpack://./index.js" and "webpack://../index.js")
81-
const actualSources = basic.sources;
82-
basic.sources = basic.sources.map((_, i) => `source${i}.js`);
83+
let actualSources: string[] = [];
84+
if ('sections' in basic && Array.isArray(basic.sections)) {
85+
actualSources = [];
86+
let i = 0;
87+
for (const section of basic.sections) {
88+
actualSources.push(...section.map.sources);
89+
section.map.sources = section.map.sources.map(() => `source${i++}.js`);
90+
hasNames ||= !!section.map.names?.length;
91+
}
92+
} else if ('sources' in basic && Array.isArray(basic.sources)) {
93+
actualSources = basic.sources;
94+
basic.sources = basic.sources.map((_, i) => `source${i}.js`);
95+
hasNames = !!basic.names?.length;
96+
}
8397

8498
return new SourceMap(
85-
(await new SourceMapConsumer(basic)) as BasicSourceMapConsumer,
99+
await new SourceMapConsumer(basic),
86100
metadata,
87101
actualRoot ?? '',
88102
actualSources,
89-
!!basic.names?.length,
103+
hasNames,
90104
);
91105
}
92106

@@ -136,7 +150,7 @@ export class SourceMapFactory implements ISourceMapFactory {
136150
// no-op
137151
}
138152

139-
private async parseSourceMap(sourceMapUrl: string): Promise<RawSourceMap> {
153+
private async parseSourceMap(sourceMapUrl: string): Promise<RawSourceMap | RawIndexMap> {
140154
let absolutePath = fileUrlToAbsolutePath(sourceMapUrl);
141155
if (absolutePath) {
142156
absolutePath = this.pathResolve.rebaseRemoteToLocal(absolutePath);

0 commit comments

Comments
 (0)