Skip to content

Commit c06b066

Browse files
committed
apply review tasks
1 parent b2b7021 commit c06b066

2 files changed

Lines changed: 52 additions & 54 deletions

File tree

‎lib/NormalModule.js‎

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class NormalModule extends Module {
215215
// check if module should not be parsed
216216
// returns "true" if the module should !not! be parsed
217217
// returns "false" if the module !must! be parsed
218-
preventParsing(noParseRule, request) {
218+
shouldPreventParsing(noParseRule, request) {
219219
// if no noParseRule exists, return false
220220
// the module !must! be parsed.
221221
if(!noParseRule) {
@@ -264,7 +264,7 @@ class NormalModule extends Module {
264264
// check if this module should !not! be parsed.
265265
// if so, exit here;
266266
const noParseRule = options.module && options.module.noParse;
267-
if(this.preventParsing(noParseRule, this.request)) {
267+
if(this.shouldPreventParsing(noParseRule, this.request)) {
268268
return callback();
269269
}
270270

@@ -310,10 +310,14 @@ class NormalModule extends Module {
310310
};
311311
}
312312

313+
/*
314+
* creates the start part of a IIFE around the module to inject a variable name
315+
* (function(...){ <- this part
316+
* }.call(...))
317+
*/
313318
variableInjectionFunctionWrapperStartCode(varNames) {
314-
const openingIIFEParanthesis = "(";
315319
const args = varNames.join(", ");
316-
return `/* WEBPACK VAR INJECTION */${openingIIFEParanthesis}function(${args}) {`;
320+
return `/* WEBPACK VAR INJECTION */(function(${args}) {`;
317321
}
318322

319323
contextArgument(block) {
@@ -322,40 +326,34 @@ class NormalModule extends Module {
322326
}
323327
return "this";
324328
}
329+
330+
/*
331+
* creates the end part of a IIFE around the module to inject a variable name
332+
* (function(...){
333+
* }.call(...)) <- this part
334+
*/
325335
variableInjectionFunctionWrapperEndCode(varExpressions, block) {
326336
const firstParam = this.contextArgument(block);
327337
const furtherParams = varExpressions.map(e => e.source()).join(", ");
328-
const closingIIFEParanthesis = ")";
329-
return `}.call(${firstParam}, ${furtherParams})${closingIIFEParanthesis}`;
338+
return `}.call(${firstParam}, ${furtherParams}))`;
330339
}
331340

332341
splitVariablesInUniqueNamedChunks(vars) {
333-
const chunkCollection = vars.reduce((chunkCol, variable) => {
334-
const nameStore = chunkCol.names;
335-
// if we dont know this variable name yet, add it to the tempChunk and names list
336-
if(nameStore.length === 0 || nameStore.indexOf(variable.name) === -1) {
337-
chunkCol.tempChunk.push(variable);
338-
chunkCol.names.push(variable.name);
342+
return vars.reduce((chunks, variable) => {
343+
const current = chunks[chunks.length - 1];
344+
// check if variable with same name exists already
345+
// if so create a new chunk of variables.
346+
const variableNameAlreadyExists = current.some(v => v.name === variable.name);
347+
348+
if(variableNameAlreadyExists) {
349+
// start new chunk with current variable
350+
chunks.push([variable]);
339351
} else {
340-
/**
341-
* We know this variable name already.
342-
* Add current tempChunk to chunks and start a new tempChunk/names list
343-
*/
344-
chunkCol.chunks.push(chunkCol.tempChunk);
345-
chunkCol.tempChunk = [variable];
346-
chunkCol.names = [variable.name];
352+
// else add it to current chunk
353+
current.push(variable);
347354
}
348-
return chunkCol;
349-
}, {
350-
chunks: [],
351-
names: [],
352-
tempChunk: []
353-
});
354-
const finalChunks = chunkCollection.chunks;
355-
if(chunkCollection.tempChunk.length !== 0) {
356-
finalChunks.push(chunkCollection.tempChunk);
357-
}
358-
return finalChunks;
355+
return chunks;
356+
}, [[]]);
359357
}
360358

361359
sourceBlock(block, availableVars, dependencyTemplates, source, outputOptions, requestShortener) {
@@ -376,16 +374,16 @@ class NormalModule extends Module {
376374
/**
377375
* Split all variables up into chunks of unique names.
378376
* e.g. imagine you have the following variable names that need to be injected:
379-
* [foo, bar, baz, foo, wurst, suppe]
377+
* [foo, bar, baz, foo, some, more]
380378
* we can not inject "foo" twice, therefore we just make two IIFEs like so:
381379
* (function(foo, bar, baz){
382-
* (function(foo, wurst, suppe){
380+
* (function(foo, some, more){
383381
* ...
384382
* }(...));
385383
* }(...));
386384
*
387385
* "splitVariablesInUniqueNamedChunks" splits the variables shown above up to this:
388-
* [[foo, bar, baz], [foo, wurst, suppe]]
386+
* [[foo, bar, baz], [foo, some, more]]
389387
*/
390388
const injectionVariableChunks = this.splitVariablesInUniqueNamedChunks(vars);
391389

@@ -429,8 +427,8 @@ class NormalModule extends Module {
429427
source: source,
430428
hash: hashDigest
431429
};
432-
const topLevelBlock = this;
433-
this.sourceBlock(this, [], dependencyTemplates, source, outputOptions, requestShortener, topLevelBlock);
430+
431+
this.sourceBlock(this, [], dependencyTemplates, source, outputOptions, requestShortener);
434432
return new CachedSource(source);
435433
}
436434

‎test/NormalModule.test.js‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe("NormalModule", function() {
2323
loaders = [];
2424
resource = "some/resource";
2525
parser = {
26-
parser() {}
26+
parse() {}
2727
};
2828
normalModule = new NormalModule(
2929
request,
@@ -161,7 +161,7 @@ describe("NormalModule", function() {
161161
});
162162
});
163163
describe("without the module having source", function() {
164-
let expectedSource = "wurst suppe";
164+
let expectedSource = "some source";
165165
beforeEach(function() {
166166
normalModule._source = new RawSource(expectedSource);
167167
});
@@ -255,10 +255,10 @@ describe("NormalModule", function() {
255255
name: "baz"
256256
},
257257
{
258-
name: "wurst"
258+
name: "some"
259259
},
260260
{
261-
name: "suppe"
261+
name: "more"
262262
}
263263
];
264264
});
@@ -296,15 +296,15 @@ describe("NormalModule", function() {
296296
content = rule + "some-content";
297297
});
298298
it("returns true", function() {
299-
normalModule.preventParsing(rule, content).should.eql(true);
299+
normalModule.shouldPreventParsing(rule, content).should.eql(true);
300300
});
301301
});
302302
describe("and the content does not start with the string specified in rule", function() {
303303
beforeEach(function() {
304304
content = "some-content";
305305
});
306306
it("returns false", function() {
307-
normalModule.preventParsing(rule, content).should.eql(false);
307+
normalModule.shouldPreventParsing(rule, content).should.eql(false);
308308
});
309309
});
310310
});
@@ -317,47 +317,47 @@ describe("NormalModule", function() {
317317
content = rule + "some-content";
318318
});
319319
it("returns true", function() {
320-
normalModule.preventParsing(rule, content).should.eql(true);
320+
normalModule.shouldPreventParsing(rule, content).should.eql(true);
321321
});
322322
});
323323
describe("and the content does not match the rule", function() {
324324
beforeEach(function() {
325325
content = "some-content";
326326
});
327327
it("returns false", function() {
328-
normalModule.preventParsing(rule, content).should.eql(false);
328+
normalModule.shouldPreventParsing(rule, content).should.eql(false);
329329
});
330330
});
331331
});
332332
});
333333

334-
describe("#preventParsing", function() {
334+
describe("#shouldPreventParsing", function() {
335335
let applyNoParseRuleSpy;
336336
beforeEach(function() {
337337
applyNoParseRuleSpy = sinon.stub();
338338
normalModule.applyNoParseRule = applyNoParseRuleSpy;
339339
});
340340
describe("given no noParseRule", function() {
341341
it("returns false", function() {
342-
normalModule.preventParsing().should.eql(false);
342+
normalModule.shouldPreventParsing().should.eql(false);
343343
applyNoParseRuleSpy.callCount.should.eql(0);
344344
});
345345
});
346346
describe("given a noParseRule", function() {
347347
let returnValOfSpy;
348348
beforeEach(function() {
349-
returnValOfSpy = Math.random() >= 0.5 ? true : false;
349+
returnValOfSpy = true;
350350
applyNoParseRuleSpy.returns(returnValOfSpy);
351351
});
352352
describe("that is a string", function() {
353353
it("calls and returns whatever applyNoParseRule returns", function() {
354-
normalModule.preventParsing("some rule").should.eql(returnValOfSpy);
354+
normalModule.shouldPreventParsing("some rule").should.eql(returnValOfSpy);
355355
applyNoParseRuleSpy.callCount.should.eql(1);
356356
});
357357
});
358358
describe("that is a regex", function() {
359359
it("calls and returns whatever applyNoParseRule returns", function() {
360-
normalModule.preventParsing("some rule").should.eql(returnValOfSpy);
360+
normalModule.shouldPreventParsing("some rule").should.eql(returnValOfSpy);
361361
applyNoParseRuleSpy.callCount.should.eql(1);
362362
});
363363
});
@@ -366,9 +366,9 @@ describe("NormalModule", function() {
366366
let someRules;
367367
beforeEach(function() {
368368
someRules = [
369-
Math.random() >= 0.5 ? "some rule" : /some rule/,
370-
Math.random() >= 0.5 ? "some rule1" : /some rule1/,
371-
Math.random() >= 0.5 ? "some rule2" : /some rule2/,
369+
"some rule",
370+
/some rule1/,
371+
"some rule2",
372372
];
373373
});
374374
describe("and none of them match", function() {
@@ -377,7 +377,7 @@ describe("NormalModule", function() {
377377
applyNoParseRuleSpy.returns(returnValOfSpy);
378378
});
379379
it("returns false", function() {
380-
normalModule.preventParsing(someRules).should.eql(returnValOfSpy);
380+
normalModule.shouldPreventParsing(someRules).should.eql(returnValOfSpy);
381381
applyNoParseRuleSpy.callCount.should.eql(3);
382382
});
383383
});
@@ -387,7 +387,7 @@ describe("NormalModule", function() {
387387
applyNoParseRuleSpy.returns(returnValOfSpy);
388388
});
389389
it("returns true", function() {
390-
normalModule.preventParsing(someRules).should.eql(returnValOfSpy);
390+
normalModule.shouldPreventParsing(someRules).should.eql(returnValOfSpy);
391391
applyNoParseRuleSpy.callCount.should.eql(1);
392392
});
393393
});
@@ -399,7 +399,7 @@ describe("NormalModule", function() {
399399
applyNoParseRuleSpy.onCall(2).returns(true);
400400
});
401401
it("returns true", function() {
402-
normalModule.preventParsing(someRules).should.eql(returnValOfSpy);
402+
normalModule.shouldPreventParsing(someRules).should.eql(returnValOfSpy);
403403
applyNoParseRuleSpy.callCount.should.eql(3);
404404
});
405405
});

0 commit comments

Comments
 (0)