Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80552 views
1
'use strict';
2
var fs = require('fs');
3
var path = require('path');
4
5
var commentRx = /^[ \t]*(?:\/\/|\/\*)[@#][ \t]+sourceMappingURL=data:(?:application|text)\/json;base64,(.+)(?:\*\/)?/mg;
6
var mapFileCommentRx =
7
// //# sourceMappingURL=foo.js.map /*# sourceMappingURL=foo.js.map */
8
/(?:^[ \t]*\/\/[@|#][ \t]+sourceMappingURL=(.+?)[ \t]*$)|(?:^[ \t]*\/\*[@#][ \t]+sourceMappingURL=(.+?)[ \t]*\*\/[ \t]*$)/mg
9
10
function decodeBase64(base64) {
11
return new Buffer(base64, 'base64').toString();
12
}
13
14
function stripComment(sm) {
15
return sm.split(',').pop();
16
}
17
18
function readFromFileMap(sm, dir) {
19
// NOTE: this will only work on the server since it attempts to read the map file
20
21
var r = mapFileCommentRx.exec(sm);
22
mapFileCommentRx.lastIndex = 0;
23
24
// for some odd reason //# .. captures in 1 and /* .. */ in 2
25
var filename = r[1] || r[2];
26
var filepath = path.join(dir, filename);
27
28
try {
29
return fs.readFileSync(filepath, 'utf8');
30
} catch (e) {
31
throw new Error('An error occurred while trying to read the map file at ' + filepath + '\n' + e);
32
}
33
}
34
35
function Converter (sm, opts) {
36
opts = opts || {};
37
try {
38
if (opts.isFileComment) sm = readFromFileMap(sm, opts.commentFileDir);
39
if (opts.hasComment) sm = stripComment(sm);
40
if (opts.isEncoded) sm = decodeBase64(sm);
41
if (opts.isJSON || opts.isEncoded) sm = JSON.parse(sm);
42
43
this.sourcemap = sm;
44
} catch(e) {
45
console.error(e);
46
return null;
47
}
48
}
49
50
Converter.prototype.toJSON = function (space) {
51
return JSON.stringify(this.sourcemap, null, space);
52
};
53
54
Converter.prototype.toBase64 = function () {
55
var json = this.toJSON();
56
return new Buffer(json).toString('base64');
57
};
58
59
Converter.prototype.toComment = function () {
60
var base64 = this.toBase64();
61
return '//# sourceMappingURL=data:application/json;base64,' + base64;
62
};
63
64
// returns copy instead of original
65
Converter.prototype.toObject = function () {
66
return JSON.parse(this.toJSON());
67
};
68
69
Converter.prototype.addProperty = function (key, value) {
70
if (this.sourcemap.hasOwnProperty(key)) throw new Error('property %s already exists on the sourcemap, use set property instead');
71
return this.setProperty(key, value);
72
};
73
74
Converter.prototype.setProperty = function (key, value) {
75
this.sourcemap[key] = value;
76
return this;
77
};
78
79
Converter.prototype.getProperty = function (key) {
80
return this.sourcemap[key];
81
};
82
83
exports.fromObject = function (obj) {
84
return new Converter(obj);
85
};
86
87
exports.fromJSON = function (json) {
88
return new Converter(json, { isJSON: true });
89
};
90
91
exports.fromBase64 = function (base64) {
92
return new Converter(base64, { isEncoded: true });
93
};
94
95
exports.fromComment = function (comment) {
96
comment = comment
97
.replace(/^\/\*/g, '//')
98
.replace(/\*\/$/g, '');
99
100
return new Converter(comment, { isEncoded: true, hasComment: true });
101
};
102
103
exports.fromMapFileComment = function (comment, dir) {
104
return new Converter(comment, { commentFileDir: dir, isFileComment: true, isJSON: true });
105
};
106
107
// Finds last sourcemap comment in file or returns null if none was found
108
exports.fromSource = function (content) {
109
var m = content.match(commentRx);
110
commentRx.lastIndex = 0;
111
return m ? exports.fromComment(m.pop()) : null;
112
};
113
114
// Finds last sourcemap comment in file or returns null if none was found
115
exports.fromMapFileSource = function (content, dir) {
116
var m = content.match(mapFileCommentRx);
117
mapFileCommentRx.lastIndex = 0;
118
return m ? exports.fromMapFileComment(m.pop(), dir) : null;
119
};
120
121
exports.removeComments = function (src) {
122
commentRx.lastIndex = 0;
123
return src.replace(commentRx, '');
124
};
125
126
exports.removeMapFileComments = function (src) {
127
mapFileCommentRx.lastIndex = 0;
128
return src.replace(mapFileCommentRx, '');
129
};
130
131
exports.__defineGetter__('commentRegex', function () {
132
commentRx.lastIndex = 0;
133
return commentRx;
134
});
135
136
exports.__defineGetter__('mapFileCommentRegex', function () {
137
mapFileCommentRx.lastIndex = 0;
138
return mapFileCommentRx;
139
});
140
141