-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
collapseSourcemaps.js
176 lines (137 loc) · 4.76 KB
/
collapseSourcemaps.js
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
import { encode } from 'sourcemap-codec';
import error from './error.js';
import { basename, dirname, relative, resolve } from './path.js';
class Source {
constructor ( filename, content ) {
this.isOriginal = true;
this.filename = filename;
this.content = content;
}
traceSegment ( line, column, name ) {
return { line, column, name, source: this };
}
}
class Link {
constructor ( map, sources ) {
this.sources = sources;
this.names = map.names;
this.mappings = map.mappings;
}
traceMappings () {
const sources = [];
const sourcesContent = [];
const names = [];
const mappings = this.mappings.map( line => {
const tracedLine = [];
line.forEach( segment => {
const source = this.sources[ segment[1] ];
if ( !source ) return;
const traced = source.traceSegment( segment[2], segment[3], this.names[ segment[4] ] );
if ( traced ) {
let sourceIndex = null;
let nameIndex = null;
segment = [
segment[0],
null,
traced.line,
traced.column
];
// newer sources are more likely to be used, so search backwards.
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 ${source.filename}`
});
}
segment[1] = sourceIndex;
if ( traced.name ) {
nameIndex = names.indexOf( traced.name );
if ( nameIndex === -1 ) {
nameIndex = names.length;
names.push( traced.name );
}
segment[4] = nameIndex;
}
tracedLine.push( segment );
}
});
return tracedLine;
});
return { sources, sourcesContent, names, mappings };
}
traceSegment ( line, column, name ) {
const segments = this.mappings[ line ];
if ( !segments ) return null;
for ( let i = 0; i < segments.length; i += 1 ) {
const segment = segments[i];
if ( segment[0] > column ) return null;
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 );
}
}
return null;
}
}
export default function collapseSourcemaps ( bundle, file, map, modules, bundleSourcemapChain ) {
const moduleSources = modules.filter( module => !module.excludeFromSourcemap ).map( module => {
let sourceMapChain = module.sourceMapChain;
let source;
if ( module.originalSourceMap == null ) {
source = new Source( module.id, module.originalCode );
} else {
const sources = module.originalSourceMap.sources;
const sourcesContent = module.originalSourceMap.sourcesContent || [];
if ( sources == null || ( sources.length <= 1 && sources[0] == null ) ) {
source = new Source( module.id, sourcesContent[0] );
sourceMapChain = [ module.originalSourceMap ].concat( sourceMapChain );
} else {
// TODO indiscriminately treating IDs and sources as normal paths is probably bad.
const directory = dirname( module.id ) || '.';
const sourceRoot = module.originalSourceMap.sourceRoot || '.';
const baseSources = sources.map( (source, i) => {
return new Source( resolve( directory, sourceRoot, source ), sourcesContent[i] );
});
source = new Link( module.originalSourceMap, baseSources );
}
}
sourceMapChain.forEach( map => {
if ( map.missing ) {
bundle.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`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#sourcemap-is-likely-to-be-incorrect`
});
map = {
names: [],
mappings: ''
};
}
source = new Link( map, [ source ]);
});
return source;
});
let source = new Link( map, moduleSources );
bundleSourcemapChain.forEach( map => {
source = new Link( map, [ source ] );
});
let { sources, sourcesContent, names, mappings } = source.traceMappings();
if ( file ) {
const directory = dirname( file );
sources = sources.map( source => relative( directory, source ) );
map.file = basename( file );
}
// we re-use the `map` object because it has convenient toString/toURL methods
map.sources = sources;
map.sourcesContent = sourcesContent;
map.names = names;
map.mappings = encode( mappings );
return map;
}