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