Repository: webpack/webpack
Pull request #6482 (2018-02-13 17:15:07)

Improvement: add schema for profiling plugin options, remove todo item
<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. -->

**What kind of change does this PR introduce?**
* Adding schema for `ProfilingPlugin`
* Removed unused schema (belongs to deprecated plugin)
* Renamed parameter for readability
* Added documentation section

<!-- E.g. a bugfix, feature, refactoring, build related change, etc… -->

**Did you add tests for your changes?**
No
<!-- Note that we won't merge your changes if you don't add tests -->

**If relevant, link to documentation update:**
webpack/webpack.js.org#1830
<!-- Link PR from webpack/webpack.js.org here, or N/A -->

**Summary**
Was working on another change when bumped at this `TODO` item
<!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? -->
<!-- Try to link to an open issue for more information. -->

**Does this PR introduce a breaking change?**
Yes, option was renamed
<!-- If this PR introduces a breaking change, please describe the impact and a migration path for existing applications. -->

**Other information**



Patch:
diff --git a/lib/debug/ProfilingPlugin.js b/lib/debug/ProfilingPlugin.js
index 3b3aeffeb..a563e1a98 100644
--- a/lib/debug/ProfilingPlugin.js
+++ b/lib/debug/ProfilingPlugin.js
@@ -1,5 +1,7 @@
 const fs = require("fs");
 const Trace = require("chrome-trace-event").Tracer;
