forked from paazmaya/shuji
-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.js
46 lines (40 loc) · 1.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* reverse-sourcemap
* https://github.com/davidkevork/reverse-sourcemap
*
* Reverse engineering JavaScript and CSS sources from sourcemaps
*
* Copyright (c) Juga Paazmaya <[email protected]> (https://paazmaya.fi)
* Copyright (c) David Kevork <[email protected]> (https://davidkevork.me)
* Licensed under the MIT license
*/
'use strict';
const path = require('path');
const sourceMap = require('source-map');
/**
* @param {string} input Contents of the sourcemap file
* @param {object} options Object {verbose: boolean}
*
* @returns {object} Source contents mapped to file names
*/
module.exports = (input, options) => {
const consumer = new sourceMap.SourceMapConsumer(input);
return consumer.then((response) => {
let map = {};
if (response.hasContentsOfAllSources()) {
if (options.verbose) {
console.log('All sources were included in the sourcemap');
}
response.sources.forEach((source) => {
const contents = response.sourceContentFor(source);
map[path.normalize(source).replace(/^(\.\.[/\\])+/, '').replace(/[|\&#,+()?$~%'":*?<>{}]/g, '').replace(' ', '.')] = contents;
});
} else if (options.verbose) {
console.log('Not all sources were included in the sourcemap');
}
return map;
}).catch((e) => {
console.log(e);
return {};
});
};