|
1 | 1 | const path = require("path"); |
2 | 2 | const fs = require("fs"); |
3 | 3 | const chalk = require("chalk"); |
4 | | -const { fg } = require("./colours"); |
5 | | -const exec = require("child_process").exec; |
6 | 4 | const { WrappedPlugin, clear } = require("./WrappedPlugin"); |
7 | 5 | const { |
8 | 6 | getModuleName, |
@@ -33,6 +31,7 @@ module.exports = class SpeedMeasurePlugin { |
33 | 31 | this.apply = this.apply.bind(this); |
34 | 32 | this.provideLoaderTiming = this.provideLoaderTiming.bind(this); |
35 | 33 | this.getLoadersBuildComparison = this.getLoadersBuildComparison.bind(this); |
| 34 | + this.isValidJson = this.isValidJson.bind(this); |
36 | 35 | } |
37 | 36 |
|
38 | 37 | wrap(config) { |
@@ -71,15 +70,24 @@ module.exports = class SpeedMeasurePlugin { |
71 | 70 | return config; |
72 | 71 | } |
73 | 72 |
|
| 73 | + isValidJson(strJson) { |
| 74 | + try { |
| 75 | + return JSON.parse(strJson) && !!strJson; |
| 76 | + } catch (e) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + } |
| 80 | + |
74 | 81 | getLoadersBuildComparison() { |
75 | 82 | let objBuildData = { loaderInfo: [] }; |
76 | 83 | let loaderFile = this.options.compareLoadersBuild.filePath || ""; |
77 | 84 | const outputObj = getLoadersOutput(this.timeEventData.loaders); |
78 | 85 |
|
79 | 86 | if (outputObj && loaderFile && fs.existsSync(loaderFile)) { |
80 | 87 | let buildDetails = fs.readFileSync(loaderFile); |
81 | | - buildDetails = |
82 | | - buildDetails.toString() !== "" ? JSON.parse(buildDetails) : []; |
| 88 | + buildDetails = this.isValidJson(buildDetails.toString()) |
| 89 | + ? JSON.parse(buildDetails) |
| 90 | + : []; |
83 | 91 | const buildCount = buildDetails.length; |
84 | 92 | const buildNo = |
85 | 93 | buildCount > 0 ? buildDetails[buildCount - 1]["buildNo"] + 1 : 1; |
|
0 commit comments