2013-03-26 23:54:41 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2014-06-03 03:23:53 +08:00
|
|
|
var Tapable = require("tapable");
|
2013-12-13 19:58:10 +08:00
|
|
|
var ConcatSource = require("webpack-core/lib/ConcatSource");
|
|
|
|
|
2013-03-26 23:54:41 +08:00
|
|
|
function Template(outputOptions) {
|
2014-06-03 03:23:53 +08:00
|
|
|
Tapable.call(this);
|
2013-03-26 23:54:41 +08:00
|
|
|
this.outputOptions = outputOptions || {};
|
|
|
|
}
|
|
|
|
module.exports = Template;
|
|
|
|
|
2014-08-22 19:51:24 +08:00
|
|
|
Template.getFunctionContent = function(fn) {
|
|
|
|
return fn.toString().replace(/^function\s?\(\)\s?\{\n?|\n?\}$/g, "").replace(/^\t/mg, "");
|
|
|
|
};
|
|
|
|
|
|
|
|
Template.toIdentifier = function(str) {
|
2015-04-24 05:55:50 +08:00
|
|
|
return str.replace(/^[^a-zA-Z$_]/, "_").replace(/[^a-zA-Z0-9$_]/g, "_");
|
2014-08-22 19:51:24 +08:00
|
|
|
};
|
2013-03-26 23:54:41 +08:00
|
|
|
|
2014-06-03 03:23:53 +08:00
|
|
|
Template.prototype = Object.create(Tapable.prototype);
|
|
|
|
|
2013-03-26 23:54:41 +08:00
|
|
|
Template.prototype.indent = function indent(str) {
|
|
|
|
if(Array.isArray(str)) {
|
|
|
|
return str.map(indent).join("\n");
|
|
|
|
} else {
|
2014-06-03 03:23:53 +08:00
|
|
|
str = str.trimRight();
|
|
|
|
if(!str) return "";
|
|
|
|
return (str[0] === "\n" ? "" : "\t") + str.replace(/\n([^\n])/g, "\n\t$1");
|
2013-03-26 23:54:41 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Template.prototype.prefix = function(str, prefix) {
|
|
|
|
if(Array.isArray(str)) {
|
|
|
|
str = str.join("\n");
|
|
|
|
}
|
2014-06-03 03:23:53 +08:00
|
|
|
str = str.trim();
|
|
|
|
if(!str) return "";
|
|
|
|
return (str[0] === "\n" ? "" : prefix) + str.replace(/\n([^\n])/g, "\n" + prefix + "$1");
|
2013-03-26 23:54:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Template.prototype.asString = function(str) {
|
|
|
|
if(Array.isArray(str)) {
|
|
|
|
return str.join("\n");
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
};
|
2013-12-13 19:58:10 +08:00
|
|
|
|
|
|
|
Template.prototype.renderChunkModules = function(chunk, moduleTemplate, dependencyTemplates, prefix) {
|
|
|
|
if(!prefix) prefix = "";
|
|
|
|
var source = new ConcatSource();
|
2013-12-19 18:46:45 +08:00
|
|
|
if(chunk.modules.length === 0) {
|
|
|
|
source.add("[]");
|
|
|
|
return source;
|
|
|
|
}
|
2014-08-22 19:51:24 +08:00
|
|
|
var maxId = chunk.modules[chunk.modules.length - 1].id;
|
2013-12-13 19:58:10 +08:00
|
|
|
var minId = chunk.modules[0].id;
|
2014-08-22 19:51:24 +08:00
|
|
|
if(minId < 16 + ("" + minId).length) {
|
2013-12-13 19:58:10 +08:00
|
|
|
minId = 0;
|
|
|
|
}
|
|
|
|
var objectOverhead = chunk.modules.map(function(module) {
|
|
|
|
return (module.id + "").length + 2;
|
2014-08-22 19:51:24 +08:00
|
|
|
}).reduce(function(a, b) { return a + b; }, -1);
|
|
|
|
var arrayOverhead = minId === 0 ? maxId : 16 + ("" + minId).length + maxId;
|
2013-12-13 19:58:10 +08:00
|
|
|
if(arrayOverhead < objectOverhead) {
|
|
|
|
// Render a sparse array
|
|
|
|
if(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];
|
2014-06-25 00:53:32 +08:00
|
|
|
if(idx !== minId) source.add(",\n");
|
2013-12-13 19:58:10 +08:00
|
|
|
source.add("/* " + idx + " */");
|
|
|
|
if(module) {
|
|
|
|
source.add("\n");
|
|
|
|
source.add(moduleTemplate.render(module, dependencyTemplates, chunk));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
source.add("\n" + prefix + "]");
|
|
|
|
if(minId !== 0) source.add(")");
|
|
|
|
} else {
|
|
|
|
// Render an object
|
|
|
|
source.add("{\n");
|
|
|
|
chunk.modules.forEach(function(module, idx) {
|
2014-06-25 00:53:32 +08:00
|
|
|
if(idx !== 0) source.add(",\n");
|
2013-12-13 19:58:10 +08:00
|
|
|
source.add("\n/***/ " + module.id + ":\n");
|
|
|
|
source.add(moduleTemplate.render(module, dependencyTemplates, chunk));
|
|
|
|
});
|
|
|
|
source.add("\n\n" + prefix + "}");
|
|
|
|
}
|
|
|
|
return source;
|
|
|
|
};
|