Skip to content

Commit 6658b1b

Browse files
Suraj PatelSuraj Patel
authored andcommitted
Integrating build comparison for all loaders use in bundling.
1 parent d2c3d61 commit 6658b1b

File tree

1 file changed

+104
-1
lines changed

1 file changed

+104
-1
lines changed

index.js

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const path = require("path");
22
const fs = require("fs");
33
const chalk = require("chalk");
4+
const { fg } = require("./colours");
5+
const exec = require("child_process").exec;
46
const { WrappedPlugin, clear } = require("./WrappedPlugin");
57
const {
68
getModuleName,
@@ -30,6 +32,8 @@ module.exports = class SpeedMeasurePlugin {
3032
this.addTimeEvent = this.addTimeEvent.bind(this);
3133
this.apply = this.apply.bind(this);
3234
this.provideLoaderTiming = this.provideLoaderTiming.bind(this);
35+
this.getLoadersBuildComparison = this.getLoadersBuildComparison.bind(this);
36+
this.commitBuidlComparison = this.commitBuidlComparison.bind(this);
3337
}
3438

3539
wrap(config) {
@@ -68,6 +72,104 @@ module.exports = class SpeedMeasurePlugin {
6872
return config;
6973
}
7074

75+
commitBuidlComparison() {
76+
let loaderFile = this.options.compareLoadersBuild.filePath || "";
77+
let gitCmd = `git add ${loaderFile} && git commit -m 'Updating file with new build info' && git push origin`;
78+
exec(gitCmd, function(err, stdout, stderr) {
79+
if (err) {
80+
console.log(fg("There was error while committing your changes."), err);
81+
return;
82+
}
83+
console.log("--------------------------------------------");
84+
console.log(fg("Succefully Committed Build Info."), stdout);
85+
console.log("--------------------------------------------");
86+
});
87+
}
88+
getLoadersBuildComparison() {
89+
let objBuildData = { loaderInfo: [] };
90+
let loaderFile = this.options.compareLoadersBuild.filePath || "";
91+
const outputObj = getLoadersOutput(this.timeEventData.loaders);
92+
93+
if (outputObj && loaderFile && fs.existsSync(loaderFile)) {
94+
let buildDetails = fs.readFileSync(loaderFile);
95+
buildDetails =
96+
buildDetails.toString() !== "" ? JSON.parse(buildDetails) : [];
97+
const buildCount = buildDetails.length;
98+
const buildNo =
99+
buildCount > 0 ? buildDetails[buildCount - 1]["buildNo"] + 1 : 1;
100+
101+
/*********************** Code to create object format of current loader and write in the file. ************************/
102+
outputObj.build.forEach((loaderObj, loaderIndex) => {
103+
const loaderInfo = {};
104+
loaderInfo["Name"] = loaderObj.loaders.join(",") || "";
105+
loaderInfo["Time"] = loaderObj.activeTime || "";
106+
loaderInfo["Count"] =
107+
this.options.outputFormat === "humanVerbose"
108+
? loaderObj.averages.dataPoints
109+
: "";
110+
loaderInfo[`Comparison`] = "";
111+
112+
// Getting the comparison from the previous build by default only in case if build data is more then one.
113+
if (buildCount > 0) {
114+
const prevBuildIndex = buildCount - 1;
115+
for (
116+
var y = 0;
117+
y < buildDetails[prevBuildIndex]["loaderInfo"].length;
118+
y++
119+
) {
120+
const prevloaderDetails =
121+
buildDetails[prevBuildIndex]["loaderInfo"][y];
122+
if (
123+
loaderInfo["Name"] == prevloaderDetails["Name"] &&
124+
prevloaderDetails["Time"]
125+
) {
126+
let previousBuildTime =
127+
buildDetails[prevBuildIndex]["loaderInfo"][y]["Time"];
128+
loaderInfo[`Comparison`] = `buildDiff--> ${Math.abs(
129+
previousBuildTime - loaderObj.activeTime
130+
)}|${previousBuildTime > loaderObj.activeTime ? "Good" : "Bad"}`;
131+
}
132+
}
133+
}
134+
135+
objBuildData["loaderInfo"].push(loaderInfo);
136+
});
137+
138+
buildDetails.push({ buildNo, loaderInfo: objBuildData["loaderInfo"] });
139+
fs.writeFileSync(loaderFile, JSON.stringify(buildDetails));
140+
/****************************************************************************************/
141+
142+
let outputTable = [];
143+
let objCurrentBuild = {};
144+
145+
for (let i = 0; i < buildDetails.length; i++) {
146+
outputTable = [];
147+
console.log("--------------------------------------------");
148+
console.log("Build No ", buildDetails[i]["buildNo"]);
149+
console.log("--------------------------------------------");
150+
151+
if (buildDetails[i]["loaderInfo"]) {
152+
buildDetails[i]["loaderInfo"].forEach(
153+
(buildIndex, buildInfoIndex) => {
154+
const buildInfo = buildDetails[i]["loaderInfo"][buildInfoIndex];
155+
objCurrentBuild = {};
156+
objCurrentBuild["Name"] = buildInfo["Name"] || "";
157+
objCurrentBuild["Time"] = buildInfo["Time"] || "";
158+
if (this.options.outputFormat === "humanVerbose")
159+
objCurrentBuild["Count"] = buildInfo["Count"] || 0;
160+
objCurrentBuild["Comparison"] = buildInfo["Comparison"] || "";
161+
outputTable.push(objCurrentBuild);
162+
}
163+
);
164+
}
165+
console.table(outputTable);
166+
}
167+
168+
// Commiting the build info file to git remote repo.
169+
this.options.compareLoadersBuild.commitBuildInfo &&
170+
this.commitBuidlComparison();
171+
}
172+
}
71173
getOutput() {
72174
const outputObj = {};
73175
if (this.timeEventData.misc)
@@ -150,7 +252,8 @@ module.exports = class SpeedMeasurePlugin {
150252
const outputFunc = this.options.outputTarget || console.log;
151253
outputFunc(output);
152254
}
153-
255+
// Build Comparison functionality.
256+
if (this.options.compareLoadersBuild) this.getLoadersBuildComparison();
154257
this.timeEventData = {};
155258
});
156259

0 commit comments

Comments
 (0)