OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// Tool for visualizing the source mapped parts of a generated JS file. |
| 6 |
| 7 import 'dart:convert'; |
| 8 import 'dart:io'; |
| 9 import 'package:source_maps/source_maps.dart'; |
| 10 import 'sourcemap_html_helper.dart'; |
| 11 |
| 12 main(List<String> args) { |
| 13 String jsFileName = 'out.js'; |
| 14 if (args.length > 0) { |
| 15 jsFileName = args[0]; |
| 16 } |
| 17 String jsMapFileName = '$jsFileName.map'; |
| 18 if (args.length > 1) { |
| 19 jsMapFileName = args[1]; |
| 20 } |
| 21 generateHtml(jsFileName, jsMapFileName); |
| 22 } |
| 23 |
| 24 class MappingState { |
| 25 final String cssClass; |
| 26 final int _continuedState; |
| 27 final int _nextState; |
| 28 |
| 29 const MappingState(this.cssClass, this._continuedState, this._nextState); |
| 30 |
| 31 MappingState get continuedState => values[_continuedState]; |
| 32 |
| 33 MappingState get nextState => values[_nextState]; |
| 34 |
| 35 static const MappingState INITIAL = const MappingState('initial', 0, 1); |
| 36 static const MappingState MAPPED0 = const MappingState('mapped0', 2, 3); |
| 37 static const MappingState MAPPED0_CONTINUED = |
| 38 const MappingState('mapped0continued', 2, 3); |
| 39 static const MappingState MAPPED1 = const MappingState('mapped1', 4, 1); |
| 40 static const MappingState MAPPED1_CONTINUED = |
| 41 const MappingState('mapped1continued', 4, 1); |
| 42 static const MappingState UNMAPPED = const MappingState('unmapped', 5, 1); |
| 43 |
| 44 static const List<MappingState> values = const <MappingState>[ |
| 45 INITIAL, |
| 46 MAPPED0, |
| 47 MAPPED0_CONTINUED, |
| 48 MAPPED1, |
| 49 MAPPED1_CONTINUED, |
| 50 UNMAPPED |
| 51 ]; |
| 52 } |
| 53 |
| 54 void generateHtml(String jsFileName, String jsMapFileName) { |
| 55 String jsFile = new File(jsFileName).readAsStringSync(); |
| 56 String jsMapFile = new File(jsMapFileName).readAsStringSync(); |
| 57 SingleMapping mapping = new SingleMapping.fromJson(JSON.decode(jsMapFile)); |
| 58 StringBuffer output = new StringBuffer(); |
| 59 output.write(''' |
| 60 <html> |
| 61 <head> |
| 62 <title>${escape(jsFileName)} / ${escape(jsMapFileName)}</title> |
| 63 <style type="text/css"> |
| 64 .initial{ |
| 65 background-color: #FFFFFF; |
| 66 } |
| 67 .mapped0 { |
| 68 background-color: #E0E0C0; |
| 69 } |
| 70 .mapped0continued { |
| 71 background-color: #F8F8D8; |
| 72 } |
| 73 .mapped1 { |
| 74 background-color: #C0E0E0; |
| 75 } |
| 76 .mapped1continued { |
| 77 background-color: #D8F8F8; |
| 78 } |
| 79 .unmapped { |
| 80 background-color: #E0E0E0; |
| 81 } |
| 82 .code { |
| 83 font-family: monospace; |
| 84 white-space: pre; |
| 85 font-size: smaller; |
| 86 } |
| 87 .lineNumber { |
| 88 color: #C0C0C0; |
| 89 font-size: small; |
| 90 } |
| 91 .legend { |
| 92 position: fixed; |
| 93 top: 5px; |
| 94 right: 5px; |
| 95 border: 1px solid black; |
| 96 padding: 5px; |
| 97 background-color: #F0F0F0; |
| 98 } |
| 99 .box { |
| 100 border: 1px solid grey; |
| 101 } |
| 102 </style> |
| 103 </head> |
| 104 <body> |
| 105 <div class="legend"> |
| 106 <span class="initial"> </span> no mapping (yet)<br/> |
| 107 <span class="mapped0"> </span> |
| 108 <span class="mapped1"> </span> mapped<br/> |
| 109 <span class="mapped0continued"> </span> |
| 110 <span class="mapped1continued"> </span> mapping |
| 111 continued from previous line<br/> |
| 112 <span class="unmapped"> </span> mapping off<br/> |
| 113 </div> |
| 114 <pre class="code"> |
| 115 '''); |
| 116 |
| 117 MappingState state = MappingState.INITIAL; |
| 118 TargetEntry lastEntry; |
| 119 |
| 120 void write(String text, TargetEntry entry) { |
| 121 output.write('<span class="${state.cssClass}"'); |
| 122 String prefix = ''; |
| 123 if (entry == lastEntry) { |
| 124 prefix = 'continued: '; |
| 125 } |
| 126 lastEntry = entry; |
| 127 if (lastEntry != null) { |
| 128 if (lastEntry.sourceUrlId != null) { |
| 129 output.write(' title="$prefix'); |
| 130 output.write(escape(mapping.urls[lastEntry.sourceUrlId])); |
| 131 output.write( |
| 132 ':${lastEntry.sourceLine + 1}:${lastEntry.sourceColumn + 1}'); |
| 133 if (lastEntry.sourceNameId != null) { |
| 134 output.write(' ('); |
| 135 output.write(escape(mapping.names[lastEntry.sourceNameId])); |
| 136 output.write(')'); |
| 137 } |
| 138 output.write('"'); |
| 139 } else { |
| 140 output.write(' title="unmapped"'); |
| 141 } |
| 142 } |
| 143 output.write('>'); |
| 144 output.write(escape(text)); |
| 145 output.write('</span>'); |
| 146 } |
| 147 |
| 148 int nextTargetLineIndex = 0; |
| 149 List<String> lines = jsFile.split('\n'); |
| 150 int lineNoWidth = '${lines.length}'.length; |
| 151 for (int lineNo = 0; lineNo < lines.length; lineNo++) { |
| 152 output.write(lineNumber(lineNo, width: lineNoWidth)); |
| 153 String line = lines[lineNo]; |
| 154 TargetLineEntry targetLineEntry; |
| 155 while (nextTargetLineIndex < mapping.lines.length) { |
| 156 TargetLineEntry entry = mapping.lines[nextTargetLineIndex]; |
| 157 if (entry.line == lineNo) { |
| 158 targetLineEntry = entry; |
| 159 nextTargetLineIndex++; |
| 160 break; |
| 161 } else if (entry.line > lineNo) { |
| 162 break; |
| 163 } else { |
| 164 nextTargetLineIndex++; |
| 165 } |
| 166 } |
| 167 if (targetLineEntry != null) { |
| 168 int columnNo = 0; |
| 169 for (int index = 0; index < targetLineEntry.entries.length; index++) { |
| 170 TargetEntry entry = targetLineEntry.entries[index]; |
| 171 if (entry.column > columnNo) { |
| 172 write(line.substring(columnNo, entry.column), lastEntry); |
| 173 columnNo = entry.column; |
| 174 } |
| 175 state = |
| 176 entry.sourceUrlId != null ? state.nextState : MappingState.UNMAPPED; |
| 177 int end; |
| 178 if (index + 1 < targetLineEntry.entries.length) { |
| 179 end = targetLineEntry.entries[index + 1].column; |
| 180 } else { |
| 181 end = line.length; |
| 182 } |
| 183 write(line.substring(entry.column, end), entry); |
| 184 columnNo = end; |
| 185 } |
| 186 } else { |
| 187 write(line, lastEntry); |
| 188 } |
| 189 output.write('\n'); |
| 190 state = state.continuedState; |
| 191 } |
| 192 output.write('</pre></body></html>'); |
| 193 new File('out.js.map.html').writeAsStringSync(output.toString()); |
| 194 } |
OLD | NEW |