Skip to content

Refactor NormalModule to es6#4259

Merged
sokra merged 23 commits intowebpack:masterfrom
timse:refactor-normalmodule-to-es6
Feb 16, 2017
Merged

Refactor NormalModule to es6#4259
sokra merged 23 commits intowebpack:masterfrom
timse:refactor-normalmodule-to-es6

Conversation

@timse
Copy link
Copy Markdown
Contributor

@timse timse commented Feb 11, 2017

What kind of change does this PR introduce?

refactor

Did you add tests for your changes?

N/A

If relevant, link to documentation update:

N/A

Summary

refactor NormalModule to es6

Does this PR introduce a breaking change?

No

Other information

Some refactorings to split up bigger method bodies in smaller ones. (to make it easier to read for stupid people like me :D )

Copy link
Copy Markdown
Member

@sokra sokra left a comment

Choose a reason for hiding this comment

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

Looks good

Comment thread lib/NormalModule.js Outdated
module._compile(code, filename);
return module.exports;
},
resolve: function(context, request, callback) {
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.

arrow functions

Comment thread lib/NormalModule.js
return this.applyNoParseRule(noParseRule, request);
}

for(let i = 0; i < noParseRule.length; i++) {
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 add a tests for this. It's new and not tested.

Comment thread lib/NormalModule.js Outdated
super.disconnect();
}

markModuleAsErrored() {
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 rewrite this method to take an error argument and be the only place where this.error = error is set? This should reduce the number of places where this.error is written and make the control flow more clear.

Comment thread lib/NormalModule.js Outdated
}

source(dependencyTemplates, outputOptions, requestShortener) {
let hash = require("crypto").createHash("md5");
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.

require crypto on top of the his file.

Comment thread lib/NormalModule.js Outdated
if(varNames.length === 0) return;
varStartCode += "/* WEBPACK VAR INJECTION */(function(" + varNames.join(", ") + ") {";
// exports === this in the topLevelBlock, but exports do compress better...
varEndCode = (topLevelBlock === block ? "}.call(" + (topLevelBlock.exportsArgument || "exports") + ", " : "}.call(this, ") +
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 line more readable? i. e. by moving some parts into local variables.

Comment thread lib/NormalModule.js Outdated
needRebuild(fileTimestamps, contextTimestamps) {
const highestFileDepTimestamp = this.getHighestTimestamp(
this.fileDependencies, fileTimestamps);
// if the hightest is Infinity, we need a needRebuild
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 need a needRebuild -> we need a rebuild

@timse
Copy link
Copy Markdown
Contributor Author

timse commented Feb 12, 2017

will do the tasks tomorrow! :)
looks like i missed out on the entire source method.
Will take a closer look at it!

tasks:

  • fix comments
  • move crypto to top
  • use object method expression
  • change markModuleAsErrored
  • add tests
  • take a closer look at source

Comment thread lib/NormalModule.js Outdated
// check if module should not be parsed
// returns "true" if the module should !not! be parsed
// returns "false" if the module !must! be parsed
preventParsing(noParseRule, request) {
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.

preventParsing -> isParsingPrevented

Comment thread lib/NormalModule.js Outdated
}

variableInjectionFunctionWrapperStartCode(varNames) {
const openingIIFEParanthesis = "(";
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.

This is an overkill. Just add the ( to the string and add a comment if you want to make it clear.

Comment thread lib/NormalModule.js Outdated
variableInjectionFunctionWrapperEndCode(varExpressions, block) {
const firstParam = this.contextArgument(block);
const furtherParams = varExpressions.map(e => e.source()).join(", ");
const closingIIFEParanthesis = ")";
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.

same

Comment thread lib/NormalModule.js Outdated
}

splitVariablesInUniqueNamedChunks(vars) {
const chunkCollection = vars.reduce((chunkCol, variable) => {
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.

This can be simplied to not require a tempChunk.

return vars.reduce((chunks, variable) => {
  const current = chunks[chunks.length - 1];
  if(current.some(v => v.name === variable.name)) {
    chunks.push([variable]);
  } else {
    current.push(variable);
  }
  return chunks;
}, [[]]);

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.

fair enough

Comment thread lib/NormalModule.js Outdated
source: source,
hash: hashDigest
};
const topLevelBlock = this;
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.

remove this line

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.

just realized that even the parameter this resembles doesnt even exist :)

Comment thread test/NormalModule.test.js Outdated
name: "baz"
},
{
name: "wurst"
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.

use english names.

Comment thread test/NormalModule.test.js Outdated
loaders = [];
resource = "some/resource";
parser = {
parser() {}
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.

This is weird. A Parser doesn't have a parser method. I would expect constructor() {} or parse() {}.

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.

this was supposed to be parse! nice catch

Comment thread test/NormalModule.test.js Outdated
describe("given a noParseRule", function() {
let returnValOfSpy;
beforeEach(function() {
returnValOfSpy = Math.random() >= 0.5 ? true : false;
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.

Never use Math.random in tests. They should be deterministic.

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.

i kind of wanted to make them be random, but i guess that doesnt help should one ever try to debug such a test. will change

Comment thread test/NormalModule.test.js Outdated
let someRules;
beforeEach(function() {
someRules = [
Math.random() >= 0.5 ? "some rule" : /some rule/,
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.

Never use Math.random in tests. They should be deterministic.

Comment thread test/NormalModule.test.js Outdated
beforeEach(function() {
variables = [{
name: "foo"
},
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.

The array styling looks weird.

The following should be accepted by the linter:

variables = [{
  name: "foo"
}, {
  name: "bar"
}]

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.

it does look weird
but if i do what you suggest (what i tried in the first place) the linter complains.
Having it not like that makes eslint complain though.
The two linters should be aligned at some point

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.

+++ test/NormalModule.test.js
@@ -245,22 +245,22 @@
 	describe("#splitVariablesInUniqueNamedChunks", function() {
 		let variables;
 		beforeEach(function() {
 			variables = [{
-				name: "foo"
-			},
-			{
-				name: "bar"
-			},
-			{
-				name: "baz"
-			},
-			{
-				name: "some"
-			},
-			{
-				name: "more"
-			}
+					name: "foo"
+				},
+				{
+					name: "bar"
+				},
+				{
+					name: "baz"
+				},
+				{
+					name: "some"
+				},
+				{
+					name: "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.

Try it with }, { in between. See my above example.

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.

Will try!

@sokra sokra merged commit 27deabc into webpack:master Feb 16, 2017
@sokra
Copy link
Copy Markdown
Member

sokra commented Feb 16, 2017

Thanks

@timse timse deleted the refactor-normalmodule-to-es6 branch February 16, 2017 10:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants