feat(index): add options validation (schema-utils) - #80
Conversation
| const { uglifyOptions } = this.options; | ||
| const uglifiedAssets = new WeakSet(); | ||
|
|
||
| if (typeof this.options.sourceMap === 'undefined' && (compiler.options.devtool === 'sourcemap' || compiler.options.devtool === 'source-map')) { |
There was a problem hiding this comment.
Seems not related to this PR
There was a problem hiding this comment.
You're right. I can separate this into a diff pr if u like. However, I think it's been a missing piece to this plugin for some time now. It's also related to the options so i figured it would be good to include.
Not to mention - this same logic is built into webpack core. I believe this responsibility belongs in this plugin.
If this pr is merged i plan to follow up by making a pr at webpack core repo to extract this logic from there. This also doesn't introduce a breaking change and all tests are passing.
There was a problem hiding this comment.
@hulkish let's wait what other will say, but be good have separate PR for this, it is allow to maintenance CHANGELOG
There was a problem hiding this comment.
If this pr is merged i plan to follow up by making a pr at webpack core repo to extract this logic from there. This also doesn't introduce a breaking change and all tests are passing.
btw good enchantment!
| const defaultUglifyOptions = { | ||
| output: { | ||
| comments: /^\**!|@preserve|@license|@cc_on/, | ||
| beautify: false, |
There was a problem hiding this comment.
Removed these defaults because these are the same defaults uglify-es uses internally:
beautify: false,
semicolons: true,
shebang: true,| ], | ||
| "scripts": { | ||
| "start": "npm run build -- -w", | ||
| "build": "cross-env NODE_ENV=production babel src -d dist --ignore 'src/**/*.test.js'", |
There was a problem hiding this comment.
added this for babel-cli to auto-copy src/options-schema.json to the dist folder. (to prevent MODULE_NOT_FOUND error on published code)
There was a problem hiding this comment.
=> webpack-defaults, but options.json in the root is also ok imho, since it is metadata, still needs to be added to pkg.files then, so really no preference :)
| this.uglifyOptions = this.options.uglifyOptions || {}; | ||
| } | ||
|
|
||
| static buildDefaultUglifyOptions({ ecma, warnings, parse = {}, compress = {}, mangle, output, toplevel, ie8 }) { |
There was a problem hiding this comment.
Simplified this by leveraging destructering with default values in the plugin's constructor.
| @@ -0,0 +1,50 @@ | |||
| { | |||
| const uglifyOptions = UglifyJsPlugin.buildDefaultUglifyOptions(this.uglifyOptions); | ||
| // Making sure output options exists if there is an extractComments options | ||
| if (this.options.extractComments) { | ||
| uglifyOptions.output = uglifyOptions.output || {}; |
There was a problem hiding this comment.
replaced with logic in constructor
c8673e1 to
0810394
Compare
michael-ciniawsky
left a comment
There was a problem hiding this comment.
Please try to separate changes into logical pieces (PR's) whenever possible. The weakSet hoisting and options refactoring is strictly speaking a separate PR :) refactor(index): ....
| import ModuleFilenameHelpers from 'webpack/lib/ModuleFilenameHelpers'; | ||
| import validateOptions from 'schema-utils'; | ||
| import uglify from 'uglify-es'; | ||
| import optionsSchema from './options-schema.json'; |
There was a problem hiding this comment.
optionsSchema => schema
| this.options = options || {}; | ||
| } | ||
| constructor(options = {}) { | ||
| validateOptions(optionsSchema, options, 'UglifyJsPlugin'); |
There was a problem hiding this comment.
UglifyJsPlugin => UglifyJS Plugin
| @@ -0,0 +1,50 @@ | |||
| { | |||
There was a problem hiding this comment.
src/options-schema.json => ./options.json, if it should reside in the src and needs to be copied, the change needs to be made in webpack-defaults beforehand as always 😛
| } | ||
| }, | ||
| "output": { | ||
| "type": ["object", "null"] |
There was a problem hiding this comment.
yes, it's default value is null: https://github.com/mishoo/UglifyJS2#user-content-minify-options
There was a problem hiding this comment.
I was referring to the schema validation here :) e.g {Regex}, {Function} aren't supported by ajv (schema-utils) yet.
| "properties": { | ||
| "ecma": { | ||
| "type": "number", | ||
| "enum": [5, 6, 7, 8] |
There was a problem hiding this comment.
Could you make this throw and see if the ValidationError is still displayed correctly with this additional info please?
| ], | ||
| "scripts": { | ||
| "start": "npm run build -- -w", | ||
| "build": "cross-env NODE_ENV=production babel src -d dist --ignore 'src/**/*.test.js'", |
There was a problem hiding this comment.
=> webpack-defaults, but options.json in the root is also ok imho, since it is metadata, still needs to be added to pkg.files then, so really no preference :)
options validation
|
ok done @michael-ciniawsky 2a3687e |
options validationoptions validation (schema-utils)
| "uglifyOptions": { | ||
| "additionalProperties": true, | ||
| "type": "object", | ||
| "properties": { |
| @@ -0,0 +1,51 @@ | |||
| { | |||
| "type": "object", | |||
| "properties": { | |||
There was a problem hiding this comment.
include, exclude, parallel ?
| "files": [ | ||
| "dist" | ||
| "dist", | ||
| "options.json" |
There was a problem hiding this comment.
I'm 💯 sry about this, your initial way of handling this file via --copy-files in the build script is the better solution, please revert :) 👍
2a3687e to
38f2cac
Compare
38f2cac to
5d1ffea
Compare
| import RequestShortener from 'webpack/lib/RequestShortener'; | ||
| import ModuleFilenameHelpers from 'webpack/lib/ModuleFilenameHelpers'; | ||
| import validateOptions from 'schema-utils'; | ||
| import schema from '../options.json'; |
There was a problem hiding this comment.
../options.json => ./options.json (../ => ./) ?
Closes #4, #64.