-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.js
69 lines (55 loc) · 1.97 KB
/
helpers.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const path = require('path');
const SourceMapGenerator = require('source-map').SourceMapGenerator;
const SourceMapConsumer = require('source-map').SourceMapConsumer;
function startsWithSingleDot(fpath) {
const first2chars = fpath.slice(0, 2);
return first2chars === '.' + path.sep || first2chars === './';
}
function replace(npath, ext) {
if (typeof npath !== 'string' || npath.length === 0) {
return npath;
}
const nFileName = path.basename(npath, path.extname(npath)) + ext;
const nFilepath = path.join(path.dirname(npath), nFileName);
if (startsWithSingleDot(npath)) {
return '.' + path.sep + nFilepath;
}
return nFilepath;
}
function replaceExtension(fp) {
return path.extname(fp) ? replace(fp, '.js') : fp;
}
function applySourceMap(file, sourceMap) {
if (typeof sourceMap === 'string' || sourceMap instanceof String) {
sourceMap = JSON.parse(sourceMap);
}
if (file.sourceMap && (typeof file.sourceMap === 'string' || file.sourceMap instanceof String)) {
file.sourceMap = JSON.parse(file.sourceMap);
}
// check source map properties
assertProperty(sourceMap, "file");
assertProperty(sourceMap, "mappings");
assertProperty(sourceMap, "sources");
// fix paths if Windows style paths
sourceMap.file = sourceMap.file.replace(/\\/g, '/');
sourceMap.sources = sourceMap.sources.map(function(filePath) {
return filePath.replace(/\\/g, '/');
});
if (file.sourceMap && file.sourceMap.mappings !== '') {
const generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(sourceMap));
generator.applySourceMap(new SourceMapConsumer(file.sourceMap));
file.sourceMap = JSON.parse(generator.toString());
} else {
file.sourceMap = sourceMap;
}
};
function assertProperty(sourceMap, propertyName) {
if (!sourceMap.hasOwnProperty(propertyName)) {
const e = new Error('Source map to be applied is missing the \"' + propertyName + '\" property');
throw e;
}
}
module.exports = {
replaceExtension,
applySourceMap
}