-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
82 lines (72 loc) · 2.06 KB
/
index.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
// Copyright 2015, 2016 Simon Lydell
// X11 (“MIT”) Licensed. (See LICENSE.)
var fs = require('fs')
var opn = require('opn')
var sourceMapResolve = require('source-map-resolve')
var URL_BASE = 'https://sokra.github.io/source-map-visualization/'
exports.resolve = function (options, callback) {
fs.readFile(options.generatedFilePath, function (error, generatedContent) {
if (error) {
return callback(error)
}
generatedContent = generatedContent.toString()
var content = generatedContent
var filepath = options.generatedFilePath
if (typeof options.sourceMapPath === 'string') {
content = null
filepath = options.sourceMapPath
}
var resolveOptions = {
sourceRoot: options.sourceRoot
}
sourceMapResolve.resolve(
content, filepath, fs.readFile, resolveOptions,
function (error, result) {
if (error) {
return callback(error)
}
if (result === null) {
return callback(
new Error('No sourceMappingURL found in ' + filepath)
)
}
result.generatedContent = generatedContent
callback(null, result)
}
)
})
}
exports.createUrl = function (data) {
return URL_BASE + '#base64,' +
[data.generatedContent, JSON.stringify(data.map)]
.concat(data.sourcesContent).map(encode).join(',')
}
function encode (str) {
return new Buffer(unescape(encodeURIComponent(str))).toString('base64')
}
exports.displaySourceMap = function (map) {
if (typeof map === 'string') {
return map
} else {
return JSON.stringify(map, function (key, value) {
switch (key) {
case 'mappings':
return truncate(value, 70)
case 'sourcesContent':
return value.map(function (item) {
return truncate(item, 70)
})
default:
return value
}
}, 2)
}
}
function truncate (string, maxLength) {
if (typeof string === 'string' && string.length > maxLength) {
return string.slice(0, maxLength - 3) + '...'
} else {
return string
}
}
exports.open = opn