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 |
|
8 | /(?:\/\/[@#][ \t]+sourceMappingURL=(.+?)[ \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 |
|
20 |
|
21 | var r = mapFileCommentRx.exec(sm);
|
22 | mapFileCommentRx.lastIndex = 0;
|
23 |
|
24 |
|
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 | function convertFromLargeSource(content){
|
51 | var lines = content.split('\n');
|
52 | var line;
|
53 |
|
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 |
|
60 | Converter.prototype.toJSON = function (space) {
|
61 | return JSON.stringify(this.sourcemap, null, space);
|
62 | };
|
63 |
|
64 | Converter.prototype.toBase64 = function () {
|
65 | var json = this.toJSON();
|
66 | return new Buffer(json).toString('base64');
|
67 | };
|
68 |
|
69 | Converter.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 |
|
76 | Converter.prototype.toObject = function () {
|
77 | return JSON.parse(this.toJSON());
|
78 | };
|
79 |
|
80 | Converter.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 |
|
85 | Converter.prototype.setProperty = function (key, value) {
|
86 | this.sourcemap[key] = value;
|
87 | return this;
|
88 | };
|
89 |
|
90 | Converter.prototype.getProperty = function (key) {
|
91 | return this.sourcemap[key];
|
92 | };
|
93 |
|
94 | exports.fromObject = function (obj) {
|
95 | return new Converter(obj);
|
96 | };
|
97 |
|
98 | exports.fromJSON = function (json) {
|
99 | return new Converter(json, { isJSON: true });
|
100 | };
|
101 |
|
102 | exports.fromBase64 = function (base64) {
|
103 | return new Converter(base64, { isEncoded: true });
|
104 | };
|
105 |
|
106 | exports.fromComment = function (comment) {
|
107 | comment = comment
|
108 | .replace(/^\/\*/g, '//')
|
109 | .replace(/\*\/$/g, '');
|
110 |
|
111 | return new Converter(comment, { isEncoded: true, hasComment: true });
|
112 | };
|
113 |
|
114 | exports.fromMapFileComment = function (comment, dir) {
|
115 | return new Converter(comment, { commentFileDir: dir, isFileComment: true, isJSON: true });
|
116 | };
|
117 |
|
118 |
|
119 | exports.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 |
|
128 | exports.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 |
|
134 | exports.removeComments = function (src) {
|
135 | commentRx.lastIndex = 0;
|
136 | return src.replace(commentRx, '');
|
137 | };
|
138 |
|
139 | exports.removeMapFileComments = function (src) {
|
140 | mapFileCommentRx.lastIndex = 0;
|
141 | return src.replace(mapFileCommentRx, '');
|
142 | };
|
143 |
|
144 | exports.__defineGetter__('commentRegex', function () {
|
145 | commentRx.lastIndex = 0;
|
146 | return commentRx;
|
147 | });
|
148 |
|
149 | exports.__defineGetter__('mapFileCommentRegex', function () {
|
150 | mapFileCommentRx.lastIndex = 0;
|
151 | return mapFileCommentRx;
|
152 | });
|