@@ -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
0 commit comments