-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
collapseSourcemaps.ts
213 lines (174 loc) · 5.7 KB
/
collapseSourcemaps.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { DecodedSourceMap, SourceMap } from 'magic-string';
import Chunk from '../Chunk';
import Module from '../Module';
import { ExistingRawSourceMap, RawSourceMap } from '../rollup/types';
import error from './error';
import { basename, dirname, relative, resolve } from './path';
class Source {
isOriginal: boolean;
filename: string;
content: string;
constructor(filename: string, content: string) {
this.isOriginal = true;
this.filename = filename;
this.content = content;
}
traceSegment(line: number, column: number, name: string): SourceMapSegmentObject {
return { line, column, name, source: this };
}
}
type SourceMapSegmentVector =
| [number, number, number, number, number]
| [number, number, number, number];
interface SourceMapSegmentObject {
line: number;
column: number;
name: string;
source: Source;
}
class Link {
sources: Source[];
names: string[];
mappings: SourceMapSegmentVector[][];
constructor(map: { names: string[]; mappings: SourceMapSegmentVector[][] }, sources: Source[]) {
this.sources = sources;
this.names = map.names;
this.mappings = map.mappings;
}
traceMappings() {
const sources: string[] = [];
const sourcesContent: string[] = [];
const names: string[] = [];
const mappings = [];
for (const line of this.mappings) {
const tracedLine: SourceMapSegmentVector[] = [];
for (const segment of line) {
const source = this.sources[segment[1]];
if (!source) continue;
const traced = source.traceSegment(segment[2], segment[3], this.names[segment[4]]);
if (traced) {
// newer sources are more likely to be used, so search backwards.
let sourceIndex = sources.lastIndexOf(traced.source.filename);
if (sourceIndex === -1) {
sourceIndex = sources.length;
sources.push(traced.source.filename);
sourcesContent[sourceIndex] = traced.source.content;
} else if (sourcesContent[sourceIndex] == null) {
sourcesContent[sourceIndex] = traced.source.content;
} else if (
traced.source.content != null &&
sourcesContent[sourceIndex] !== traced.source.content
) {
error({
message: `Multiple conflicting contents for sourcemap source ${
traced.source.filename
}`
});
}
const tracedSegment: SourceMapSegmentVector = [
segment[0],
sourceIndex,
traced.line,
traced.column
];
if (traced.name) {
let nameIndex = names.indexOf(traced.name);
if (nameIndex === -1) {
nameIndex = names.length;
names.push(traced.name);
}
(<SourceMapSegmentVector>tracedSegment)[4] = nameIndex;
}
tracedLine.push(tracedSegment);
}
}
mappings.push(tracedLine);
}
return { sources, sourcesContent, names, mappings };
}
traceSegment(line: number, column: number, name: string) {
const segments = this.mappings[line];
if (!segments) return null;
// binary search through segments for the given column
let i = 0;
let j = segments.length - 1;
while (i <= j) {
const m = (i + j) >> 1;
const segment = segments[m];
if (segment[0] === column) {
const source = this.sources[segment[1]];
if (!source) return null;
return source.traceSegment(segment[2], segment[3], this.names[segment[4]] || name);
}
if (segment[0] > column) {
j = m - 1;
} else {
i = m + 1;
}
}
return null;
}
}
// TODO TypeScript: Fix <any> typecasts
export default function collapseSourcemaps(
bundle: Chunk,
file: string,
map: DecodedSourceMap,
modules: Module[],
bundleSourcemapChain: RawSourceMap[],
excludeContent: boolean
) {
const moduleSources = modules.filter(module => !module.excludeFromSourcemap).map(module => {
let sourcemapChain = module.sourcemapChain;
let source: Source;
const originalSourcemap = <ExistingRawSourceMap>module.originalSourcemap;
if (!originalSourcemap) {
source = new Source(module.id, module.originalCode);
} else {
const sources = originalSourcemap.sources;
const sourcesContent = originalSourcemap.sourcesContent || [];
if (sources == null || (sources.length <= 1 && sources[0] == null)) {
source = new Source(module.id, sourcesContent[0]);
sourcemapChain = [<RawSourceMap>originalSourcemap].concat(sourcemapChain);
} else {
// TODO indiscriminately treating IDs and sources as normal paths is probably bad.
const directory = dirname(module.id) || '.';
const sourceRoot = originalSourcemap.sourceRoot || '.';
const baseSources = sources.map((source, i) => {
return new Source(resolve(directory, sourceRoot, source), sourcesContent[i]);
});
source = <any>new Link(<any>originalSourcemap, baseSources);
}
}
sourcemapChain.forEach((map: any) => {
if (map.missing) {
bundle.graph.warn({
code: 'SOURCEMAP_BROKEN',
plugin: map.plugin,
message: `Sourcemap is likely to be incorrect: a plugin${
map.plugin ? ` ('${map.plugin}')` : ``
} was used to transform files, but didn't generate a sourcemap for the transformation. Consult the plugin documentation for help`,
url: `https://rollupjs.org/guide/en#warning-sourcemap-is-likely-to-be-incorrect`
});
map = {
names: [],
mappings: ''
};
}
source = <any>new Link(map, [source]);
});
return source;
});
let source = new Link(<any>map, moduleSources);
bundleSourcemapChain.forEach(map => {
source = new Link(<any>map, [<any>source]);
});
let { sources, sourcesContent, names, mappings } = source.traceMappings();
if (file) {
const directory = dirname(file);
sources = sources.map((source: string) => relative(directory, source));
file = basename(file);
}
sourcesContent = excludeContent ? null : sourcesContent;
return new SourceMap({ file, sources, sourcesContent, names, mappings });
}