Skip to content

Commit c71840a

Browse files
committed
refactor part 2, add comments
1 parent fcac9a3 commit c71840a

1 file changed

Lines changed: 67 additions & 35 deletions

File tree

‎lib/NormalModule.js‎

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -329,48 +329,80 @@ class NormalModule extends Module {
329329
return `}.call(${firstParam}, ${furtherParams})${closingIIFEParanthesis}`;
330330
}
331331

332+
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);
339+
} 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];
347+
}
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;
359+
}
360+
332361
sourceBlock(block, availableVars, dependencyTemplates, source, outputOptions, requestShortener) {
333362
block.dependencies.forEach((dependency) => this.sourceDependency(
334363
dependency, dependencyTemplates, source, outputOptions, requestShortener));
335364

365+
/**
366+
* Get the variables of all blocks that we need to inject.
367+
* These will contain the variable name and its expression.
368+
* The name will be added as a paramter in a IIFE the expression as its value.
369+
*/
336370
const vars = block.variables.map((variable) => this.sourceVariables(
337371
variable, availableVars, dependencyTemplates, outputOptions, requestShortener))
338-
.filter(Boolean);
372+
.filter(Boolean);
373+
374+
// if we actually have variables
339375
if(vars.length > 0) {
340-
const injectionResult = vars.reduce((injections, variable) => {
341-
if(injections.varNames.indexOf(variable.name) >= 0) {
342-
const functionWrapperStart = this.variableInjectionFunctionWrapperStartCode(
343-
injections.varNames);
344-
const functionWrapperEnd = this.variableInjectionFunctionWrapperEndCode(
345-
injections.varExpressions, block);
346-
// add IIFE to existing injection wrapper IIFEs
347-
injections.varStartCode = injections.varStartCode + functionWrapperStart;
348-
injections.varEndCode = functionWrapperEnd + injections.varEndCode;
349-
// reset varNames and expressions
350-
injections.varNames = [];
351-
injections.varExpressions = [];
352-
}
353-
injections.varNames.push(variable.name);
354-
injections.varExpressions.push(variable.expression);
355-
356-
return injections;
357-
}, {
358-
varNames: [],
359-
varExpressions: [],
360-
varStartCode: "",
361-
varEndCode: "",
362-
});
363-
if(injectionResult.varNames.length !== 0) {
364-
const functionWrapperStart = this.variableInjectionFunctionWrapperStartCode(
365-
injectionResult.varNames);
366-
const functionWrapperEnd = this.variableInjectionFunctionWrapperEndCode(
367-
injectionResult.varExpressions, block);
368-
// add IIFE to existing injection wrapper IIFEs
369-
injectionResult.varStartCode = injectionResult.varStartCode + functionWrapperStart;
370-
injectionResult.varEndCode = functionWrapperEnd + injectionResult.varEndCode;
371-
}
372-
const varStartCode = injectionResult.varStartCode;
373-
const varEndCode = injectionResult.varEndCode;
376+
/**
377+
* Split all variables up into chunks of unique names.
378+
* e.g. imagine you have the following variable names that need to be injected:
379+
* [foo, bar, baz, foo, wurst, suppe]
380+
* we can not inject "foo" twice, therefore we just make two IIFEs like so:
381+
* (function(foo, bar, baz){
382+
* (function(foo, wurst, suppe){
383+
* ...
384+
* }(...));
385+
* }(...));
386+
*
387+
* "splitVariablesInUniqueNamedChunks" splits the variables shown above up to this:
388+
* [[foo, bar, baz], [foo, wurst, suppe]]
389+
*/
390+
const injectionVariableChunks = this.splitVariablesInUniqueNamedChunks(vars);
391+
392+
// create all the beginnings of IIFEs
393+
const functionWrapperStarts = injectionVariableChunks.map((variableChunk) => variableChunk.map(variable => variable.name))
394+
.map(names => this.variableInjectionFunctionWrapperStartCode(names));
395+
396+
// and all the ends
397+
const functionWrapperEnds = injectionVariableChunks.map((variableChunk) => variableChunk.map(variable => variable.expression))
398+
.map(expressions => this.variableInjectionFunctionWrapperEndCode(expressions, block));
399+
400+
// join them to one big string
401+
const varStartCode = functionWrapperStarts.join("");
402+
// reverse the ends first before joining them, as the last added must be the inner most
403+
const varEndCode = functionWrapperEnds.reverse().join("");
404+
405+
// if we have anything, add it to the source
374406
if(varStartCode && varEndCode) {
375407
const start = block.range ? block.range[0] : -10;
376408
const end = block.range ? block.range[1] : (this._source.size() + 1);

0 commit comments

Comments
 (0)