Code coverage report for lib\Template.js

Statements: 93.33% (70 / 75)      Branches: 80.56% (29 / 36)      Functions: 100% (10 / 10)      Lines: 97.06% (66 / 68)      Ignored: none     

All files » lib\ » Template.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100        1 1   1 1984 1984   1   1 1 1 1 1 1 1   1   1 6439 752   5687 5687 5673       1 502 502   502 502 502     1 3963 2959   1004     1 734 734 734       734 734 734 730   734 2999 2999 734 734   646 646 646 646 2876   646 3288 3288 3288 3288 2876 2876     646 646     88 88 123 123 123   88   734     1 93  
/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author Tobias Koppers @sokra
*/
var Tapable = require("tapable");
var ConcatSource = require("webpack-core/lib/ConcatSource");
 
function Template(outputOptions) {
	Tapable.call(this);
	this.outputOptions = outputOptions || {};
}
module.exports = Template;
 
Template.REGEXP_HASH = /\[hash\]/gi;
Template.REGEXP_CHUNKHASH = /\[chunkhash\]/gi;
Template.REGEXP_NAME = /\[name\]/gi;
Template.REGEXP_ID = /\[id\]/gi;
Template.REGEXP_FILE = /\[file\]/gi;
Template.REGEXP_QUERY = /\[query\]/gi;
Template.REGEXP_FILEBASE = /\[filebase\]/gi;
 
Template.prototype = Object.create(Tapable.prototype);
 
Template.prototype.indent = function indent(str) {
	if(Array.isArray(str)) {
		return str.map(indent).join("\n");
	} else {
		str = str.trimRight();
		if(!str) return "";
		return (str[0] === "\n" ? "" : "\t") + str.replace(/\n([^\n])/g, "\n\t$1");
	}
};
 
Template.prototype.prefix = function(str, prefix) {
	Eif(Array.isArray(str)) {
		str = str.join("\n");
	}
	str = str.trim();
	Iif(!str) return "";
	return (str[0] === "\n" ? "" : prefix) + str.replace(/\n([^\n])/g, "\n" + prefix + "$1");
};
 
Template.prototype.asString = function(str) {
	if(Array.isArray(str)) {
		return str.join("\n");
	}
	return str;
};
 
Template.prototype.renderChunkModules = function(chunk, moduleTemplate, dependencyTemplates, prefix) {
	if(!prefix) prefix = "";
	var source = new ConcatSource();
	Iif(chunk.modules.length === 0) {
		source.add("[]");
		return source;
	}
	var maxId = chunk.modules[chunk.modules.length-1].id;
	var minId = chunk.modules[0].id;
	if(minId < 16 + (""+minId).length) {
		minId = 0;
	}
	var objectOverhead = chunk.modules.map(function(module) {
		return (module.id + "").length + 2;
	}).reduce(function(a, b) { return a + b }, -1);
	var arrayOverhead = minId === 0 ? maxId : 16 + (""+minId).length + maxId;
	if(arrayOverhead < objectOverhead) {
		// Render a sparse array
		Iif(minId !== 0) source.add("Array(" + minId + ").concat(");
		source.add("[\n");
		var modules = {};
		chunk.modules.forEach(function(module) {
			modules[module.id] = module;
		});
		for(var idx = minId; idx <= maxId; idx++) {
			var module = modules[idx];
			if(idx !== minId) source.add(",\n");
			source.add("/* " + idx + " */");
			if(module) {
				source.add("\n");
				source.add(moduleTemplate.render(module, dependencyTemplates, chunk));
			}
		}
		source.add("\n" + prefix + "]");
		Iif(minId !== 0) source.add(")");
	} else {
		// Render an object
		source.add("{\n");
		chunk.modules.forEach(function(module, idx) {
			if(idx !== 0) source.add(",\n");
			source.add("\n/***/ " + module.id + ":\n");
			source.add(moduleTemplate.render(module, dependencyTemplates, chunk));
		});
		source.add("\n\n" + prefix + "}");
	}
	return source;
};
 
Template.getFunctionContent = function(fn) {
	return fn.toString().replace(/^function\s?\(\)\s?\{\n?|\n?\}$/g, "").replace(/^\t/mg, "");
};