Skip to content

Commit 8f8c057

Browse files
Minor fix-ups and tidy-ups
1 parent 07caba0 commit 8f8c057

File tree

2 files changed

+89
-89
lines changed

2 files changed

+89
-89
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,15 @@ const smp = new SpeedMeasurePlugin({
140140
Type: `Object`<br>
141141
Default: `{}`
142142

143-
This option gives you per loader module count, module time to execute, time comparison
144-
with previous build of each loader. It takes input as `filePath` which is
145-
filename where all build details will be written.
146-
`Note :- i) Node version > 0.10 ii) filePath option is mandatory. iii) The module count will only be visible when the outputFormat is 'humanVerbose'.`,
147-
e.g.
143+
This option gives you a comparison over time of the module count and time spent, per loader. This option provides more data when `outputFormat: "humanVerbose"`.
144+
145+
Given a required `filePath` to store the build information, this option allows you to compare differences to your codebase over time. E.g.
148146

149147
```javascript
150148
const smp = new SpeedMeasurePlugin({
151-
compareLoadersBuild: { filePath: "./buildInfo.json" },
149+
compareLoadersBuild: {
150+
filePath: "./buildInfo.json",
151+
},
152152
});
153153
```
154154

index.js

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ module.exports = class SpeedMeasurePlugin {
3030
this.addTimeEvent = this.addTimeEvent.bind(this);
3131
this.apply = this.apply.bind(this);
3232
this.provideLoaderTiming = this.provideLoaderTiming.bind(this);
33-
this.getLoadersBuildComparison = this.getLoadersBuildComparison.bind(this);
34-
this.isValidJson = this.isValidJson.bind(this);
33+
this.generateLoadersBuildComparison = this.generateLoadersBuildComparison.bind(
34+
this
35+
);
3536
}
3637

3738
wrap(config) {
@@ -70,94 +71,91 @@ module.exports = class SpeedMeasurePlugin {
7071
return config;
7172
}
7273

73-
isValidJson(strJson) {
74-
try {
75-
return JSON.parse(strJson) && !!strJson;
76-
} catch (e) {
77-
return false;
74+
generateLoadersBuildComparison() {
75+
const objBuildData = { loaderInfo: [] };
76+
const loaderFile = this.options.compareLoadersBuild.filePath;
77+
const outputObj = getLoadersOutput(this.timeEventData.loaders);
78+
79+
if (!loaderFile) {
80+
throw new Error(
81+
"`options.compareLoadersBuild.filePath` is a required field"
82+
);
7883
}
79-
}
8084

81-
getLoadersBuildComparison() {
82-
let objBuildData = { loaderInfo: [] };
83-
let loaderFile = this.options.compareLoadersBuild.filePath || "";
84-
const outputObj = getLoadersOutput(this.timeEventData.loaders);
85+
if (!outputObj) {
86+
throw new Error("No output found!");
87+
}
8588

86-
if (outputObj && loaderFile && fs.existsSync(loaderFile)) {
87-
let buildDetails = fs.readFileSync(loaderFile);
88-
buildDetails = this.isValidJson(buildDetails.toString())
89-
? JSON.parse(buildDetails)
90-
: [];
91-
const buildCount = buildDetails.length;
92-
const buildNo =
93-
buildCount > 0 ? buildDetails[buildCount - 1]["buildNo"] + 1 : 1;
94-
95-
/*********************** Code to create object format of current loader and write in the file. ************************/
96-
outputObj.build.forEach((loaderObj, loaderIndex) => {
97-
const loaderInfo = {};
98-
loaderInfo["Name"] = loaderObj.loaders.join(",") || "";
99-
loaderInfo["Time"] = loaderObj.activeTime || "";
100-
loaderInfo["Count"] =
101-
this.options.outputFormat === "humanVerbose"
102-
? loaderObj.averages.dataPoints
103-
: "";
104-
loaderInfo[`Comparison`] = "";
105-
106-
// Getting the comparison from the previous build by default only in case if build data is more then one.
107-
if (buildCount > 0) {
108-
const prevBuildIndex = buildCount - 1;
109-
for (
110-
var y = 0;
111-
y < buildDetails[prevBuildIndex]["loaderInfo"].length;
112-
y++
89+
const buildDetailsFile = fs.existsSync(loaderFile)
90+
? fs.readFileSync(loaderFile)
91+
: "[]";
92+
const buildDetails = JSON.parse(buildDetailsFile.toString());
93+
const buildCount = buildDetails.length;
94+
const buildNo =
95+
buildCount > 0 ? buildDetails[buildCount - 1]["buildNo"] + 1 : 1;
96+
97+
// create object format of current loader and write in the file
98+
outputObj.build.forEach((loaderObj) => {
99+
const loaderInfo = {};
100+
loaderInfo["Name"] = loaderObj.loaders.join(",") || "";
101+
loaderInfo["Time"] = loaderObj.activeTime || "";
102+
loaderInfo["Count"] =
103+
this.options.outputFormat === "humanVerbose"
104+
? loaderObj.averages.dataPoints
105+
: "";
106+
loaderInfo[`Comparison`] = "";
107+
108+
// Getting the comparison from the previous build by default only
109+
// in case if build data is more then one
110+
if (buildCount > 0) {
111+
const prevBuildIndex = buildCount - 1;
112+
for (
113+
var y = 0;
114+
y < buildDetails[prevBuildIndex]["loaderInfo"].length;
115+
y++
116+
) {
117+
const prevloaderDetails =
118+
buildDetails[prevBuildIndex]["loaderInfo"][y];
119+
if (
120+
loaderInfo["Name"] == prevloaderDetails["Name"] &&
121+
prevloaderDetails["Time"]
113122
) {
114-
const prevloaderDetails =
115-
buildDetails[prevBuildIndex]["loaderInfo"][y];
116-
if (
117-
loaderInfo["Name"] == prevloaderDetails["Name"] &&
118-
prevloaderDetails["Time"]
119-
) {
120-
let previousBuildTime =
121-
buildDetails[prevBuildIndex]["loaderInfo"][y]["Time"];
122-
loaderInfo[`Comparison`] = `buildDiff--> ${Math.abs(
123-
previousBuildTime - loaderObj.activeTime
124-
)}|${previousBuildTime > loaderObj.activeTime ? "Good" : "Bad"}`;
125-
}
123+
const previousBuildTime =
124+
buildDetails[prevBuildIndex]["loaderInfo"][y]["Time"];
125+
const savedTime = previousBuildTime > loaderObj.activeTime;
126+
127+
loaderInfo[`Comparison`] = `${savedTime ? "-" : "+"}${Math.abs(
128+
loaderObj.activeTime - previousBuildTime
129+
)}ms | ${savedTime ? "(slower)" : "(faster)"}`;
126130
}
127131
}
132+
}
128133

129-
objBuildData["loaderInfo"].push(loaderInfo);
130-
});
134+
objBuildData["loaderInfo"].push(loaderInfo);
135+
});
131136

132-
buildDetails.push({ buildNo, loaderInfo: objBuildData["loaderInfo"] });
133-
fs.writeFileSync(loaderFile, JSON.stringify(buildDetails));
134-
/****************************************************************************************/
135-
136-
let outputTable = [];
137-
let objCurrentBuild = {};
138-
139-
for (let i = 0; i < buildDetails.length; i++) {
140-
outputTable = [];
141-
console.log("--------------------------------------------");
142-
console.log("Build No ", buildDetails[i]["buildNo"]);
143-
console.log("--------------------------------------------");
144-
145-
if (buildDetails[i]["loaderInfo"]) {
146-
buildDetails[i]["loaderInfo"].forEach(
147-
(buildIndex, buildInfoIndex) => {
148-
const buildInfo = buildDetails[i]["loaderInfo"][buildInfoIndex];
149-
objCurrentBuild = {};
150-
objCurrentBuild["Name"] = buildInfo["Name"] || "";
151-
objCurrentBuild["Time"] = buildInfo["Time"] || "";
152-
if (this.options.outputFormat === "humanVerbose")
153-
objCurrentBuild["Count"] = buildInfo["Count"] || 0;
154-
objCurrentBuild["Comparison"] = buildInfo["Comparison"] || "";
155-
outputTable.push(objCurrentBuild);
156-
}
157-
);
158-
}
159-
console.table(outputTable);
137+
buildDetails.push({ buildNo, loaderInfo: objBuildData["loaderInfo"] });
138+
139+
fs.writeFileSync(loaderFile, JSON.stringify(buildDetails));
140+
141+
for (let i = 0; i < buildDetails.length; i++) {
142+
const outputTable = [];
143+
console.log("--------------------------------------------");
144+
console.log("Build No ", buildDetails[i]["buildNo"]);
145+
console.log("--------------------------------------------");
146+
147+
if (buildDetails[i]["loaderInfo"]) {
148+
buildDetails[i]["loaderInfo"].forEach((buildInfo) => {
149+
const objCurrentBuild = {};
150+
objCurrentBuild["Name"] = buildInfo["Name"] || "";
151+
objCurrentBuild["Time (ms)"] = buildInfo["Time"] || "";
152+
if (this.options.outputFormat === "humanVerbose")
153+
objCurrentBuild["Count"] = buildInfo["Count"] || 0;
154+
objCurrentBuild["Comparison"] = buildInfo["Comparison"] || "";
155+
outputTable.push(objCurrentBuild);
156+
});
160157
}
158+
console.table(outputTable);
161159
}
162160
}
163161

@@ -247,8 +245,10 @@ module.exports = class SpeedMeasurePlugin {
247245
const outputFunc = this.options.outputTarget || console.log;
248246
outputFunc(output);
249247
}
250-
// Build Comparison functionality.
251-
if (this.options.compareLoadersBuild) this.getLoadersBuildComparison();
248+
249+
if (this.options.compareLoadersBuild)
250+
this.generateLoadersBuildComparison();
251+
252252
this.timeEventData = {};
253253
});
254254

0 commit comments

Comments
 (0)