Skip to content

Commit 4d378f5

Browse files
committed
untangle source method
1 parent a5d5b9e commit 4d378f5

1 file changed

Lines changed: 74 additions & 64 deletions

File tree

‎lib/NormalModule.js‎

Lines changed: 74 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -285,78 +285,88 @@ class NormalModule extends Module {
285285
});
286286
}
287287

288-
source(dependencyTemplates, outputOptions, requestShortener) {
289-
let hash = crypto.createHash("md5");
288+
getHashDigest() {
289+
const hash = crypto.createHash("md5");
290290
this.updateHash(hash);
291-
hash = hash.digest("hex");
292-
if(this._cachedSource && this._cachedSource.hash === hash) {
293-
return this._cachedSource.source;
294-
}
295-
const _source = this._source;
296-
if(!_source) return new RawSource("throw new Error('No source available');");
297-
const source = new ReplaceSource(_source);
298-
this._cachedSource = {
299-
source: source,
300-
hash: hash
301-
};
302-
const topLevelBlock = this;
291+
return hash.digest("hex");
292+
}
303293

304-
function doDep(dep) {
305-
const template = dependencyTemplates.get(dep.constructor);
306-
if(!template) throw new Error("No template for dependency: " + dep.constructor.name);
307-
template.apply(dep, source, outputOptions, requestShortener, dependencyTemplates);
308-
}
294+
sourceDependency(dependency, dependencyTemplates, source, outputOptions, requestShortener) {
295+
const template = dependencyTemplates.get(dependency.constructor);
296+
if(!template) throw new Error("No template for dependency: " + dependency.constructor.name);
297+
template.apply(dependency, source, outputOptions, requestShortener, dependencyTemplates);
298+
}
309299

310-
function doVariable(availableVars, vars, variable) {
311-
const name = variable.name;
312-
const expr = variable.expressionSource(dependencyTemplates, outputOptions, requestShortener);
300+
sourceVariables(variable, availableVars, vars, dependencyTemplates, outputOptions, requestShortener) {
301+
const name = variable.name;
302+
const expr = variable.expressionSource(dependencyTemplates, outputOptions, requestShortener);
313303

314-
function isEqual(v) {
315-
return v.name === name && v.expression.source() === expr.source();
316-
}
317-
if(availableVars.some(isEqual)) return;
318-
vars.push({
319-
name: name,
320-
expression: expr
304+
if(availableVars.some(v => v.name === name && v.expression.source() === expr.source())) {
305+
return;
306+
}
307+
vars.push({
308+
name: name,
309+
expression: expr
310+
});
311+
}
312+
313+
sourceBlock(block, availableVars, dependencyTemplates, source, outputOptions, requestShortener) {
314+
block.dependencies.forEach((dependency) => this.sourceDependency(
315+
dependency, dependencyTemplates, source, outputOptions, requestShortener));
316+
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);
321342
});
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);
322348
}
349+
block.blocks.forEach((block) => this.sourceBlock(
350+
block, availableVars.concat(vars), dependencyTemplates, source, outputOptions, requestShortener));
351+
}
323352

324-
function doBlock(availableVars, block) {
325-
block.dependencies.forEach(doDep);
326-
const vars = [];
327-
if(block.variables.length > 0) {
328-
block.variables.forEach(doVariable.bind(null, availableVars, vars));
329-
const varNames = [];
330-
const varExpressions = [];
331-
let varStartCode = "";
332-
let varEndCode = "";
333-
334-
const emitFunction = function emitFunction() {
335-
if(varNames.length === 0) return;
336-
varStartCode += "/* WEBPACK VAR INJECTION */(function(" + varNames.join(", ") + ") {";
337-
// exports === this in the topLevelBlock, but exports do compress better...
338-
varEndCode = (topLevelBlock === block ? "}.call(" + (topLevelBlock.exportsArgument || "exports") + ", " : "}.call(this, ") +
339-
varExpressions.map(function(e) {
340-
return e.source();
341-
}).join(", ") + "))" + varEndCode;
342-
343-
varNames.length = 0;
344-
varExpressions.length = 0;
345-
};
346-
vars.forEach(function(v) {
347-
if(varNames.indexOf(v.name) >= 0) emitFunction();
348-
varNames.push(v.name);
349-
varExpressions.push(v.expression);
350-
});
351-
emitFunction();
352-
const start = block.range ? block.range[0] : -10;
353-
const end = block.range ? block.range[1] : (_source.size() + 1);
354-
if(varStartCode) source.insert(start + 0.5, varStartCode);
355-
if(varEndCode) source.insert(end + 0.5, "\n/* WEBPACK VAR INJECTION */" + varEndCode);
356-
}
357-
block.blocks.forEach(doBlock.bind(null, availableVars.concat(vars)));
353+
source(dependencyTemplates, outputOptions, requestShortener) {
354+
const hashDigest = this.getHashDigest();
355+
if(this._cachedSource && this._cachedSource.hash === hashDigest) {
356+
return this._cachedSource.source;
357+
}
358+
359+
if(!this._source) {
360+
return new RawSource("throw new Error('No source available');");
358361
}
359-
doBlock([], this);
362+
363+
const source = new ReplaceSource(this._source);
364+
this._cachedSource = {
365+
source: source,
366+
hash: hashDigest
367+
};
368+
const topLevelBlock = this;
369+
this.sourceBlock(this, [], dependencyTemplates, source, outputOptions, requestShortener, topLevelBlock);
360370
return new CachedSource(source);
361371
}
362372

0 commit comments

Comments
 (0)