-
-
Notifications
You must be signed in to change notification settings - Fork 179
feat(index): add options
validation (schema-utils
)
#80
feat(index): add options
validation (schema-utils
)
#80
Conversation
src/index.js
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems not related to this PR
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'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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok sure np
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.
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!
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.
bahaha : 🌮
src/index.js
Outdated
const defaultUglifyOptions = { | ||
output: { | ||
comments: /^\**!|@preserve|@license|@cc_on/, | ||
beautify: false, |
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.
Removed these defaults because these are the same defaults uglify-es
uses internally:
beautify: false,
semicolons: true,
shebang: true,
@@ -10,7 +10,7 @@ | |||
], | |||
"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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
=> 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 :)
src/index.js
Outdated
this.uglifyOptions = this.options.uglifyOptions || {}; | ||
} | ||
|
||
static buildDefaultUglifyOptions({ ecma, warnings, parse = {}, compress = {}, mangle, output, toplevel, ie8 }) { |
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.
Simplified this by leveraging destructering with default values in the plugin's constructor.
src/options-schema.json
Outdated
@@ -0,0 +1,50 @@ | |||
{ |
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.
Reworked from #64
src/index.js
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replaced with logic in constructor
c8673e1
to
0810394
Compare
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.
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): ....
src/index.js
Outdated
import uglify from 'uglify-es'; | ||
import optionsSchema from './options-schema.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.
optionsSchema
=> schema
src/index.js
Outdated
this.options = options || {}; | ||
} | ||
constructor(options = {}) { | ||
validateOptions(optionsSchema, options, 'UglifyJsPlugin'); |
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.
UglifyJsPlugin
=> UglifyJS Plugin
src/options-schema.json
Outdated
@@ -0,0 +1,50 @@ | |||
{ |
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.
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 😛
src/options-schema.json
Outdated
} | ||
}, | ||
"output": { | ||
"type": ["object", "null"] |
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.
Is null
valid ? 🙃
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.
yes, it's default value is null: https://github.com/mishoo/UglifyJS2#user-content-minify-options
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.
I was referring to the schema validation here :) e.g {Regex}, {Function}
aren't supported by ajv
(schema-utils
) yet.
src/options-schema.json
Outdated
"properties": { | ||
"ecma": { | ||
"type": "number", | ||
"enum": [5, 6, 7, 8] |
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.
Could you make this throw and see if the ValidationError
is still displayed correctly with this additional info please?
@@ -10,7 +10,7 @@ | |||
], | |||
"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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
=> 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
)
options.json
Outdated
"uglifyOptions": { | ||
"additionalProperties": true, | ||
"type": "object", | ||
"properties": { |
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.
ie8
?
options.json
Outdated
@@ -0,0 +1,51 @@ | |||
{ | |||
"type": "object", | |||
"properties": { |
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.
include, exclude, parallel
?
package.json
Outdated
@@ -6,7 +6,8 @@ | |||
"license": "MIT", | |||
"main": "dist/cjs.js", | |||
"files": [ | |||
"dist" | |||
"dist", | |||
"options.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.
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
src/index.js
Outdated
@@ -7,6 +7,8 @@ import { SourceMapConsumer } from 'source-map'; | |||
import { SourceMapSource, RawSource, ConcatSource } from 'webpack-sources'; | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
../options.json
=> ./options.json
(../
=> ./
) ?
Closes #4, #64.