Skip to content

fix: schema #783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ The middleware accepts an `options` Object. The following is a property referenc
Type: `Array`
Default: `[ 'GET', 'HEAD' ]`

This property allows a user to pass the list of HTTP request methods accepted by the server.
This property allows a user to pass the list of HTTP request methods accepted by the middleware\*\*.

### headers

Expand Down Expand Up @@ -107,26 +107,20 @@ For more information about `publicPath`, please see [the webpack documentation](
Type: `Boolean`
Default: `undefined`

Instructs the module to enable or disable the server-side rendering mode. Please
see [Server-Side Rendering](#server-side-rendering) for more information.
Instructs the module to enable or disable the server-side rendering mode.
Please see [Server-Side Rendering](#server-side-rendering) for more information.

### writeToDisk

Type: `Boolean|Function`
Default: `false`

If `true`, the option will instruct the module to write files to the configured
location on disk as specified in your `webpack` config file. _Setting
`writeToDisk: true` won't change the behavior of the `webpack-dev-middleware`,
and bundle files accessed through the browser will still be served from memory._
This option provides the same capabilities as the
[`WriteFilePlugin`](https://github.com/gajus/write-file-webpack-plugin/pulls).

This option also accepts a `Function` value, which can be used to filter which
files are written to disk. The function follows the same premise as
[`Array#filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
in which a return value of `false` _will not_ write the file, and a return value
of `true` _will_ write the file to disk. eg.
If `true`, the option will instruct the module to write files to the configured location on disk as specified in your `webpack` config file.
_Setting `writeToDisk: true` won't change the behavior of the `webpack-dev-middleware`, and bundle files accessed through the browser will still be served from memory._
This option provides the same capabilities as the [`WriteFilePlugin`](https://github.com/gajus/write-file-webpack-plugin/pulls).

This option also accepts a `Function` value, which can be used to filter which files are written to disk.
The function follows the same premise as [`Array#filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) in which a return value of `false` _will not_ write the file, and a return value of `true` _will_ write the file to disk. eg.

```js
const webpack = require('webpack');
Expand Down
25 changes: 22 additions & 3 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
"type": "object",
"properties": {
"mimeTypes": {
"description": "Allows a user to register custom mime types or extension mappings.",
"type": "object"
},
"writeToDisk": {
"description": "Allows to write generated files on disk.",
"anyOf": [
{
"type": "boolean"
Expand All @@ -15,30 +17,47 @@
]
},
"methods": {
"description": "Allows to pass the list of HTTP request methods accepted by the middleware.",
"type": "array",
"items": {
"type": "string"
"type": "string",
"minlength": "1"
}
},
"headers": {
"type": "object"
},
"publicPath": {
"type": "string"
"description": "The `publicPath` specifies the public URL address of the output files when referenced in a browser.",
"anyOf": [
{
"enum": ["auto"]
},
{
"type": "string"
},
{
"instanceof": "Function"
}
]
},
"serverSideRender": {
"description": "Instructs the module to enable or disable the server-side rendering mode.",
"type": "boolean"
},
"outputFileSystem": {
"description": "Set the default file system which will be used by webpack as primary destination of generated files.",
"type": "object"
},
"index": {
"description": "Allows to serve an index of the directory.",
"anyOf": [
{
"type": "boolean"
},
{
"type": "string"
"type": "string",
"minlength": "1"
}
]
}
Expand Down
29 changes: 22 additions & 7 deletions test/__snapshots__/validation-options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exports[`validation should throw an error on the "index" option with "{}" value
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.index should be one of these:
boolean | string
-> Allows to serve an index of the directory.
Details:
* options.index should be a boolean.
* options.index should be a string."
Expand All @@ -19,6 +20,7 @@ exports[`validation should throw an error on the "index" option with "0" value 1
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.index should be one of these:
boolean | string
-> Allows to serve an index of the directory.
Details:
* options.index should be a boolean.
* options.index should be a string."
Expand All @@ -27,46 +29,59 @@ exports[`validation should throw an error on the "index" option with "0" value 1
exports[`validation should throw an error on the "methods" option with "{}" value 1`] = `
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.methods should be an array:
[string, ...]"
[string, ...]
-> Allows to pass the list of HTTP request methods accepted by the middleware."
`;

exports[`validation should throw an error on the "methods" option with "true" value 1`] = `
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.methods should be an array:
[string, ...]"
[string, ...]
-> Allows to pass the list of HTTP request methods accepted by the middleware."
`;

exports[`validation should throw an error on the "mimeTypes" option with "foo" value 1`] = `
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.mimeTypes should be an object:
object { … }"
object { … }
-> Allows a user to register custom mime types or extension mappings."
`;

exports[`validation should throw an error on the "outputFileSystem" option with "false" value 1`] = `
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.outputFileSystem should be an object:
object { … }"
object { … }
-> Set the default file system which will be used by webpack as primary destination of generated files."
`;

exports[`validation should throw an error on the "publicPath" option with "false" value 1`] = `
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.publicPath should be a string."
- options.publicPath should be one of these:
\\"auto\\" | string | function
-> The \`publicPath\` specifies the public URL address of the output files when referenced in a browser.
Details:
* options.publicPath should be \\"auto\\".
* options.publicPath should be a string.
* options.publicPath should be an instance of function."
`;

exports[`validation should throw an error on the "serverSideRender" option with "0" value 1`] = `
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.serverSideRender should be a boolean."
- options.serverSideRender should be a boolean.
-> Instructs the module to enable or disable the server-side rendering mode."
`;

exports[`validation should throw an error on the "serverSideRender" option with "foo" value 1`] = `
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.serverSideRender should be a boolean."
- options.serverSideRender should be a boolean.
-> Instructs the module to enable or disable the server-side rendering mode."
`;

exports[`validation should throw an error on the "writeToDisk" option with "{}" value 1`] = `
"Invalid options object. Dev Middleware has been initialized using an options object that does not match the API schema.
- options.writeToDisk should be one of these:
boolean | function
-> Allows to write generated files on disk.
Details:
* options.writeToDisk should be a boolean.
* options.writeToDisk should be an instance of function."
Expand Down
9 changes: 8 additions & 1 deletion test/validation-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ describe('validation', () => {
failure: [true],
},
publicPath: {
success: ['/foo'],
success: [
'/foo',
'',
'auto',
() => {
return '/public/path';
},
],
failure: [false],
},
serverSideRender: {
Expand Down