+const validateOptions = require("schema-utils");
+const schema = require("../../schemas/plugins/debug/ProfilingPlugin.json");
 let inspector = undefined;
 
 try {
@@ -8,17 +10,6 @@ try {
 	console.log("Unable to CPU profile in < node 8.0");
 }
 
-// TODO: Add this to webpack.js.org docs for this plugin, and for profiling build times
-/**
- * How this plugin works: (placeholder until in docs)
- *
- * In chrome, open the `Profile Tab`, when you run webpack,
- * this plugin will output an events.json file that you
- * can drag and drop into the profiler. It will then display timeline stats and calls per plugin!
- *
- * 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
- */
-
 class Profiler {
 	constructor(inspector) {
 		this.session = undefined;
@@ -81,10 +72,10 @@ class Profiler {
 }
 
 /**
- * @param {string} outPath The location where to write the log.
+ * @param {string} outputPath The location where to write the log.
  * @returns {{trace: ?,	counter: number, profiler: Profiler}} The trace object
  */
-function createTrace(outPath) {
+function createTrace(outputPath) {
 	const trace = new Trace({
 		noStream: true
 	});
@@ -92,7 +83,7 @@ function createTrace(outPath) {
 
 	let counter = 0;
 
-	trace.pipe(fs.createWriteStream(outPath));
+	trace.pipe(fs.createWriteStream(outputPath));
 	// These are critical events that need to be inserted so that tools like
 	// chrome dev tools can load the profile.
 	trace.instantEvent({
@@ -133,14 +124,14 @@ function createTrace(outPath) {
 const pluginName = "ProfilingPlugin";
 
 class ProfilingPlugin {
-	// TODO: Add plugin schema validation here since there are options.
 	constructor(opts) {
+		validateOptions(schema, opts || {}, "Profiling plugin");
 		opts = opts || {};
-		this.outPath = opts.outPath || "events.json";
+		this.outputPath = opts.outputPath || "events.json";
 	}
 
 	apply(compiler) {
-		const tracer = createTrace(this.outPath);
+		const tracer = createTrace(this.outputPath);
 		tracer.profiler.startProfiling();
 
 		// Compiler Hooks
diff --git a/schemas/plugins/debug/ProfilingPlugin.json b/schemas/plugins/debug/ProfilingPlugin.json
new file mode 100644
index 000000000..20bbdc9df
--- /dev/null
+++ b/schemas/plugins/debug/ProfilingPlugin.json
@@ -0,0 +1,13 @@
+{
+  "id": "ProfilingPlugin",
+  "type": "object",
+  "additionalProperties": false,
+  "properties": {
+    "outputPath": {
+      "description": "Path to the output file e.g. `profiling/events.json`. Defaults to `events.json`.",
+      "type": "string",
+      "absolutePath": false,
+      "minLength": 4
+    }
+  }
+}
diff --git a/schemas/plugins/optimize/CommonsChunkPlugin.json b/schemas/plugins/optimize/CommonsChunkPlugin.json
deleted file mode 100644
index 859075590..000000000
--- a/schemas/plugins/optimize/CommonsChunkPlugin.json
+++ /dev/null
@@ -1,88 +0,0 @@
-{
-  "id": "CommonsChunkPlugin",
-  "oneOf": [
-    {
-      "description": "The chunk name of the commons chunk",
-      "minLength": 1,
-      "type": "string"
-    },
-    {
-      "description": "The chunk names of the commons chunk",
-      "type": "array",
-      "items": {
-        "description": "Chunk name",
-        "minLength": 1,
-        "type": "string"
-      }
-    },
-    {
-      "type": "object",
-      "additionalProperties": false,
-      "properties": {
-        "name": {
-          "description": "The chunk name of the commons chunk",
-          "minLength": 1,
-          "type": "string"
-        },
-        "names": {
-          "description": "The chunk names of the commons chunk",
-          "type": "array",
-          "items": {
-            "description": "Chunk name",
-            "minLength": 1,
-            "type": "string"
-          }
-        },
-        "filename": {
-          "description": "The filename template for the commons chunk",
-          "minLength": 1,
-          "type": "string"
-        },
-        "minChunks": {
-          "description": "The minimum number of chunks which need to contain a module before it's moved into the commons chunk",
-          "oneOf": [
-            {
-              "type": "number"
-            },
-            {
-              "instanceof": "Function"
-            }
-          ]
-        },
-        "chunks": {
-          "description": "Select the source chunks by chunk names",
-          "type": "array",
-          "items": {
-            "description": "Chunk name",
-            "minLength": 1,
-            "type": "string"
-          }
-        },
-        "children": {
-          "description": "If `true` all children of the commons chunk are selected",
-          "type": "boolean"
-        },
-        "deepChildren": {
-          "description": "If `true` all descendants of the commons chunk are selected",
-          "type": "boolean"
-        },
-        "async": {
-          "description": "If `true` a new async commons chunk is created as child of `options.name` and sibling of `options.chunks`. Instead of using `option.filename`, it is possible to change the name of the output file by providing the desired string here instead of `true`",
-          "oneOf": [
-            {
-              "type": "boolean"
-            },
-            {
-              "minLength": 1,
-              "type": "string"
-            }
-          ]
-        },
-        "minSize": {
-          "description": "Minimum size of all common module before a commons chunk is created",
-          "type": "number"
-        }
-      }
-    }
-  ]
-}


Test patch:
diff --git a/test/ProfilingPlugin.unittest.js b/test/ProfilingPlugin.unittest.js
index 9d7746957..9698f185e 100644
--- a/test/ProfilingPlugin.unittest.js
+++ b/test/ProfilingPlugin.unittest.js
@@ -6,14 +6,14 @@ const ProfilingPlugin = require("../lib/debug/ProfilingPlugin");
 describe("Profiling Plugin", () => {
 	it("should persist the passed outpath", () => {
 		const plugin = new ProfilingPlugin({
-			outPath: "invest_in_doge_coin"
+			outputPath: "invest_in_doge_coin"
 		});
-		plugin.outPath.should.equal("invest_in_doge_coin");
+		plugin.outputPath.should.equal("invest_in_doge_coin");
 	});
 
 	it("should handle no options", () => {
 		const plugin = new ProfilingPlugin();
-		plugin.outPath.should.equal("events.json");
+		plugin.outputPath.should.equal("events.json");
 	});
 
 	it("should handle when unable to require the inspector", (done) => {
diff --git a/test/configCases/plugins/profiling-plugin/webpack.config.js b/test/configCases/plugins/profiling-plugin/webpack.config.js
index c27f11352..58933fe41 100644
--- a/test/configCases/plugins/profiling-plugin/webpack.config.js
+++ b/test/configCases/plugins/profiling-plugin/webpack.config.js
@@ -6,7 +6,7 @@ var os = require("os");
 module.exports = {
 	plugins: [
 		new webpack.debug.ProfilingPlugin({
-			outPath: path.join(os.tmpdir(), "events.json")
+			outputPath: path.join(os.tmpdir(), "events.json")
 		})
 	],
 	node: {