forked from rollup/rollup
/
collapseSourcemaps.ts
223 lines (185 loc) · 5.74 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
214
215
216
217
218
219
220
221
222
223
import { DecodedSourceMap, SourceMap } from 'magic-string';
import Chunk from '../Chunk';
import Module from '../Module';
import {
DecodedSourceMapOrMissing,
ExistingDecodedSourceMap,
SourceMapSegment
} from '../rollup/types';
import { error } from './error';
import { basename, dirname, relative, resolve } from './path';
class Source {
content: string;
filename: string;
isOriginal: boolean;
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 };
}
}
interface SourceMapSegmentObject {
column: number;
line: number;
name: string;
source: Source;
}
class Link {
mappings: SourceMapSegment[][];
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;
}
traceMappings() {
const sources: string[] = [];
const sourcesContent: string[] = [];
const names: string[] = [];
const mappings = [];
for (const line of this.mappings) {
const tracedLine: SourceMapSegment[] = [];
for (const segment of line) {
if (segment.length == 1) continue;
const source = this.sources[segment[1]];
if (!source) continue;
const traced = source.traceSegment(
segment[2],
segment[3],
segment.length === 5 ? 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: SourceMapSegment = [
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);
}
(tracedSegment as SourceMapSegment)[4] = nameIndex;
}
tracedLine.push(tracedSegment);
}
}
mappings.push(tracedLine);
}
return { sources, sourcesContent, names, mappings };
}
traceSegment(line: number, column: number, name: string): SourceMapSegmentObject | null {
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) {
if (segment.length == 1) return null;
const source = this.sources[segment[1]];
if (!source) return null;
return source.traceSegment(
segment[2],
segment[3],
segment.length === 5 ? this.names[segment[4]] : name
);
}
if (segment[0] > column) {
j = m - 1;
} else {
i = m + 1;
}
}
return null;
}
}
export default function collapseSourcemaps(
bundle: Chunk,
file: string,
map: DecodedSourceMap,
modules: Module[],
bundleSourcemapChain: DecodedSourceMapOrMissing[],
excludeContent: boolean
) {
function linkMap(source: Source | Link, map: DecodedSourceMapOrMissing) {
if (map.mappings) {
return new Link(map, [source]);
}
bundle.graph.warn({
code: 'SOURCEMAP_BROKEN',
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`,
plugin: map.plugin,
url: `https://rollupjs.org/guide/en/#warning-sourcemap-is-likely-to-be-incorrect`
});
return new Link(
{
mappings: [],
names: []
},
[source]
);
}
const moduleSources = modules
.filter(module => !module.excludeFromSourcemap)
.map(module => {
let source: Source | Link;
const originalSourcemap = module.originalSourcemap;
if (!originalSourcemap) {
source = new Source(module.id, module.originalCode);
} else {
const sources = originalSourcemap.sources;
const sourcesContent = originalSourcemap.sourcesContent || [];
// 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) => new Source(resolve(directory, sourceRoot, source), sourcesContent[i])
);
source = new Link(originalSourcemap, baseSources);
}
source = module.sourcemapChain.reduce(linkMap, source);
return source;
});
// 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);
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) as string[];
return new SourceMap({ file, sources, sourcesContent, names, mappings });
}