Skip to content

Commit 54d71d2

Browse files
Add prettier for formatting
1 parent 28266f8 commit 54d71d2

7 files changed

Lines changed: 331 additions & 268 deletions

File tree

.prettierrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"trailingComma": "es5"
3+
}

WrappedPlugin/index.js

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,71 @@
11
let idInc = 0;
22

3-
const genPluginMethod = (orig, pluginName, smp, type) => function(method, func) {
4-
const id = idInc++;
5-
const timeEventName = type + "/" + method;
3+
const genPluginMethod = (orig, pluginName, smp, type) =>
4+
function(method, func) {
5+
const id = idInc++;
6+
const timeEventName = type + "/" + method;
67

7-
const wrappedFunc = (...args) => {
8-
smp.addTimeEvent("plugins", timeEventName, "start", {
9-
id,
10-
name: pluginName,
11-
});
12-
const ret = func.apply(
13-
this,
14-
args.map(a => wrap(a, pluginName, smp))
15-
);
16-
smp.addTimeEvent("plugins", timeEventName, "end", { id });
17-
return ret;
18-
};
8+
const wrappedFunc = (...args) => {
9+
smp.addTimeEvent("plugins", timeEventName, "start", {
10+
id,
11+
name: pluginName,
12+
});
13+
const ret = func.apply(this, args.map(a => wrap(a, pluginName, smp)));
14+
smp.addTimeEvent("plugins", timeEventName, "end", { id });
15+
return ret;
16+
};
1917

20-
return orig.plugin(method, wrappedFunc);
21-
};
18+
return orig.plugin(method, wrappedFunc);
19+
};
2220

2321
const construcNamesToWrap = [
24-
"Compiler",
25-
"Compilation",
26-
"MainTemplate",
27-
"Parser",
28-
"NormalModuleFactory",
29-
"ContextModuleFactory",
22+
"Compiler",
23+
"Compilation",
24+
"MainTemplate",
25+
"Parser",
26+
"NormalModuleFactory",
27+
"ContextModuleFactory",
3028
];
3129

3230
const wrap = (orig, pluginName, smp) => {
33-
const origConstrucName = orig && orig.constructor && orig.constructor.name;
34-
const shouldWrap = construcNamesToWrap.includes(origConstrucName);
35-
if(!shouldWrap) return orig;
31+
const origConstrucName = orig && orig.constructor && orig.constructor.name;
32+
const shouldWrap = construcNamesToWrap.includes(origConstrucName);
33+
if (!shouldWrap) return orig;
3634

37-
const proxy = new Proxy(orig, {
38-
get: (target, property) => {
39-
if(property === "plugin")
40-
return genPluginMethod(orig, pluginName, smp, origConstrucName).bind(proxy);
35+
const proxy = new Proxy(orig, {
36+
get: (target, property) => {
37+
if (property === "plugin")
38+
return genPluginMethod(orig, pluginName, smp, origConstrucName).bind(
39+
proxy
40+
);
4141

42-
if(typeof orig[property] === "function") return orig[property].bind(proxy);
43-
return wrap(orig[property], pluginName, smp);
44-
},
45-
set: (target, property, value) => {
46-
target[property] = value;
47-
},
48-
deleteProperty: (target, property) => {
49-
delete target[property];
50-
},
51-
});
42+
if (typeof orig[property] === "function")
43+
return orig[property].bind(proxy);
44+
return wrap(orig[property], pluginName, smp);
45+
},
46+
set: (target, property, value) => {
47+
target[property] = value;
48+
},
49+
deleteProperty: (target, property) => {
50+
delete target[property];
51+
},
52+
});
5253

53-
return proxy;
54-
}
54+
return proxy;
55+
};
5556

5657
module.exports.WrappedPlugin = class WrappedPlugin {
57-
constructor(plugin, pluginName, smp) {
58-
this._smp_plugin = plugin;
59-
this._smp_pluginName = pluginName;
60-
this._smp = smp;
58+
constructor(plugin, pluginName, smp) {
59+
this._smp_plugin = plugin;
60+
this._smp_pluginName = pluginName;
61+
this._smp = smp;
6162

62-
this.apply = this.apply.bind(this);
63-
}
63+
this.apply = this.apply.bind(this);
64+
}
6465

65-
apply(compiler) {
66-
return this._smp_plugin.apply(wrap(compiler, this._smp_pluginName, this._smp));
67-
}
68-
}
66+
apply(compiler) {
67+
return this._smp_plugin.apply(
68+
wrap(compiler, this._smp_pluginName, this._smp)
69+
);
70+
}
71+
};

index.js

Lines changed: 123 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,128 @@
11
const fs = require("fs");
22
const { WrappedPlugin } = require("./WrappedPlugin");
33
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");
510

611
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

Comments
 (0)