Skip to content

Commit 1ce7200

Browse files
committed
major refactoring
more performance side-by-side view
1 parent 863df53 commit 1ce7200

File tree

24 files changed

+684
-325
lines changed

24 files changed

+684
-325
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/node_modules
2-
/assets
2+
/build

app/app.jade

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,12 @@
44
h1 source-map-visualization
55
p
66
each kind in kinds
7-
a(href="#"+kind).btn.example(data-example=kind)= kind
7+
a.btn.example(href="#"+kind, data-example=kind)= kind
88
= " "
99
a(href="#custom-choose", title="You can also drag'n'drop all files into this page").btn.custom custom...
1010
= " "
1111
a.custom-link
12-
.row
13-
.span6
14-
pre: code.original Original source code
15-
.span6
16-
pre: code.generated Generated code
17-
.row
18-
.span12
19-
pre: code.mappings List of mappings
12+
pre: code.visu
2013
.custom-modal.modal.fade.hide
2114
.modal-header
2215
a.close(data-dismiss="modal") ×

app/app.js

Lines changed: 12 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
require("imports?this=>window!jquery-hashchange");
22
var SourceMap = require("source-map");
33
var UglifyJS = require("./uglify-js");
4+
var generateHtml = require("./generateHtml");
45

5-
var exampleKinds = ["coffee", "simple-coffee", "coffee-redux", "simple-coffee-redux", "typescript"];
6-
var LINESTYLES = 5;
6+
var exampleKinds = ["coffee", "simple-coffee", "typescript", "babel"];
77
var SOURCE_MAPPING_URL_REG_EXP = /\/\/[@#]\s*sourceMappingURL\s*=\s*data:[^\n]*?base64,([^\n]*)/;
88
var SOURCE_MAPPING_URL_REG_EXP2 = /\/\*\s*[@#]\s*sourceMappingURL\s*=\s*data:[^\n]*?base64,([^\n]*)\s*\*\//;
99

@@ -280,192 +280,13 @@ $(function() {
280280
}).join(",")).text("Link to this");
281281
}
282282
function loadExample(sources, exampleJs, exampleMap) {
283-
var generated = $(".generated").hide().text("");
284-
var original = $(".original").hide().text("");
285-
var mappings = $(".mappings").hide().text("1: ");
283+
var visu = $(".visu").hide().text("");
286284

287285
try {
288286
exampleMap.file = exampleMap.file || "example.js";
289287
var map = new SourceMap.SourceMapConsumer(exampleMap);
290-
var mapSources = map.sources;
291-
292-
var generatedLine = 1;
293-
var nodes = SourceMap.SourceNode.fromStringWithSourceMap(exampleJs, map).children;
294-
nodes.forEach(function(item, idx) {
295-
if(generatedLine > 1000) return;
296-
if(typeof item === "string") {
297-
generated.append($("<span>").text(item));
298-
generatedLine += item.split("\n").length - 1;
299-
} else {
300-
var str = item.toString();
301-
var source = mapSources.indexOf(item.source);
302-
generated.append($("<span>")
303-
.addClass("generated-item")
304-
.addClass("item-" + source + "-" + item.line + "-" + item.column)
305-
.attr("title", item.name)
306-
.data("source", source)
307-
.data("line", item.line)
308-
.data("column", item.column)
309-
.addClass("style-" + (item.line%LINESTYLES))
310-
.text(str));
311-
generatedLine += str.split("\n").length - 1;
312-
}
313-
});
314-
315-
316-
var lastGenLine = 1;
317-
var lastOrgSource = "";
318-
map.eachMapping(function(mapping) {
319-
if(mapping.generatedLine > 1000) return;
320-
while(lastGenLine < mapping.generatedLine) {
321-
mappings.append($("<br>"));
322-
lastGenLine++;
323-
mappings.append($("<span>").text(lastGenLine + ": "));
324-
}
325-
if(typeof mapping.originalLine == "number") {
326-
if(lastOrgSource !== mapping.source && mapSources.length > 1) {
327-
mappings.append($("<span>").text("[" + mapping.source + "] "));
328-
lastOrgSource = mapping.source;
329-
}
330-
var source = mapSources.indexOf(mapping.source);
331-
mappings.append(
332-
$("<span>")
333-
.text(mapping.generatedColumn + "->" + mapping.originalLine + ":" + mapping.originalColumn)
334-
.addClass("mapping-item")
335-
.addClass("item-" + source + "-" + mapping.originalLine + "-" + mapping.originalColumn)
336-
.data("source", source)
337-
.data("line", mapping.originalLine)
338-
.data("column", mapping.originalColumn)
339-
.addClass("style-" + (mapping.originalLine%LINESTYLES))
340-
);
341-
} else
342-
mappings.append($("<span>").text(mapping.generatedColumn).addClass("mapping-item"));
343-
mappings.append($("<span>").text(" "));
344-
});
345-
346-
347-
var line = 1, column = 0, currentOutputLine = 1, targetOutputLine = -1, limited = false;
348-
var lastMapping = null;
349-
var currentSource = null;
350-
var exampleLines;
351-
var mappingsBySource = {};
352-
map.eachMapping(function(mapping) {
353-
if(typeof mapping.originalLine !== "number") return;
354-
if(mapping.generatedLine > 1000) return limited = true;
355-
if(!mappingsBySource[mapping.source]) mappingsBySource[mapping.source] = [];
356-
mappingsBySource[mapping.source].push(mapping);
357-
}, undefined, SourceMap.SourceMapConsumer.ORIGINAL_ORDER);
358-
Object.keys(mappingsBySource).map(function(source) {
359-
return [source, mappingsBySource[source][0].generatedLine];
360-
}).sort(function(a, b) {
361-
return a[1] - b[1];
362-
}).forEach(function(arr) {
363-
var source = arr[0];
364-
var mappings = mappingsBySource[source];
365-
366-
if(currentSource) endFile();
367-
lastMapping = null;
368-
line = 1;
369-
column = 0;
370-
targetOutputLine = -1;
371-
if(mapSources.length > 1)
372-
currentOutputLine += 2;
373-
var startLine = mappings.map(function(mapping) {
374-
return mapping.generatedLine - mapping.originalLine + 1;
375-
}).sort(function(a, b) { return a - b });
376-
startLine = startLine[0];
377-
while(currentOutputLine < startLine) {
378-
original.append($("<span>").text("\n"));
379-
currentOutputLine++;
380-
}
381-
if(mapSources.length > 1)
382-
original.append($("<h4>")
383-
.text(source));
384-
var exampleSource = sources[mapSources.indexOf(source)];
385-
if(!exampleSource) throw new Error("Source '" + source + "' missing");
386-
exampleLines = exampleSource.split("\n");
387-
currentSource = source;
388-
mappings.forEach(function(mapping, idx) {
389-
if(lastMapping) {
390-
var source = mapSources.indexOf(lastMapping.source);
391-
if(line < mapping.originalLine) {
392-
original.append($("<span>")
393-
.addClass("original-item")
394-
.addClass("item-" + source + "-" + lastMapping.originalLine + "-" + lastMapping.originalColumn)
395-
.data("source", source)
396-
.data("line", lastMapping.originalLine)
397-
.data("column", lastMapping.originalColumn)
398-
.addClass("style-" + (lastMapping.originalLine%LINESTYLES))
399-
.text(exampleLines.shift() + "\n"));
400-
line++; column = 0;
401-
currentOutputLine++;
402-
while(line < mapping.originalLine) {
403-
original.append($("<span>").text(exampleLines.shift() + "\n"));
404-
line++; column = 0;
405-
currentOutputLine++;
406-
}
407-
startLine = [];
408-
for(var i = idx; i < mappings.length && mappings[i].originalLine <= mapping.originalLine + 1; i++) {
409-
startLine.push(mappings[i].generatedLine - mappings[i].originalLine + mapping.originalLine);
410-
}
411-
startLine.sort(function(a, b) { return a - b });
412-
startLine = startLine[0];
413-
while(startLine && currentOutputLine < startLine) {
414-
original.append($("<span>").text("~\n"));
415-
currentOutputLine++;
416-
}
417-
if(column < mapping.originalColumn) {
418-
original.append($("<span>").text(shiftColumns(mapping.originalColumn - column)));
419-
}
420-
}
421-
if(mapping.originalColumn > column) {
422-
original.append($("<span>")
423-
.addClass("original-item")
424-
.addClass("item-" + source + "-" + lastMapping.originalLine + "-" + lastMapping.originalColumn)
425-
.data("source", source)
426-
.data("line", lastMapping.originalLine)
427-
.data("column", lastMapping.originalColumn)
428-
.addClass("style-" + (lastMapping.originalLine%LINESTYLES))
429-
.text(shiftColumns(mapping.originalColumn - column)));
430-
}
431-
} else {
432-
while(line < mapping.originalLine) {
433-
original.append($("<span>").text(exampleLines.shift() + "\n"));
434-
line++; column = 0;
435-
}
436-
if(column < mapping.originalColumn) {
437-
original.append($("<span>").text(shiftColumns(mapping.originalColumn - column)));
438-
}
439-
}
440-
lastMapping = mapping;
441-
});
442-
});
443-
function endFile() {
444-
if(lastMapping) {
445-
var source = mapSources.indexOf(lastMapping.source);
446-
original.append($("<span>")
447-
.addClass("original-item")
448-
.addClass("item-" + source + "-" + lastMapping.originalLine + "-" + lastMapping.originalColumn)
449-
.data("source", source)
450-
.data("line", lastMapping.originalLine)
451-
.data("column", lastMapping.originalColumn)
452-
.addClass("style-" + (lastMapping.originalLine%LINESTYLES))
453-
.text(exampleLines.shift()));
454-
}
455-
if(!limited) {
456-
exampleLines.forEach(function(line) {
457-
original.append($("<span>").text("\n" + line));
458-
});
459-
}
460-
}
461-
endFile();
462-
463-
function shiftColumns(count) {
464-
var nextLine = exampleLines[0];
465-
exampleLines[0] = nextLine.substr(count);
466-
column += count;
467-
return nextLine.substr(0, count);
468-
}
288+
289+
visu.html(generateHtml(map, exampleJs, sources));
469290

470291

471292

@@ -489,15 +310,18 @@ $(function() {
489310
elem.scrollIntoViewIfNeeded();
490311
});
491312

492-
generated.append($("<br>"));
493-
generated.append($("<br>"));
494-
generated.append($("<button>")
313+
visu.append($("<br>"));
314+
visu.append($("<br>"));
315+
visu.append($("<button>")
495316
.addClass("btn btn-primary")
496317
.text("minimize")
497318
.attr("title", "Minimize the file with uglify-js and combine the SourceMaps.")
498319
.click(function() {
499320
var result = UglifyJS.minify(exampleJs, {
500321
outSourceMap: "example.map",
322+
output: {
323+
beautify: true
324+
}
501325
});
502326
var minmap = JSON.parse(result.map);
503327
minmap.file = "example";
@@ -508,22 +332,14 @@ $(function() {
508332
minmap.applySourceMap(map, "?");
509333
minmap = minmap.toJSON();
510334
var idx = minmap.sources.indexOf("?");
511-
if(idx >= 0) {
512-
var name = "example.js";
513-
while(minmap.sources.indexOf(name) >= 0)
514-
name += "*";
515-
minmap.sources[idx] = name;
516-
}
517335

518336
loadExample(minmap.sourcesContent, result.code, minmap);
519337
oldHash = window.location.hash = "custom";
520338
}));
521339
} catch(e) {
522340
throw e;
523341
} finally {
524-
generated.show();
525-
original.show(),
526-
mappings.show();
342+
visu.show();
527343
}
528344
}
529345
});

app/app.less

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,13 @@ pre code {
3434
white-space: pre-wrap;
3535
word-break: normal;
3636
word-wrap: normal;
37+
}
38+
39+
table {
40+
width: 100%;
41+
}
42+
43+
tr, td {
44+
margin: 0;
45+
width: 33%;
3746
}

0 commit comments

Comments
 (0)