-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuglify.js
79 lines (67 loc) · 2.64 KB
/
uglify.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
70
71
72
73
74
75
76
77
78
79
const fs = require("fs");
const uuidv4 = require('uuid/v4');
const fileSystem = require("./filesystem");
const { execSync } = require('child_process');
// stderr is sent to stderr of parent process
// you can set options.stdio if you want it to go elsewhere
// execSync('rm -rf scripts/*');
const regexPattern = /(require\([\'\"])(.+)([\'\"]\))/gm; // .js de olabilir
const uglifier = ({
paths,
keeps,
prefix
}) => {
let files = {};
for(var inputFolder in paths) {
console.log("=============== " + paths[inputFolder] + " ===============");
fileSystem.getAllFiles(fileSystem.scriptsPath + paths[inputFolder]).forEach(function(file){
files[file.absolutePath] = prefix + uuidv4();
console.log(file.absolutePath + " === " + files[file.absolutePath]);
});
}
findAndReplaceAllRequiredPages(files, keeps);
writeMappingFile(files);
removeInputFolders(paths);
};
module.exports = uglifier;
function removeInputFolders(paths) {
for(var inputFolder in paths) {
execSync(`rm -rf ${paths[inputFolder]}`);
}
}
function writeMappingFile(files){
let fileName = "file_mapping.json";
if(fs.existsSync(fileName)){
fs.unlinkSync(fileName);
}
fs.appendFileSync(fileName, JSON.stringify(files));
}
function findAndReplaceAllRequiredPages(files, keeps){
Object.keys(files).forEach(function (fileAbsPath) {
let fileContent = fs.readFileSync(fileAbsPath).toString();
let result = fileContent.replace(regexPattern, function(str, prefix, requiredPage, postfix){
if(requiredPage.endsWith(".js")) {
requiredPage = requiredPage.slice(0, requiredPage.length-3);
}
let fullPath = fileSystem.getAbsolutePath(fileAbsPath, requiredPage);
// console.fileContentlog(a, b, c, d);
if(files[fullPath]) {
// console.log(`${requiredPage} ---> ${fullPath}`);
let uuid = prefix + files[fullPath] + postfix;
if(requiredPage.includes("pdpDummys")) {
console.log("\n" + fileAbsPath);
console.log(`${requiredPage} ---> ${uuid}`);
}
return prefix + files[fullPath] + postfix;
}
return str;
});
if(!keeps[fileAbsPath.replace(".js", "")]) {
// console.log("Overrides: " + fileAbsPath);
fs.writeFile(files[fileAbsPath] + ".js", result);
} else {
fs.writeFile(fileAbsPath, result);
// console.log("Keeps: " + fileAbsPath);
}
});
}