UNPKG

3.98 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
50Converter.prototype.toJSON = function (space) {
51 return JSON.stringify(this.sourcemap, null, space);
52};
53
54Converter.prototype.toBase64 = function () {
55 var json = this.toJSON();
56 return new Buffer(json).toString('base64');
57};
58
59Converter.prototype.toComment = function () {
60 var base64 = this.toBase64();
61 return '//# sourceMappingURL=data:application/json;base64,' + base64;
62};
63
64// returns copy instead of original
65Converter.prototype.toObject = function () {
66 return JSON.parse(this.toJSON());
67};
68
69Converter.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
74Converter.prototype.setProperty = function (key, value) {
75 this.sourcemap[key] = value;
76 return this;
77};
78
79Converter.prototype.getProperty = function (key) {
80 return this.sourcemap[key];
81};
82
83exports.fromObject = function (obj) {
84 return new Converter(obj);
85};
86
87exports.fromJSON = function (json) {
88 return new Converter(json, { isJSON: true });
89};
90
91exports.fromBase64 = function (base64) {
92 return new Converter(base64, { isEncoded: true });
93};
94
95exports.fromComment = function (comment) {
96 comment = comment
97 .replace(/^\/\*/g, '//')
98 .replace(/\*\/$/g, '');
99
100 return new Converter(comment, { isEncoded: true, hasComment: true });
101};
102
103exports.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
108exports.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
115exports.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
121exports.removeComments = function (src) {
122 commentRx.lastIndex = 0;
123 return src.replace(commentRx, '');
124};
125
126exports.removeMapFileComments = function (src) {
127 mapFileCommentRx.lastIndex = 0;
128 return src.replace(mapFileCommentRx, '');
129};
130
131exports.__defineGetter__('commentRegex', function () {
132 commentRx.lastIndex = 0;
133 return commentRx;
134});
135
136exports.__defineGetter__('mapFileCommentRegex', function () {
137 mapFileCommentRx.lastIndex = 0;
138 return mapFileCommentRx;
139});