Code coverage report for lib\LibraryTemplatePlugin.js

Statements: 61.36% (27 / 44)      Branches: 40% (10 / 25)      Functions: 100% (7 / 7)      Lines: 61.9% (26 / 42)      Ignored: none     

All files » lib\ » LibraryTemplatePlugin.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        1   1 3 1       1 3 3 3 3           1 14 14   1 1 14 14   2 2                         1 1     1   7 7           4 4 4                  
/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author Tobias Koppers @sokra
*/
var SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin");
 
function accessorToObjectAccess(accessor) {
	return accessor.map(function(a) {
		return "[" + JSON.stringify(a) + "]";
	}).join("");
}
 
function accessorAccess(base, accessor, joinWith) {
	accessor = [].concat(accessor);
	return accessor.map(function(a, idx) {
		a = base ? base + accessorToObjectAccess(accessor.slice(0, idx+1)) : accessor[0] + accessorToObjectAccess(accessor.slice(1, idx+1));
		Eif(idx === accessor.length - 1) return a;
		if(idx === 0 && typeof base === "undefined") return a + " = typeof " + a + " === \"object\" ? " + a + " : {}";
		return a + " = " + a + " || {}";
	}).join(joinWith || "; ");
}
 
function LibraryTemplatePlugin(name, target) {
	this.name = name;
	this.target = target;
}
module.exports = LibraryTemplatePlugin;
LibraryTemplatePlugin.prototype.apply = function(compiler) {
	compiler.plugin("this-compilation", function(compilation) {
		switch(this.target) {
		case "var":
			compilation.mainTemplate.apply(new SetVarMainTemplatePlugin("var " + accessorAccess(false, this.name)));
			break;
		case "assign":
			compilation.mainTemplate.apply(new SetVarMainTemplatePlugin(accessorAccess(undefined, this.name)));
			break;
		case "this":
		case "window":
		case "global":
			if(this.name)
				compilation.mainTemplate.apply(new SetVarMainTemplatePlugin(accessorAccess(this.target, this.name)));
			else
				compilation.mainTemplate.apply(new SetVarMainTemplatePlugin(this.target, true));
			break;
		case "commonjs":
			Eif(this.name)
				compilation.mainTemplate.apply(new SetVarMainTemplatePlugin(accessorAccess("exports", this.name)));
			else
				compilation.mainTemplate.apply(new SetVarMainTemplatePlugin("exports", true));
			break;
		case "commonjs2":
			compilation.mainTemplate.apply(new SetVarMainTemplatePlugin("module.exports"));
			break;
		case "amd":
			var AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin");
			compilation.mainTemplate.apply(new AmdMainTemplatePlugin(this.name));
			break;
		case "umd":
			var UmdMainTemplatePlugin = require("./UmdMainTemplatePlugin");
			compilation.mainTemplate.apply(new UmdMainTemplatePlugin(this.name));
			break;
		case "jsonp":
			var JsonpExportMainTemplatePlugin = require("./JsonpExportMainTemplatePlugin");
			compilation.mainTemplate.apply(new JsonpExportMainTemplatePlugin(this.name));
			break;
		default:
			throw new Error(this.target + " is not a valid Library target");
		}
	}.bind(this));
};