Skip to content

Commit fcac9a3

Browse files
committed
refactor variable cleanup code generation, step one
1 parent 4d378f5 commit fcac9a3

1 file changed

Lines changed: 65 additions & 33 deletions

File tree

‎lib/NormalModule.js‎

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -297,54 +297,86 @@ class NormalModule extends Module {
297297
template.apply(dependency, source, outputOptions, requestShortener, dependencyTemplates);
298298
}
299299

300-
sourceVariables(variable, availableVars, vars, dependencyTemplates, outputOptions, requestShortener) {
300+
sourceVariables(variable, availableVars, dependencyTemplates, outputOptions, requestShortener) {
301301
const name = variable.name;
302302
const expr = variable.expressionSource(dependencyTemplates, outputOptions, requestShortener);
303303

304304
if(availableVars.some(v => v.name === name && v.expression.source() === expr.source())) {
305305
return;
306306
}
307-
vars.push({
307+
return {
308308
name: name,
309309
expression: expr
310-
});
310+
};
311+
}
312+
313+
variableInjectionFunctionWrapperStartCode(varNames) {
314+
const openingIIFEParanthesis = "(";
315+
const args = varNames.join(", ");
316+
return `/* WEBPACK VAR INJECTION */${openingIIFEParanthesis}function(${args}) {`;
317+
}
318+
319+
contextArgument(block) {
320+
if(this === block) {
321+
return this.exportsArgument || "exports";
322+
}
323+
return "this";
324+
}
325+
variableInjectionFunctionWrapperEndCode(varExpressions, block) {
326+
const firstParam = this.contextArgument(block);
327+
const furtherParams = varExpressions.map(e => e.source()).join(", ");
328+
const closingIIFEParanthesis = ")";
329+
return `}.call(${firstParam}, ${furtherParams})${closingIIFEParanthesis}`;
311330
}
312331

313332
sourceBlock(block, availableVars, dependencyTemplates, source, outputOptions, requestShortener) {
314333
block.dependencies.forEach((dependency) => this.sourceDependency(
315334
dependency, dependencyTemplates, source, outputOptions, requestShortener));
316335

317-
const vars = [];
318-
if(block.variables.length > 0) {
319-
block.variables.forEach((variable) => this.sourceVariables(
320-
variable, availableVars, vars, dependencyTemplates, outputOptions, requestShortener));
321-
const varNames = [];
322-
const varExpressions = [];
323-
let varStartCode = "";
324-
let varEndCode = "";
325-
326-
const emitFunction = () => {
327-
if(varNames.length === 0) return;
328-
varStartCode += "/* WEBPACK VAR INJECTION */(function(" + varNames.join(", ") + ") {";
329-
// exports === this, but exports do compress better...
330-
varEndCode = (this === block ? "}.call(" + (this.exportsArgument || "exports") + ", " : "}.call(this, ") +
331-
varExpressions.map(function(e) {
332-
return e.source();
333-
}).join(", ") + "))" + varEndCode;
334-
335-
varNames.length = 0;
336-
varExpressions.length = 0;
337-
};
338-
vars.forEach((v) => {
339-
if(varNames.indexOf(v.name) >= 0) emitFunction();
340-
varNames.push(v.name);
341-
varExpressions.push(v.expression);
336+
const vars = block.variables.map((variable) => this.sourceVariables(
337+
variable, availableVars, dependencyTemplates, outputOptions, requestShortener))
338+
.filter(Boolean);
339+
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: "",
342362
});
343-
emitFunction();
344-
const start = block.range ? block.range[0] : -10;
345-
const end = block.range ? block.range[1] : (this._source.size() + 1);
346-
if(varStartCode) source.insert(start + 0.5, varStartCode);
347-
if(varEndCode) source.insert(end + 0.5, "\n/* WEBPACK VAR INJECTION */" + varEndCode);
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;
374+
if(varStartCode && varEndCode) {
375+
const start = block.range ? block.range[0] : -10;
376+
const end = block.range ? block.range[1] : (this._source.size() + 1);
377+
source.insert(start + 0.5, varStartCode);
378+
source.insert(end + 0.5, "\n/* WEBPACK VAR INJECTION */" + varEndCode);
379+
}
348380
}
349381
block.blocks.forEach((block) => this.sourceBlock(
350382
block, availableVars.concat(vars), dependencyTemplates, source, outputOptions, requestShortener));

0 commit comments

Comments
 (0)