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 = /^\s*\/(?:\/|\*)[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+;)?base64,(.*)$/mg;
6
var mapFileCommentRx =
7
// //# sourceMappingURL=foo.js.map /*# sourceMappingURL=foo.js.map */
8
/(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/){1}[ \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
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
}
45
46
function convertFromLargeSource(content){
47
var lines = content.split('\n');
48
var line;
49
// find first line which contains a source map starting at end of content
50
for (var i = lines.length - 1; i > 0; i--) {
51
line = lines[i]
52
if (~line.indexOf('sourceMappingURL=data:')) return exports.fromComment(line);
53
}
54
}
55
56
Converter.prototype.toJSON = function (space) {
57
return JSON.stringify(this.sourcemap, null, space);
58
};
59
60
Converter.prototype.toBase64 = function () {
61
var json = this.toJSON();
62
return new Buffer(json).toString('base64');
63
};
64
65
Converter.prototype.toComment = function (options) {
66
var base64 = this.toBase64();
67
var data = 'sourceMappingURL=data:application/json;base64,' + base64;
68
return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
69
};
70
71
// returns copy instead of original
72
Converter.prototype.toObject = function () {
73
return JSON.parse(this.toJSON());
74
};
75
76
Converter.prototype.addProperty = function (key, value) {
77
if (this.sourcemap.hasOwnProperty(key)) throw new Error('property %s already exists on the sourcemap, use set property instead');
78
return this.setProperty(key, value);
79
};
80
81
Converter.prototype.setProperty = function (key, value) {
82
this.sourcemap[key] = value;
83
return this;
84
};
85
86
Converter.prototype.getProperty = function (key) {
87
return this.sourcemap[key];
88
};
89
90
exports.fromObject = function (obj) {
91
return new Converter(obj);
92
};
93
94
exports.fromJSON = function (json) {
95
return new Converter(json, { isJSON: true });
96
};
97
98
exports.fromBase64 = function (base64) {
99
return new Converter(base64, { isEncoded: true });
100
};
101
102
exports.fromComment = function (comment) {
103
comment = comment
104
.replace(/^\/\*/g, '//')
105
.replace(/\*\/$/g, '');
106
107
return new Converter(comment, { isEncoded: true, hasComment: true });
108
};
109
110
exports.fromMapFileComment = function (comment, dir) {
111
return new Converter(comment, { commentFileDir: dir, isFileComment: true, isJSON: true });
112
};
113
114
// Finds last sourcemap comment in file or returns null if none was found
115
exports.fromSource = function (content, largeSource) {
116
if (largeSource) return convertFromLargeSource(content);
117
118
var m = content.match(commentRx);
119
commentRx.lastIndex = 0;
120
return m ? exports.fromComment(m.pop()) : null;
121
};
122
123
// Finds last sourcemap comment in file or returns null if none was found
124
exports.fromMapFileSource = function (content, dir) {
125
var m = content.match(mapFileCommentRx);
126
mapFileCommentRx.lastIndex = 0;
127
return m ? exports.fromMapFileComment(m.pop(), dir) : null;
128
};
129
130
exports.removeComments = function (src) {
131
commentRx.lastIndex = 0;
132
return src.replace(commentRx, '');
133
};
134
135
exports.removeMapFileComments = function (src) {
136
mapFileCommentRx.lastIndex = 0;
137
return src.replace(mapFileCommentRx, '');
138
};
139
140
Object.defineProperty(exports, 'commentRegex', {
141
get: function getCommentRegex () {
142
commentRx.lastIndex = 0;
143
return commentRx;
144
}
145
});
146
147
Object.defineProperty(exports, 'mapFileCommentRegex', {
148
get: function getMapFileCommentRegex () {
149
mapFileCommentRx.lastIndex = 0;
150
return mapFileCommentRx;
151
}
152
});
153
154