Skip to content

refactor(es6): refactor LoaderOptionsPlugin to ES6 class#3657

Merged
TheLarkInn merged 4 commits intowebpack:masterfrom
carloscuatin:master
Jan 4, 2017
Merged

refactor(es6): refactor LoaderOptionsPlugin to ES6 class#3657
TheLarkInn merged 4 commits intowebpack:masterfrom
carloscuatin:master

Conversation

@carloscuatin
Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Refactor (ES5 => ES6)

Did you add tests for your changes?

No functionality was changed, existing tests passed

If relevant, link to documentation update:

Not

Summary

Refactors the LoaderOptionsPlugin to an ES6 class. First time contributor 🎉

Does this PR introduce a breaking change?

Not

Other information

@jsf-clabot
Copy link
Copy Markdown

jsf-clabot commented Dec 31, 2016

CLA assistant check
All committers have signed the CLA.

Comment thread lib/LoaderOptionsPlugin.js Outdated
return ["include", "exclude", "test"].indexOf(key) < 0
}).forEach((key) => {
context[key] = options[key];
});
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.

Hm, I wonder; what is the preferred syntax for arrow with single argument:

key => ["include", "exclude", "test"].indexOf(key) < 0
(key) => ["include", "exclude", "test"].indexOf(key) < 0

This is assuming, of course, the single-expression return is allowed... and it'd be a shame IMO if it wasn't :P

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 also wonder what's the preferred syntax for chaining (not sure there's an eslint rule for this):

// single-line; any newlines are part of the argument lists
Object.keys(options).filter(...).forEach(...);
// dot-first
Object.keys(options)
.filter(...)
.forEach(...);
// dot-first indented
Object.keys(options)
	.filter(...)
	.forEach(...);

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.

Looks like a combination of dot-location and newline-per-chained-call can specify the last 2 examples, but no way to control the indent (both are considered valid).

Couldn't find a rule to enforce single line.

Copy link
Copy Markdown
Contributor Author

@carloscuatin carloscuatin Dec 31, 2016

Choose a reason for hiding this comment

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

@Kovensky thanks for the review 👍 , for arrow is single line

(key) => ["include", "exclude", "test"].indexOf(key) < 0

for chaining i prefer

Object.keys(options)
	.filter(...)
	.forEach(...);

Comment thread lib/LoaderOptionsPlugin.js Outdated
const i = resource.indexOf("?");
if(ModuleFilenameHelpers.matchObject(options, i < 0 ? resource : resource.substr(0, i))) {
Object.keys(options).filter((key) => {
return ["include", "exclude", "test"].indexOf(key) < 0
Copy link
Copy Markdown
Member

@Jessidhia Jessidhia Dec 31, 2016

Choose a reason for hiding this comment

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

This could be hoisted into a const filterSet = new Set(["include", "exclude", "test"]) (next to the const options), so the filter can be simplified into .filter(key => !filterSet.has(key)). Basically, anytime you see a membership test, it almost always can be converted into a Set.

I also remember having seen something in another PR about wanting "object"-typed values using let instead of const, but I don't understand the reasoning...

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.

We can ignore the declatation type for now. We don't have an eslint style for it now. I'll probably convert them all to const once we get closer.

Comment thread lib/LoaderOptionsPlugin.js Outdated
compilation.plugin("normal-module-loader", (context, module) => {
let resource = module.resource;
if(!resource) return;
let i = resource.includes("?");
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.

String.prototype.includes is not available on Node 4

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@Kovensky Would a polyfill not apply in this case?

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.

Nope; webpack doesn't seem to be using any polyfills...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

let i = resource.indexOf("?") !== -1

});
}
apply(compiler) {
let options = this.options;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In this case we can use const, because options doesn't change.

const options = this options

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 fine with this for now. I'm going to run eslint --fix when we get to a good point after adding prefer-const.

Comment thread lib/LoaderOptionsPlugin.js Outdated
let i = resource.includes("?");
if(ModuleFilenameHelpers.matchObject(options, !i ? resource : resource.substr(0, i))) {
Object.keys(options)
.filter((key) => !["include", "exclude", "test"].includes(key))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

.filter((key) => !["include", "exclude", "test"].indexOf(key) !== -1

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.

Array.prototype.includes is fine in Node 4.

I do still want this to be a const Set instead of array, though... (#3657 (comment))

Copy link
Copy Markdown
Member

@TheLarkInn TheLarkInn left a comment

Choose a reason for hiding this comment

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

@carloscuatin way to go choosing this as your first ever webpack PR.

For this first review, lets just get the tests passing.

So looks like remove includes and use something node 4 compatible like Set or [1,2,4,5].indexOf(5) > 0 instead.

@carloscuatin
Copy link
Copy Markdown
Contributor Author

@TheLarkInn Thanks, I made the arrangements

});
}
apply(compiler) {
let options = this.options;
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 fine with this for now. I'm going to run eslint --fix when we get to a good point after adding prefer-const.

@TheLarkInn
Copy link
Copy Markdown
Member

Awesome work. Congrats on merging your first webpack PR 🎉 🍾

@TheLarkInn TheLarkInn merged commit fbeb8ca into webpack:master Jan 4, 2017
timse pushed a commit to timse/webpack that referenced this pull request Jan 4, 2017
* refactor(es6): refactor LoaderOptionsPlugin to ES6 class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants