Skip to content

Commit 4da1ceb

Browse files
authored
Merge pull request webpack#6482 from EugeneHlushko/profiling-plugin-schema
Improvement: add schema for profiling plugin options, remove todo item
2 parents 33c907e + abc3238 commit 4da1ceb

5 files changed

Lines changed: 25 additions & 109 deletions

File tree

‎lib/debug/ProfilingPlugin.js‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const fs = require("fs");
22
const Trace = require("chrome-trace-event").Tracer;
3+
const validateOptions = require("schema-utils");
4+
const schema = require("../../schemas/plugins/debug/ProfilingPlugin.json");
35
let inspector = undefined;
46

57
try {
@@ -8,17 +10,6 @@ try {
810
console.log("Unable to CPU profile in < node 8.0");
911
}
1012

11-
// TODO: Add this to webpack.js.org docs for this plugin, and for profiling build times
12-
/**
13-
* How this plugin works: (placeholder until in docs)
14-
*
15-
* In chrome, open the `Profile Tab`, when you run webpack,
16-
* this plugin will output an events.json file that you
17-
* can drag and drop into the profiler. It will then display timeline stats and calls per plugin!
18-
*
19-
* Example: https://chromedevtools.github.io/timeline-viewer/?url=https%3A%2F%2Fgist.githubusercontent.com%2FTheLarkInn%2Fb94b728fa5e22f62c312e110a9944768%2Fraw%2Fcb672fb3f661a17576803e41db6030382b1a0fc9%2Fevents.json&loadTimelineFromURL=drive://163GY-H0wvF9rSrlwjJcrdTL-YLnppp55
20-
*/
21-
2213
class Profiler {
2314
constructor(inspector) {
2415
this.session = undefined;
@@ -81,18 +72,18 @@ class Profiler {
8172
}
8273

8374
/**
84-
* @param {string} outPath The location where to write the log.
75+
* @param {string} outputPath The location where to write the log.
8576
* @returns {{trace: ?, counter: number, profiler: Profiler}} The trace object
8677
*/
87-
function createTrace(outPath) {
78+
function createTrace(outputPath) {
8879
const trace = new Trace({
8980
noStream: true
9081
});
9182
const profiler = new Profiler(inspector);
9283

9384
let counter = 0;
9485

95-
trace.pipe(fs.createWriteStream(outPath));
86+
trace.pipe(fs.createWriteStream(outputPath));
9687
// These are critical events that need to be inserted so that tools like
9788
// chrome dev tools can load the profile.
9889
trace.instantEvent({
@@ -133,14 +124,14 @@ function createTrace(outPath) {
133124
const pluginName = "ProfilingPlugin";
134125

135126
class ProfilingPlugin {
136-
// TODO: Add plugin schema validation here since there are options.
137127
constructor(opts) {
128+
validateOptions(schema, opts || {}, "Profiling plugin");
138129
opts = opts || {};
139-
this.outPath = opts.outPath || "events.json";
130+
this.outputPath = opts.outputPath || "events.json";
140131
}
141132

142133
apply(compiler) {
143-
const tracer = createTrace(this.outPath);
134+
const tracer = createTrace(this.outputPath);
144135
tracer.profiler.startProfiling();
145136

146137
// Compiler Hooks
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"id": "ProfilingPlugin",
3+
"type": "object",
4+
"additionalProperties": false,
5+
"properties": {
6+
"outputPath": {
7+
"description": "Path to the output file e.g. `profiling/events.json`. Defaults to `events.json`.",
8+
"type": "string",
9+
"absolutePath": false,
10+
"minLength": 4
11+
}
12+
}
13+
}

‎schemas/plugins/optimize/CommonsChunkPlugin.json‎

Lines changed: 0 additions & 88 deletions
This file was deleted.

‎test/ProfilingPlugin.unittest.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ const ProfilingPlugin = require("../lib/debug/ProfilingPlugin");
66
describe("Profiling Plugin", () => {
77
it("should persist the passed outpath", () => {
88
const plugin = new ProfilingPlugin({
9-
outPath: "invest_in_doge_coin"
9+
outputPath: "invest_in_doge_coin"
1010
});
11-
plugin.outPath.should.equal("invest_in_doge_coin");
11+
plugin.outputPath.should.equal("invest_in_doge_coin");
1212
});
1313

1414
it("should handle no options", () => {
1515
const plugin = new ProfilingPlugin();
16-
plugin.outPath.should.equal("events.json");
16+
plugin.outputPath.should.equal("events.json");
1717
});
1818

1919
it("should handle when unable to require the inspector", (done) => {

‎test/configCases/plugins/profiling-plugin/webpack.config.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var os = require("os");
66
module.exports = {
77
plugins: [
88
new webpack.debug.ProfilingPlugin({
9-
outPath: path.join(os.tmpdir(), "events.json")
9+
outputPath: path.join(os.tmpdir(), "events.json")
1010
})
1111
],
1212
node: {

0 commit comments

Comments
 (0)