forked from NVE/regObs4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattach-source-maps.js
More file actions
45 lines (37 loc) · 1.31 KB
/
attach-source-maps.js
File metadata and controls
45 lines (37 loc) · 1.31 KB
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
let fs = require('fs');
let path = require('path');
const TARGET_DIR = 'www';
function getFiles() {
return fs.readdirSync(TARGET_DIR);
}
function addBase64SourceMaps() {
console.log('===========================');
console.log('attaching bas64 source maps');
console.log('===========================');
getFiles().forEach(file => {
const mapFile = path.join(TARGET_DIR, file + '.map');
const targetFile = path.join(TARGET_DIR, file);
if (path.extname(file) === '.js' && fs.existsSync(mapFile)) {
const bufMap = fs.readFileSync(mapFile).toString('base64');
const bufFile = fs.readFileSync(targetFile, "utf8");
const result = bufFile.replace('sourceMappingURL=' + file + '.map', 'sourceMappingURL=data:application/json;charset=utf-8;base64,' + bufMap);
fs.writeFileSync(targetFile, result);
}
});
}
function deleteSourceMaps() {
console.log('===========================');
console.log('deleteing source map files ');
console.log('===========================');
fs.readdirSync(TARGET_DIR).filter(f => path.extname(f) === '.map').forEach((file) => {
const targetFile = path.join(TARGET_DIR, file);
fs.unlinkSync(targetFile);
});
}
module.exports = function (ctx) {
if (ctx.argv.includes('--release')) {
deleteSourceMaps();
} else {
addBase64SourceMaps();
}
};