-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsspaPostBuild.js
71 lines (63 loc) · 2.11 KB
/
sspaPostBuild.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
const sspaPostBuildScript = `
const util = require('util');
const fs = require('fs-extra');
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const copyFile = util.promisify(fs.copyFile);
const exec = util.promisify(require('child_process').exec);
const crypto = require('crypto');
const opPath = process.cwd() + "/dist/ng-bundle";
let styleHashMap = {};
const copyCssFiles = (filename, hash) => {
try {
const updatedFilename = filename + "." + hash + ".css";
copyFile(opPath + "/" + filename + ".css", opPath + "/" + updatedFilename);
} catch (e) {
console.error("File[" + filename + "] not found | ", e);
}
};
const generateHash = async (filepath) => {
const cssContent = await readFile(filepath);
let hash = crypto.createHash('md5');
hash.update(cssContent);
return hash.digest('hex');
};
const addStyleHash = () => {
const styleFile = fs.readdirSync(opPath).filter(file => file.startsWith("styles."))[0];
let styleFileHash = styleFile.split(".")[1];
styleHashMap["{styles-hash}"] = styleFileHash;
}
const replaceCssHash = async () => {
const mainFile = fs.readdirSync(opPath).filter(file => file.startsWith("main"))[0];
const mainContent = await readFile(opPath + "/" + mainFile);
let content = mainContent.toString();
for (let placeHolder in styleHashMap) {
let hashRegex = new RegExp(placeHolder,"ig");
content = content.replace(hashRegex, styleHashMap[placeHolder]);
}
fs.writeFileSync(opPath + "/" + mainFile, content);
};
(async () => {
try {
const cssFileNames = ['wm-theme-styles', 'wm-app-styles', 'wm-styles'];
for(var i = 0; i < cssFileNames.length; i++ ) {
let file = opPath + "/" + cssFileNames[i] + ".css";
if(fs.existsSync(file)) {
const hash = await generateHash(file);
copyCssFiles(cssFileNames[i], hash);
styleHashMap["{" + cssFileNames[i] + "-hash}"] = hash;
}
};
addStyleHash();
replaceCssHash();
} catch (e) {
console.error("Error in Post ng build Script | ", e);
}
})()
`;
const getSSPAPostBuildScriptTemplate = () => {
return sspaPostBuildScript;
};
module.exports = {
getSSPAPostBuildScriptTemplate
};