Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.

feat(index): add options validation (schema-utils) - #80

Merged
michael-ciniawsky merged 12 commits into
masterfrom
hulkish-OptimizeAndValidateOptions
Jul 21, 2017
Merged

feat(index): add options validation (schema-utils)#80
michael-ciniawsky merged 12 commits into
masterfrom
hulkish-OptimizeAndValidateOptions

Conversation

@hulkish

@hulkish hulkish commented Jul 12, 2017

Copy link
Copy Markdown
Contributor

Closes #4, #64.

Comment thread 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')) {

Copy link
Copy Markdown
Member

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

@hulkish hulkish Jul 12, 2017

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok sure np

Copy link
Copy Markdown
Member

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!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bahaha : 🌮

@hulkish hulkish self-assigned this Jul 12, 2017
Comment thread src/index.js Outdated
const defaultUglifyOptions = {
output: {
comments: /^\**!|@preserve|@license|@cc_on/,
beautify: false,

@hulkish hulkish Jul 12, 2017

Copy link
Copy Markdown
Contributor Author

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,

Comment thread package.json
],
"scripts": {
"start": "npm run build -- -w",
"build": "cross-env NODE_ENV=production babel src -d dist --ignore 'src/**/*.test.js'",

@hulkish hulkish Jul 12, 2017

Copy link
Copy Markdown
Contributor Author

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)

Copy link
Copy Markdown
Member

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 :)

Comment thread src/index.js Outdated
this.uglifyOptions = this.options.uglifyOptions || {};
}

static buildDefaultUglifyOptions({ ecma, warnings, parse = {}, compress = {}, mangle, output, toplevel, ie8 }) {

@hulkish hulkish Jul 12, 2017

Copy link
Copy Markdown
Contributor Author

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.

Comment thread src/options-schema.json
@@ -0,0 +1,50 @@
{

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworked from #64

@hulkish hulkish changed the title feat: add options validation feat(index): add options validation Jul 12, 2017
Comment thread 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 || {};

Copy link
Copy Markdown
Contributor Author

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

@hulkish
hulkish force-pushed the hulkish-OptimizeAndValidateOptions branch from c8673e1 to 0810394 Compare July 12, 2017 12:27

@michael-ciniawsky michael-ciniawsky left a comment

Copy link
Copy Markdown
Member

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): ....

Comment thread src/index.js Outdated
import ModuleFilenameHelpers from 'webpack/lib/ModuleFilenameHelpers';
import validateOptions from 'schema-utils';
import uglify from 'uglify-es';
import optionsSchema from './options-schema.json';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optionsSchema => schema

Comment thread src/index.js Outdated
this.options = options || {};
}
constructor(options = {}) {
validateOptions(optionsSchema, options, 'UglifyJsPlugin');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UglifyJsPlugin => UglifyJS Plugin

Comment thread src/options-schema.json
@@ -0,0 +1,50 @@
{

Copy link
Copy Markdown
Member

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 😛

Comment thread src/options-schema.json
}
},
"output": {
"type": ["object", "null"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is null valid ? 🙃

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

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.

Comment thread src/options-schema.json Outdated
"properties": {
"ecma": {
"type": "number",
"enum": [5, 6, 7, 8]

@michael-ciniawsky michael-ciniawsky Jul 12, 2017

Copy link
Copy Markdown
Member

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?

Comment thread package.json
],
"scripts": {
"start": "npm run build -- -w",
"build": "cross-env NODE_ENV=production babel src -d dist --ignore 'src/**/*.test.js'",

Copy link
Copy Markdown
Member

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 :)

@michael-ciniawsky michael-ciniawsky added this to the 1.0.0 milestone Jul 12, 2017
@michael-ciniawsky michael-ciniawsky changed the title feat(index): add options validation feat(index): add options validation Jul 12, 2017
@hulkish

hulkish commented Jul 13, 2017

Copy link
Copy Markdown
Contributor Author

ok done @michael-ciniawsky 2a3687e

@michael-ciniawsky michael-ciniawsky changed the title feat(index): add options validation feat(index): add options validation (schema-utils) Jul 20, 2017
Comment thread options.json
"uglifyOptions": {
"additionalProperties": true,
"type": "object",
"properties": {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ie8 ?

Comment thread options.json
@@ -0,0 +1,51 @@
{
"type": "object",
"properties": {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include, exclude, parallel ?

Comment thread package.json Outdated
"files": [
"dist"
"dist",
"options.json"

Copy link
Copy Markdown
Member

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 :) 👍

@hulkish
hulkish force-pushed the hulkish-OptimizeAndValidateOptions branch from 2a3687e to 38f2cac Compare July 20, 2017 23:56
@hulkish
hulkish force-pushed the hulkish-OptimizeAndValidateOptions branch from 38f2cac to 5d1ffea Compare July 20, 2017 23:58
Comment thread src/index.js Outdated
import RequestShortener from 'webpack/lib/RequestShortener';
import ModuleFilenameHelpers from 'webpack/lib/ModuleFilenameHelpers';
import validateOptions from 'schema-utils';
import schema from '../options.json';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

../options.json => ./options.json (../ => ./) ?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants