UNPKG

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