1 | import { encode, decode } from 'sourcemap-codec';
|
2 |
|
3 | function traceSegment ( loc, mappings ) {
|
4 | const line = loc[0];
|
5 | const column = loc[1];
|
6 |
|
7 | const segments = mappings[ line ];
|
8 |
|
9 | if ( !segments ) return null;
|
10 |
|
11 | for ( let i = 0; i < segments.length; i += 1 ) {
|
12 | const segment = segments[i];
|
13 |
|
14 | if ( segment[0] > column ) return null;
|
15 |
|
16 | if ( segment[0] === column ) {
|
17 | if ( segment[1] !== 0 ) {
|
18 | throw new Error( 'Bad sourcemap' );
|
19 | }
|
20 |
|
21 | return [ segment[2], segment[3] ];
|
22 | }
|
23 | }
|
24 |
|
25 | return null;
|
26 | }
|
27 |
|
28 | export default function collapseSourcemaps ( map, modules ) {
|
29 | const chains = modules.map( module => {
|
30 | return module.sourceMapChain.map( map => {
|
31 | if ( !map ) throw new Error( 'Cannot generate a sourcemap if non-sourcemap-generating transformers are used' );
|
32 | return decode( map.mappings );
|
33 | });
|
34 | });
|
35 |
|
36 | const decodedMappings = decode( map.mappings );
|
37 |
|
38 | const tracedMappings = decodedMappings.map( line => {
|
39 | let tracedLine = [];
|
40 |
|
41 | line.forEach( segment => {
|
42 | const sourceIndex = segment[1];
|
43 | const sourceCodeLine = segment[2];
|
44 | const sourceCodeColumn = segment[3];
|
45 |
|
46 | const chain = chains[ sourceIndex ];
|
47 |
|
48 | let i = chain.length;
|
49 | let traced = [ sourceCodeLine, sourceCodeColumn ];
|
50 |
|
51 | while ( i-- && traced ) {
|
52 | traced = traceSegment( traced, chain[i] );
|
53 | }
|
54 |
|
55 | if ( traced ) {
|
56 | tracedLine.push([
|
57 | segment[0],
|
58 | segment[1],
|
59 | traced[0],
|
60 | traced[1]
|
61 |
|
62 | ]);
|
63 | }
|
64 | });
|
65 |
|
66 | return tracedLine;
|
67 | });
|
68 |
|
69 | map.sourcesContent = modules.map( module => module.originalCode );
|
70 | map.mappings = encode( tracedMappings );
|
71 | return map;
|
72 | }
|