From a01150bef049bdac8b0fa5e10a89af67ecd92db3 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Tue, 3 May 2022 07:11:24 +0530 Subject: [PATCH] fix: improve schema for watch options --- lib/options.json | 67 +++++++++++++++++-- .../validate-options.test.js.snap.webpack5 | 24 +++++-- test/validate-options.test.js | 17 +++++ 3 files changed, 97 insertions(+), 11 deletions(-) diff --git a/lib/options.json b/lib/options.json index 32b1c84ca1..76508a8506 100644 --- a/lib/options.json +++ b/lib/options.json @@ -1002,9 +1002,7 @@ } }, { - "type": "object", - "description": "Options for watch.", - "link": "https://github.com/paulmillr/chokidar#api" + "$ref": "#/definitions/WatchOptionsChokidar" } ], "description": "Watches for files in static content directory.", @@ -1064,10 +1062,7 @@ "description": "Path(s) of globs/directories/files to watch for file changes." }, "options": { - "type": "object", - "description": "Configure advanced options for watching. See the chokidar documentation for the possible options.", - "link": "https://github.com/paulmillr/chokidar#api", - "additionalProperties": true + "$ref": "#/definitions/WatchOptionsChokidar" } }, "additionalProperties": false @@ -1076,6 +1071,64 @@ "type": "string", "minLength": 1 }, + "WatchOptionsChokidar": { + "type": "object", + "additionalProperties": false, + "description": "Configure advanced options for watching. See the chokidar documentation for the possible options.", + "link": "https://github.com/paulmillr/chokidar#api", + "properties": { + "persistent": { + "type": "boolean" + }, + "ignored": { + "type": "string" + }, + "ignoreInitial": { + "type": "boolean" + }, + "followSymlinks": { + "type": "boolean" + }, + "cwd": { + "type": "string" + }, + "disableGlobbing": { + "type": "boolean" + }, + "usePolling": { + "type": "boolean" + }, + "interval": { + "type": "number" + }, + "binaryInterval": { + "type": "number" + }, + "alwaysStat": { + "type": "boolean" + }, + "depth": { + "type": "number" + }, + "awaitWriteFinish": { + "type": "object", + "additionalProperties": true + }, + "ignorePermissionErrors": { + "type": "boolean" + }, + "atomic": { + "anyOf": [ + { + "type": "boolean" + }, + { + "type": "number" + } + ] + } + } + }, "WebSocketServer": { "anyOf": [ { diff --git a/test/__snapshots__/validate-options.test.js.snap.webpack5 b/test/__snapshots__/validate-options.test.js.snap.webpack5 index 200dbbaf63..9ca856e0f6 100644 --- a/test/__snapshots__/validate-options.test.js.snap.webpack5 +++ b/test/__snapshots__/validate-options.test.js.snap.webpack5 @@ -781,6 +781,14 @@ exports[`options validate should throw an error on the "static" option with '{"s object { … }" `; +exports[`options validate should throw an error on the "static" option with '{"watch":{"invalid":true}}' value 1`] = ` +"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema. + - options.static.watch has an unknown property 'invalid'. These properties are valid: + object { persistent?, ignored?, ignoreInitial?, followSymlinks?, cwd?, disableGlobbing?, usePolling?, interval?, binaryInterval?, alwaysStat?, depth?, awaitWriteFinish?, ignorePermissionErrors?, atomic? } + -> Configure advanced options for watching. See the chokidar documentation for the possible options. + -> Read more at https://github.com/paulmillr/chokidar#api" +`; + exports[`options validate should throw an error on the "static" option with '{"watch":10}' value 1`] = ` "ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema. - options.static should be one of these: @@ -789,14 +797,14 @@ exports[`options validate should throw an error on the "static" option with '{"w -> Read more at https://webpack.js.org/configuration/dev-server/#devserverstatic Details: * options.static.watch should be one of these: - boolean | object { … } + boolean | object { persistent?, ignored?, ignoreInitial?, followSymlinks?, cwd?, disableGlobbing?, usePolling?, interval?, binaryInterval?, alwaysStat?, depth?, awaitWriteFinish?, ignorePermissionErrors?, atomic? } -> Watches for files in static content directory. -> Read more at https://webpack.js.org/configuration/dev-server/#watch Details: * options.static.watch should be a boolean. * options.static.watch should be an object: - object { … } - -> Options for watch. + object { persistent?, ignored?, ignoreInitial?, followSymlinks?, cwd?, disableGlobbing?, usePolling?, interval?, binaryInterval?, alwaysStat?, depth?, awaitWriteFinish?, ignorePermissionErrors?, atomic? } + -> Configure advanced options for watching. See the chokidar documentation for the possible options. -> Read more at https://github.com/paulmillr/chokidar#api" `; @@ -830,10 +838,18 @@ exports[`options validate should throw an error on the "static" option with 'nul object { directory?, staticOptions?, publicPath?, serveIndex?, watch? }" `; +exports[`options validate should throw an error on the "watchFiles" option with '{"options":{"invalid":true}}' value 1`] = ` +"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema. + - options.watchFiles.options has an unknown property 'invalid'. These properties are valid: + object { persistent?, ignored?, ignoreInitial?, followSymlinks?, cwd?, disableGlobbing?, usePolling?, interval?, binaryInterval?, alwaysStat?, depth?, awaitWriteFinish?, ignorePermissionErrors?, atomic? } + -> Configure advanced options for watching. See the chokidar documentation for the possible options. + -> Read more at https://github.com/paulmillr/chokidar#api" +`; + exports[`options validate should throw an error on the "watchFiles" option with '{"options":false}' value 1`] = ` "ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema. - options.watchFiles.options should be an object: - object { … } + object { persistent?, ignored?, ignoreInitial?, followSymlinks?, cwd?, disableGlobbing?, usePolling?, interval?, binaryInterval?, alwaysStat?, depth?, awaitWriteFinish?, ignorePermissionErrors?, atomic? } -> Configure advanced options for watching. See the chokidar documentation for the possible options. -> Read more at https://github.com/paulmillr/chokidar#api" `; diff --git a/test/validate-options.test.js b/test/validate-options.test.js index 9b306c3ddd..97e12fa4b4 100644 --- a/test/validate-options.test.js +++ b/test/validate-options.test.js @@ -485,6 +485,15 @@ const tests = { serveIndex: {}, watch: {}, }, + { + directory: "path", + staticOptions: {}, + publicPath: ["/public1/", "/public2/"], + watch: { + ignored: "*.txt", + usePolling: true, + }, + }, [ "path1", { @@ -512,6 +521,9 @@ const tests = { { watch: 10, }, + { + watch: { invalid: true }, + }, ], }, setupMiddlewares: { @@ -571,6 +583,11 @@ const tests = { { options: false, }, + { + options: { + invalid: true, + }, + }, ], }, };