-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
Use JSON schema to validate webpack config (fixes #2971) #2974
Use JSON schema to validate webpack config (fixes #2971) #2974
Conversation
}, | ||
"type": "object" | ||
}, | ||
"output": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing output.auxiliaryComment
, heres the PR for it.
I hadn't gotten to documenting it yet. 👀
It is essentially:
auxiliaryComment?: string|AuxCommentObject
If object
properties are:
interface AuxCommentObject {
root?: string
commonjs?: string
commonjs2?: string
amd?: string
}
Let me know if this is unclear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3339271
to
f94e945
Compare
Tests are failing because: [ { keyword: 'additionalProperties',
dataPath: '',
schemaPath: '#/additionalProperties',
params: { additionalProperty: 'name' },
message: 'should NOT have additional properties' } ] [ { keyword: 'additionalProperties',
dataPath: '',
schemaPath: '#/additionalProperties',
params: { additionalProperty: 'optimize' },
message: 'should NOT have additional properties' } ] [ { keyword: 'additionalProperties',
dataPath: '',
schemaPath: '#/additionalProperties',
params: { additionalProperty: 'worker' },
message: 'should NOT have additional properties' } ] [ { keyword: 'additionalProperties',
dataPath: '',
schemaPath: '#/additionalProperties',
params: { additionalProperty: 'updateIndex' },
message: 'should NOT have additional properties' } ] [ { keyword: 'additionalProperties',
dataPath: '',
schemaPath: '#/additionalProperties',
params: { additionalProperty: 'stats' },
message: 'should NOT have additional properties' } ] can anyone contribute a description for these? (they are not present in https://webpack.github.io/docs/configuration.html) |
if(!options.entry && !options.plugins) { | ||
var webpackOptionsValidationErrors = validateWebpackOptions(options); | ||
if(webpackOptionsValidationErrors.length) { | ||
console.log("'options' validation errors:", webpackOptionsValidationErrors); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are only allowed use console.log in the CLI not in the API.
Instead rewrite the code to throw a CustomError with a validationErrors
property. Add code to the CLI to check for this property and format the errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about users that use webpack programmatically? e.g. most of the React applications will have a setup using webpack
and webpack-dev-middleware
.
Do I need to document presence of the validationErrors
property?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will document it in the new documentation.
https://github.com/webpack/webpack.js.org/blob/develop/content/api/node.md
Please add |
|
Info on stats @gajus can be gathered from here. |
Also would this error for a user if they added an additional custom property (some sort of metadata) to their config? I know quite a few who do this, and use those options, etc. |
I'm not sure if we should throw when additional keys are encountered... webpack 2 should not have too many breaking changes. It's ok to discourage it and display warnings, but I don't think that we should break. The sass- and the less-loader for instance provide the possibility to read options from the config (because we had to deal with functions as options). |
|
ba08a06
to
3934066
Compare
In the case of /examples/web-worker/webpack.config.js, this ought to be a simple change from: module.exports = {
worker: {
output: {
filename: "hash.worker.js",
chunkFilename: "[id].hash.worker.js"
}
}
} to: var webpack = require("../../");
module.exports = {
plugins: [
new webpack.LoaderOptionsPlugin({
options: {
worker: {
output: {
filename: "hash.worker.js",
chunkFilename: "[id].hash.worker.js"
}
}
}
})
]
} Correct? |
Yes. I forgot |
git clone https://github.com/webpack/webpack.git
cd webpack
npm install
npm link
npm link webpack
npm test is giving:
Unrelated to this PR. |
@@ -1,5 +1,5 @@ | |||
root = true | |||
|
|||
[*.js] | |||
[*.{js,json}] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for npm reasons package.json
must be 2 spaces indent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically, we could have a separate rule for ./package.json
and the rest.
No comments on the error handling? |
My understanding is that all loader-specific options need to be passed using It does not require a change to the loaders. It is only a configuration change. |
1d4869a
to
bae2488
Compare
There will still be loader queries in webpack 2 (and tbh I don't know what the difference between an option and a query is). My problem with this change is, that it needs some time to be implemented in the community. Loader authors need to update their code and publish new versions of it, people need to update their dependencies. I would like to have a smooth transition where you get a warning with webpack 2 and an error with webpack 3. |
Options and queries are interchangeable. Loader authors are not affected. No code change is required to the loader to accept options passed using LoaderOptionsPlugin. Therefore, no update is needed of dependencies. As I said earlier, this only affects webpack configuration.
|
Ajv is using doT to compile validation logic. There is minimal overhead validating an object against a pre-compiled schema. In case of the CLI, options will be validated once by the CLI engine and the second time by API. I prefer to explicitly handle error checking rather than relying on an error getting thrown by the API engine. API needs to retain “validationErrors” object nevertheless to enable debugging when using webpack programmatically.
76920b3
to
4526824
Compare
Message phrasing has been improved. 9e0a95e Can this be merged? |
If I do an npm install with a package.json setup like: I get the error:
And sure enough the folder and file in the path mentioned are missing on the file system. |
|
|
A breaking change from 2.1.0-beta22 to 2.1.0-beta23 really sucks. You might really broke a lot of CI system today... |
beta22 and beta23 are both prerelease versions and there has not been a 2.x release yet. |
Having the "right" to do something is a thing. Not caring about current users (that help to test prerelease right?) and upgrade path is another thing. |
"testing" and "depending" on prerelease software are different things I think this is a good change, random plugins should not pollute the config with random keys in case webpack wants to use them in the future instead of complaining about it, we could help come up with a solution, for example, so loaders just go
im guessing that would include objects and functions Also thanks, I have been looking into static site generators atm so I might look at phenomic when I get home |
It definitely is. I think a simpler workaround than having to use a plugin to pass data should be offered. The errow thrown does not event mention a solution. It could say
I think the best solution will be to support real JS for query. It's the best user experience we can offer an make configuration for loader even more clear than having to split in multiple places. |
- let the dust settle on validation changes webpack/webpack#2974
i have a trouble in webpack2 . but i'm tring to use webpack1' way to code my webapck2. there is error (i'm not good at english .waiting your reply) |
@huizi666 |
I dont understand about "support request",but this is my git ,could you please gitclone git@github.com:huizi666/angular-webpack.git . And checkout the webpack.config.js Thanks~~ @bebraw |
@bebraw may you can send me an email : superhuizi0716@gmail.com ~ ^_^ |
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
What is the current behavior? (You can also link to an open issue here)
Validation is limited to a simple condition:
What is the new behavior?
Validation logic is expressed using
./schemas/webpackOptionsSchema.json
JSON schema.Does this PR introduce a breaking change?
If this PR contains a breaking change, please describe the following...
Impact:
An error is thrown when an unknown configuration is encountered in webpack options object.
This error might appear now because previously unknown configuration was ignored.
Migration path for existing applications:
Remove unknown configuration properties from webpack options object.
To configure a loader, use
LoaderOptionsPlugin
.For a list of valid configuration properties, refer to the webpack documentation (https://webpack.github.io/docs/configuration.html).
Github Issue(s) this is regarding:
Use JSON schema to validate webpack configuration #2971
Other information:
Need to discuss how to approach testing.
JSON schema is declarative. Therefore, the contents of the schema should not be tested. The implementation testing is left to https://github.com/epoberezkin/ajv.
Need to discuss how to inform user about errors.
Just throwing an error is not enough.
I am fine with the current implementation – throwing an error and printing detail info to the
console.log
. I can see how someone can dislike this.Outstanding tasks