UNPKG

6.55 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, 'commentRegex', {
4 get: function getCommentRegex () {
5 // Groups: 1: media type, 2: MIME type, 3: charset, 4: encoding, 5: data.
6 return /^\s*?\/[\/\*][@#]\s+?sourceMappingURL=data:(((?:application|text)\/json)(?:;charset=([^;,]+?)?)?)?(?:;(base64))?,(.*?)$/mg;
7 }
8});
9
10
11Object.defineProperty(exports, 'mapFileCommentRegex', {
12 get: function getMapFileCommentRegex () {
13 // Matches sourceMappingURL in either // or /* comment styles.
14 return /(?:\/\/[@#][ \t]+?sourceMappingURL=([^\s'"`]+?)[ \t]*?$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^*]+?)[ \t]*?(?:\*\/){1}[ \t]*?$)/mg;
15 }
16});
17
18var decodeBase64;
19if (typeof Buffer !== 'undefined') {
20 if (typeof Buffer.from === 'function') {
21 decodeBase64 = decodeBase64WithBufferFrom;
22 } else {
23 decodeBase64 = decodeBase64WithNewBuffer;
24 }
25} else {
26 decodeBase64 = decodeBase64WithAtob;
27}
28
29function decodeBase64WithBufferFrom(base64) {
30 return Buffer.from(base64, 'base64').toString();
31}
32
33function decodeBase64WithNewBuffer(base64) {
34 if (typeof value === 'number') {
35 throw new TypeError('The value to decode must not be of type number.');
36 }
37 return new Buffer(base64, 'base64').toString();
38}
39
40function decodeBase64WithAtob(base64) {
41 return decodeURIComponent(escape(atob(base64)));
42}
43
44function stripComment(sm) {
45 return sm.split(',').pop();
46}
47
48function readFromFileMap(sm, read) {
49 var r = exports.mapFileCommentRegex.exec(sm);
50 // for some odd reason //# .. captures in 1 and /* .. */ in 2
51 var filename = r[1] || r[2];
52
53 try {
54 var sm = read(filename);
55 if (sm != null && typeof sm.catch === 'function') {
56 return sm.catch(throwError);
57 } else {
58 return sm;
59 }
60 } catch (e) {
61 throwError(e);
62 }
63
64 function throwError(e) {
65 throw new Error('An error occurred while trying to read the map file at ' + filename + '\n' + e.stack);
66 }
67}
68
69function Converter (sm, opts) {
70 opts = opts || {};
71
72 if (opts.hasComment) {
73 sm = stripComment(sm);
74 }
75
76 if (opts.encoding === 'base64') {
77 sm = decodeBase64(sm);
78 } else if (opts.encoding === 'uri') {
79 sm = decodeURIComponent(sm);
80 }
81
82 if (opts.isJSON || opts.encoding) {
83 sm = JSON.parse(sm);
84 }
85
86 this.sourcemap = sm;
87}
88
89Converter.prototype.toJSON = function (space) {
90 return JSON.stringify(this.sourcemap, null, space);
91};
92
93if (typeof Buffer !== 'undefined') {
94 if (typeof Buffer.from === 'function') {
95 Converter.prototype.toBase64 = encodeBase64WithBufferFrom;
96 } else {
97 Converter.prototype.toBase64 = encodeBase64WithNewBuffer;
98 }
99} else {
100 Converter.prototype.toBase64 = encodeBase64WithBtoa;
101}
102
103function encodeBase64WithBufferFrom() {
104 var json = this.toJSON();
105 return Buffer.from(json, 'utf8').toString('base64');
106}
107
108function encodeBase64WithNewBuffer() {
109 var json = this.toJSON();
110 if (typeof json === 'number') {
111 throw new TypeError('The json to encode must not be of type number.');
112 }
113 return new Buffer(json, 'utf8').toString('base64');
114}
115
116function encodeBase64WithBtoa() {
117 var json = this.toJSON();
118 return btoa(unescape(encodeURIComponent(json)));
119}
120
121Converter.prototype.toURI = function () {
122 var json = this.toJSON();
123 return encodeURIComponent(json);
124};
125
126Converter.prototype.toComment = function (options) {
127 var encoding, content, data;
128 if (options != null && options.encoding === 'uri') {
129 encoding = '';
130 content = this.toURI();
131 } else {
132 encoding = ';base64';
133 content = this.toBase64();
134 }
135 data = 'sourceMappingURL=data:application/json;charset=utf-8' + encoding + ',' + content;
136 return options != null && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
137};
138
139// returns copy instead of original
140Converter.prototype.toObject = function () {
141 return JSON.parse(this.toJSON());
142};
143
144Converter.prototype.addProperty = function (key, value) {
145 if (this.sourcemap.hasOwnProperty(key)) throw new Error('property "' + key + '" already exists on the sourcemap, use set property instead');
146 return this.setProperty(key, value);
147};
148
149Converter.prototype.setProperty = function (key, value) {
150 this.sourcemap[key] = value;
151 return this;
152};
153
154Converter.prototype.getProperty = function (key) {
155 return this.sourcemap[key];
156};
157
158exports.fromObject = function (obj) {
159 return new Converter(obj);
160};
161
162exports.fromJSON = function (json) {
163 return new Converter(json, { isJSON: true });
164};
165
166exports.fromURI = function (uri) {
167 return new Converter(uri, { encoding: 'uri' });
168};
169
170exports.fromBase64 = function (base64) {
171 return new Converter(base64, { encoding: 'base64' });
172};
173
174exports.fromComment = function (comment) {
175 var m, encoding;
176 comment = comment
177 .replace(/^\/\*/g, '//')
178 .replace(/\*\/$/g, '');
179 m = exports.commentRegex.exec(comment);
180 encoding = m && m[4] || 'uri';
181 return new Converter(comment, { encoding: encoding, hasComment: true });
182};
183
184function makeConverter(sm) {
185 return new Converter(sm, { isJSON: true });
186}
187
188exports.fromMapFileComment = function (comment, read) {
189 if (typeof read === 'string') {
190 throw new Error(
191 'String directory paths are no longer supported with `fromMapFileComment`\n' +
192 'Please review the Upgrading documentation at https://github.com/thlorenz/convert-source-map#upgrading'
193 )
194 }
195
196 var sm = readFromFileMap(comment, read);
197 if (sm != null && typeof sm.then === 'function') {
198 return sm.then(makeConverter);
199 } else {
200 return makeConverter(sm);
201 }
202};
203
204// Finds last sourcemap comment in file or returns null if none was found
205exports.fromSource = function (content) {
206 var m = content.match(exports.commentRegex);
207 return m ? exports.fromComment(m.pop()) : null;
208};
209
210// Finds last sourcemap comment in file or returns null if none was found
211exports.fromMapFileSource = function (content, read) {
212 if (typeof read === 'string') {
213 throw new Error(
214 'String directory paths are no longer supported with `fromMapFileSource`\n' +
215 'Please review the Upgrading documentation at https://github.com/thlorenz/convert-source-map#upgrading'
216 )
217 }
218 var m = content.match(exports.mapFileCommentRegex);
219 return m ? exports.fromMapFileComment(m.pop(), read) : null;
220};
221
222exports.removeComments = function (src) {
223 return src.replace(exports.commentRegex, '');
224};
225
226exports.removeMapFileComments = function (src) {
227 return src.replace(exports.mapFileCommentRegex, '');
228};
229
230exports.generateMapFileComment = function (file, options) {
231 var data = 'sourceMappingURL=' + file;
232 return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
233};