refactor(es6): refactor LoaderOptionsPlugin to ES6 class#3657
refactor(es6): refactor LoaderOptionsPlugin to ES6 class#3657TheLarkInn merged 4 commits intowebpack:masterfrom carloscuatin:master
Conversation
| return ["include", "exclude", "test"].indexOf(key) < 0 | ||
| }).forEach((key) => { | ||
| context[key] = options[key]; | ||
| }); |
There was a problem hiding this comment.
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) < 0This is assuming, of course, the single-expression return is allowed... and it'd be a shame IMO if it wasn't :P
There was a problem hiding this comment.
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(...);There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@Kovensky thanks for the review 👍 , for arrow is single line
(key) => ["include", "exclude", "test"].indexOf(key) < 0for chaining i prefer
Object.keys(options)
.filter(...)
.forEach(...);| 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 |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
| compilation.plugin("normal-module-loader", (context, module) => { | ||
| let resource = module.resource; | ||
| if(!resource) return; | ||
| let i = resource.includes("?"); |
There was a problem hiding this comment.
String.prototype.includes is not available on Node 4
There was a problem hiding this comment.
@Kovensky Would a polyfill not apply in this case?
There was a problem hiding this comment.
Nope; webpack doesn't seem to be using any polyfills...
There was a problem hiding this comment.
let i = resource.indexOf("?") !== -1
| }); | ||
| } | ||
| apply(compiler) { | ||
| let options = this.options; |
There was a problem hiding this comment.
In this case we can use const, because options doesn't change.
const options = this options
There was a problem hiding this comment.
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.
| let i = resource.includes("?"); | ||
| if(ModuleFilenameHelpers.matchObject(options, !i ? resource : resource.substr(0, i))) { | ||
| Object.keys(options) | ||
| .filter((key) => !["include", "exclude", "test"].includes(key)) |
There was a problem hiding this comment.
.filter((key) => !["include", "exclude", "test"].indexOf(key) !== -1
There was a problem hiding this comment.
Array.prototype.includes is fine in Node 4.
I do still want this to be a const Set instead of array, though... (#3657 (comment))
TheLarkInn
left a comment
There was a problem hiding this comment.
@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.
|
@TheLarkInn Thanks, I made the arrangements |
| }); | ||
| } | ||
| apply(compiler) { | ||
| let options = this.options; |
There was a problem hiding this comment.
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.
|
Awesome work. Congrats on merging your first webpack PR 🎉 🍾 |
* refactor(es6): refactor LoaderOptionsPlugin to ES6 class
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