|
| 1 | +const { WrappedPlugin } = require("./WrappedPlugin"); |
| 2 | +const { getModuleName, getLoaderNames } = require("./utils"); |
| 3 | +const { getHumanOutput, getMiscOutput, getPluginsOutput, getLoadersOutput } = require("./output"); |
| 4 | + |
| 5 | +module.exports = class SpeedMeasurePlugin { |
| 6 | + constructor(options) { |
| 7 | + this.options = options || {}; |
| 8 | + |
| 9 | + this.timeEventData = {}; |
| 10 | + |
| 11 | + this.getOutput = this.getOutput.bind(this); |
| 12 | + this.addTimeEvent = this.addTimeEvent.bind(this); |
| 13 | + this.apply = this.apply.bind(this); |
| 14 | + } |
| 15 | + |
| 16 | + static wrapPlugins(plugins, options) { |
| 17 | + const smp = new SpeedMeasurePlugin(options); |
| 18 | + |
| 19 | + if(Array.isArray(plugins)) { |
| 20 | + let i = 1; |
| 21 | + plugins = plugins.reduce((acc, p) => ({ |
| 22 | + ...acc, |
| 23 | + ["plugin " + (i++)]: p, |
| 24 | + })); |
| 25 | + } |
| 26 | + plugins = plugins || {}; |
| 27 | + |
| 28 | + const wrappedPlugins = Object |
| 29 | + .keys(plugins) |
| 30 | + .map(pluginName => new WrappedPlugin(plugins[pluginName], pluginName, smp)); |
| 31 | + |
| 32 | + return wrappedPlugins.concat(smp); |
| 33 | + } |
| 34 | + |
| 35 | + getOutput() { |
| 36 | + const outputObj = {}; |
| 37 | + if(this.timeEventData.misc) |
| 38 | + outputObj.misc = getMiscOutput(this.timeEventData.misc); |
| 39 | + if(this.timeEventData.plugins) |
| 40 | + outputObj.plugins = getPluginsOutput(this.timeEventData.plugins); |
| 41 | + if(this.timeEventData.loaders) |
| 42 | + outputObj.loaders = getLoadersOutput(this.timeEventData.loaders); |
| 43 | + |
| 44 | + return this.options.outputFormat === "human" |
| 45 | + ? getHumanOutput(outputObj) |
| 46 | + : JSON.stringify(outputObj, null, 2); |
| 47 | + } |
| 48 | + |
| 49 | + addTimeEvent(category, event, eventType, data = {}) { |
| 50 | + const tED = this.timeEventData; |
| 51 | + if(!tED[category]) tED[category] = {}; |
| 52 | + if(!tED[category][event]) tED[category][event] = []; |
| 53 | + const eventList = tED[category][event]; |
| 54 | + const curTime = new Date().getTime(); |
| 55 | + |
| 56 | + if(eventType === "start") { |
| 57 | + eventList.push({ |
| 58 | + start: curTime, |
| 59 | + ...data, |
| 60 | + }); |
| 61 | + } else if(eventType === "end") { |
| 62 | + const matchingEvent = eventList.find(e => !e.end && (e.id === data.id || e.name === data.name)); |
| 63 | + const eventToModify = matchingEvent || eventList.find(e => !e.end); |
| 64 | + if(!eventToModify) { |
| 65 | + console.log("Could not find a matching event to end", category, event, data); |
| 66 | + throw new Error("No matching event!"); |
| 67 | + } |
| 68 | + |
| 69 | + eventToModify.end = curTime; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + apply(compiler) { |
| 74 | + compiler.plugin("compile", () => { |
| 75 | + this.addTimeEvent("misc", "compile", "start", { watch: false }); |
| 76 | + }); |
| 77 | + compiler.plugin("done", () => { |
| 78 | + this.addTimeEvent("misc", "compile", "end"); |
| 79 | + |
| 80 | + const output = this.getOutput(); |
| 81 | + if(this.options.outputTarget) { |
| 82 | + const writeMethod = fs.existsSync(this.options.outputTarget) ? fs.appendFile : fs.writeFile; |
| 83 | + writeMethod(this.options.outputTarget, output + "\n", err => { |
| 84 | + if(err) throw err; |
| 85 | + console.log("Outputted timing info to " + this.options.outputTarget); |
| 86 | + }); |
| 87 | + } else { |
| 88 | + console.log(output); |
| 89 | + } |
| 90 | + |
| 91 | + this.timeEventData = {}; |
| 92 | + }); |
| 93 | + |
| 94 | + compiler.plugin("compilation", compilation => { |
| 95 | + compilation.plugin("build-module", module => { |
| 96 | + const name = getModuleName(module); |
| 97 | + if(name) { |
| 98 | + this.addTimeEvent("loaders", "build", "start", { |
| 99 | + name, |
| 100 | + loaders: getLoaderNames(module.loaders), |
| 101 | + }); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + compilation.plugin("succeed-module", module => { |
| 106 | + const name = getModuleName(module); |
| 107 | + if(name) { |
| 108 | + this.addTimeEvent("loaders", "build", "end", { name }); |
| 109 | + } |
| 110 | + }); |
| 111 | + }); |
| 112 | + } |
| 113 | +} |
0 commit comments