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

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

Merged
merged 12 commits into from
Jul 21, 2017

Conversation

hulkish
Copy link
Contributor

@hulkish hulkish commented Jul 12, 2017

Closes #4, #64.

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

Copy link
Contributor Author

@hulkish hulkish Jul 12, 2017

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
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
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
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
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
src/index.js Outdated
const defaultUglifyOptions = {
output: {
comments: /^\**!|@preserve|@license|@cc_on/,
beautify: false,
Copy link
Contributor Author

@hulkish hulkish Jul 12, 2017

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'",
Copy link
Contributor Author

@hulkish hulkish Jul 12, 2017

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

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

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

@hulkish hulkish Jul 12, 2017

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.

@@ -0,0 +1,50 @@
{
Copy link
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
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
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
Copy link
Member

@michael-ciniawsky michael-ciniawsky left a 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';
Copy link
Member

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');
Copy link
Member

Choose a reason for hiding this comment

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

UglifyJsPlugin => UglifyJS Plugin

@@ -0,0 +1,50 @@
{
Copy link
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 😛

}
},
"output": {
"type": ["object", "null"]
Copy link
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
Contributor Author

Choose a reason for hiding this comment

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

Copy link
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.

"properties": {
"ecma": {
"type": "number",
"enum": [5, 6, 7, 8]
Copy link
Member

@michael-ciniawsky michael-ciniawsky Jul 12, 2017

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'",
Copy link
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
Copy link
Contributor Author

hulkish commented Jul 13, 2017

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
options.json Outdated
"uglifyOptions": {
"additionalProperties": true,
"type": "object",
"properties": {
Copy link
Member

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": {
Copy link
Member

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"
Copy link
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
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';
Copy link
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