[6a3a178] | 1 | 'use strict';
|
---|
| 2 | var fs = require('fs');
|
---|
| 3 | var path = require('path');
|
---|
| 4 | var SafeBuffer = require('safe-buffer');
|
---|
| 5 |
|
---|
| 6 | Object.defineProperty(exports, 'commentRegex', {
|
---|
| 7 | get: function getCommentRegex () {
|
---|
| 8 | return /^\s*\/(?:\/|\*)[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+?;)?base64,(?:.*)$/mg;
|
---|
| 9 | }
|
---|
| 10 | });
|
---|
| 11 |
|
---|
| 12 | Object.defineProperty(exports, 'mapFileCommentRegex', {
|
---|
| 13 | get: function getMapFileCommentRegex () {
|
---|
| 14 | // Matches sourceMappingURL in either // or /* comment styles.
|
---|
| 15 | return /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"`]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/){1}[ \t]*$)/mg;
|
---|
| 16 | }
|
---|
| 17 | });
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | function decodeBase64(base64) {
|
---|
| 21 | return (SafeBuffer.Buffer.from(base64, 'base64') || "").toString();
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | function stripComment(sm) {
|
---|
| 25 | return sm.split(',').pop();
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | function readFromFileMap(sm, dir) {
|
---|
| 29 | // NOTE: this will only work on the server since it attempts to read the map file
|
---|
| 30 |
|
---|
| 31 | var r = exports.mapFileCommentRegex.exec(sm);
|
---|
| 32 |
|
---|
| 33 | // for some odd reason //# .. captures in 1 and /* .. */ in 2
|
---|
| 34 | var filename = r[1] || r[2];
|
---|
| 35 | var filepath = path.resolve(dir, filename);
|
---|
| 36 |
|
---|
| 37 | try {
|
---|
| 38 | return fs.readFileSync(filepath, 'utf8');
|
---|
| 39 | } catch (e) {
|
---|
| 40 | throw new Error('An error occurred while trying to read the map file at ' + filepath + '\n' + e);
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | function Converter (sm, opts) {
|
---|
| 45 | opts = opts || {};
|
---|
| 46 |
|
---|
| 47 | if (opts.isFileComment) sm = readFromFileMap(sm, opts.commentFileDir);
|
---|
| 48 | if (opts.hasComment) sm = stripComment(sm);
|
---|
| 49 | if (opts.isEncoded) sm = decodeBase64(sm);
|
---|
| 50 | if (opts.isJSON || opts.isEncoded) sm = JSON.parse(sm);
|
---|
| 51 |
|
---|
| 52 | this.sourcemap = sm;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | Converter.prototype.toJSON = function (space) {
|
---|
| 56 | return JSON.stringify(this.sourcemap, null, space);
|
---|
| 57 | };
|
---|
| 58 |
|
---|
| 59 | Converter.prototype.toBase64 = function () {
|
---|
| 60 | var json = this.toJSON();
|
---|
| 61 | return (SafeBuffer.Buffer.from(json, 'utf8') || "").toString('base64');
|
---|
| 62 | };
|
---|
| 63 |
|
---|
| 64 | Converter.prototype.toComment = function (options) {
|
---|
| 65 | var base64 = this.toBase64();
|
---|
| 66 | var data = 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + base64;
|
---|
| 67 | return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
|
---|
| 68 | };
|
---|
| 69 |
|
---|
| 70 | // returns copy instead of original
|
---|
| 71 | Converter.prototype.toObject = function () {
|
---|
| 72 | return JSON.parse(this.toJSON());
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | Converter.prototype.addProperty = function (key, value) {
|
---|
| 76 | if (this.sourcemap.hasOwnProperty(key)) throw new Error('property "' + key + '" already exists on the sourcemap, use set property instead');
|
---|
| 77 | return this.setProperty(key, value);
|
---|
| 78 | };
|
---|
| 79 |
|
---|
| 80 | Converter.prototype.setProperty = function (key, value) {
|
---|
| 81 | this.sourcemap[key] = value;
|
---|
| 82 | return this;
|
---|
| 83 | };
|
---|
| 84 |
|
---|
| 85 | Converter.prototype.getProperty = function (key) {
|
---|
| 86 | return this.sourcemap[key];
|
---|
| 87 | };
|
---|
| 88 |
|
---|
| 89 | exports.fromObject = function (obj) {
|
---|
| 90 | return new Converter(obj);
|
---|
| 91 | };
|
---|
| 92 |
|
---|
| 93 | exports.fromJSON = function (json) {
|
---|
| 94 | return new Converter(json, { isJSON: true });
|
---|
| 95 | };
|
---|
| 96 |
|
---|
| 97 | exports.fromBase64 = function (base64) {
|
---|
| 98 | return new Converter(base64, { isEncoded: true });
|
---|
| 99 | };
|
---|
| 100 |
|
---|
| 101 | exports.fromComment = function (comment) {
|
---|
| 102 | comment = comment
|
---|
| 103 | .replace(/^\/\*/g, '//')
|
---|
| 104 | .replace(/\*\/$/g, '');
|
---|
| 105 |
|
---|
| 106 | return new Converter(comment, { isEncoded: true, hasComment: true });
|
---|
| 107 | };
|
---|
| 108 |
|
---|
| 109 | exports.fromMapFileComment = function (comment, dir) {
|
---|
| 110 | return new Converter(comment, { commentFileDir: dir, isFileComment: true, isJSON: true });
|
---|
| 111 | };
|
---|
| 112 |
|
---|
| 113 | // Finds last sourcemap comment in file or returns null if none was found
|
---|
| 114 | exports.fromSource = function (content) {
|
---|
| 115 | var m = content.match(exports.commentRegex);
|
---|
| 116 | return m ? exports.fromComment(m.pop()) : null;
|
---|
| 117 | };
|
---|
| 118 |
|
---|
| 119 | // Finds last sourcemap comment in file or returns null if none was found
|
---|
| 120 | exports.fromMapFileSource = function (content, dir) {
|
---|
| 121 | var m = content.match(exports.mapFileCommentRegex);
|
---|
| 122 | return m ? exports.fromMapFileComment(m.pop(), dir) : null;
|
---|
| 123 | };
|
---|
| 124 |
|
---|
| 125 | exports.removeComments = function (src) {
|
---|
| 126 | return src.replace(exports.commentRegex, '');
|
---|
| 127 | };
|
---|
| 128 |
|
---|
| 129 | exports.removeMapFileComments = function (src) {
|
---|
| 130 | return src.replace(exports.mapFileCommentRegex, '');
|
---|
| 131 | };
|
---|
| 132 |
|
---|
| 133 | exports.generateMapFileComment = function (file, options) {
|
---|
| 134 | var data = 'sourceMappingURL=' + file;
|
---|
| 135 | return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
|
---|
| 136 | };
|
---|