Skip to content

Commit adfcc4c

Browse files
Merge branch 'release/1.5.0' of github.com:stephencookdev/speed-measure-webpack-plugin into release/1.5.0
2 parents 69aaff2 + d2db216 commit adfcc4c

File tree

2 files changed

+112
-1
lines changed

2 files changed

+112
-1
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,23 @@ const smp = new SpeedMeasurePlugin({
135135
});
136136
```
137137

138+
### `options.compareLoadersBuild`
139+
140+
Type: `Object`<br>
141+
Default: `{}`
142+
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.
148+
149+
```javascript
150+
const smp = new SpeedMeasurePlugin({
151+
compareLoadersBuild: { filePath: "./buildInfo.json" },
152+
});
153+
```
154+
138155
### `options.granularLoaderData` _(experimental)_
139156

140157
Type: `Boolean`<br>

index.js

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ 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);
3335
}
3436

3537
wrap(config) {
@@ -68,6 +70,97 @@ module.exports = class SpeedMeasurePlugin {
6870
return config;
6971
}
7072

73+
isValidJson(strJson) {
74+
try {
75+
return JSON.parse(strJson) && !!strJson;
76+
} catch (e) {
77+
return false;
78+
}
79+
}
80+
81+
getLoadersBuildComparison() {
82+
let objBuildData = { loaderInfo: [] };
83+
let loaderFile = this.options.compareLoadersBuild.filePath || "";
84+
const outputObj = getLoadersOutput(this.timeEventData.loaders);
85+
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++
113+
) {
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+
}
126+
}
127+
}
128+
129+
objBuildData["loaderInfo"].push(loaderInfo);
130+
});
131+
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);
160+
}
161+
}
162+
}
163+
71164
getOutput() {
72165
const outputObj = {};
73166
if (this.timeEventData.misc)
@@ -154,7 +247,8 @@ module.exports = class SpeedMeasurePlugin {
154247
const outputFunc = this.options.outputTarget || console.log;
155248
outputFunc(output);
156249
}
157-
250+
// Build Comparison functionality.
251+
if (this.options.compareLoadersBuild) this.getLoadersBuildComparison();
158252
this.timeEventData = {};
159253
});
160254

0 commit comments

Comments
 (0)