Code coverage report for lib\RecordIdsPlugin.js

Statements: 68.63% (70 / 102)      Branches: 60% (30 / 50)      Functions: 66.67% (14 / 21)      Lines: 73.49% (61 / 83)      Ignored: none     

All files » lib\ » RecordIdsPlugin.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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115        1   1 1 484 496 496 496 496 496 2988 2988     496 496 5 496 5 5 5                   1 250 250 252 252 252 252 252   250 250 250   496 496 496 496 496 496 731 731 731 731 250       496 496 5 496 5 5 5 5 5 5 5 5 5 5     5 5 5                 5 5               5         5                        
/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author Tobias Koppers @sokra
*/
function RecordIdsPlugin() {
}
module.exports = RecordIdsPlugin;
RecordIdsPlugin.prototype.apply = function(compiler) {
	compiler.plugin("compilation", function(compilation) {
		compilation.plugin("record-modules", function(modules, records) {
			records.nextFreeModuleId = compilation.nextFreeModuleId;
			if(!records.modules) records.modules = {};
			if(!records.modules.byIdentifier) records.modules.byIdentifier = {};
			modules.forEach(function(module) {
				var identifier = module.identifier();
				records.modules.byIdentifier[identifier] = module.id;
			});
		});
		compilation.plugin("revive-modules", function(modules, records) {
			if(records.nextFreeModuleId)
				compilation.nextFreeModuleId = records.nextFreeModuleId;
			if(!records.modules || !records.modules.byIdentifier) return;
			var usedIds = {0: true};
			modules.forEach(function(module) {
				Eif(module.id !== null) return;
				var identifier = module.identifier();
				var id = records.modules.byIdentifier[identifier];
				if(id === undefined) return;
				if(usedIds[id]) return;
				usedIds[id] = true;
				module.id = id;
			});
		});
		
		function getDepBlockIdent(block) {
			var ident = [];
			while(block.parent) {
				var p = block.parent;
				var idx = p.blocks.indexOf(block);
				var l = p.blocks.length - 1;
				ident.unshift(idx + "/" + l);
				block = block.parent;
			}
			Iif(!block.identifier) return null;
			ident.unshift(block.identifier());
			return ident.join(":");
		}
		compilation.plugin("record-chunks", function(chunks, records) {
			records.nextFreeChunkId = compilation.nextFreeChunkId;
			if(!records.chunks) records.chunks = {};
			if(!records.chunks.byName) records.chunks.byName = {};
			if(!records.chunks.byBlocks) records.chunks.byBlocks = {};
			chunks.forEach(function(chunk) {
				var name = chunk.name;
				var blockIdents = chunk.blocks.map(getDepBlockIdent).filter(Boolean);
				if(name) records.chunks.byName[name] = chunk.id;
				blockIdents.forEach(function(blockIdent) {
					records.chunks.byBlocks[blockIdent] = chunk.id;
				});
			});
		});
		compilation.plugin("revive-chunks", function(chunks, records) {
			if(records.nextFreeChunkId)
				compilation.nextFreeChunkId = records.nextFreeChunkId;
			if(!records.chunks) return;
			var usedIds = {};
			Eif(records.chunks.byName) {
				chunks.forEach(function(chunk) {
					Iif(chunk.id !== null) return;
					Iif(!chunk.name) return;
					var id = records.chunks.byName[chunk.name];
					Iif(id === undefined) return;
					Iif(usedIds[id]) return;
					usedIds[id] = true;
					chunk.id = id;
				});
			}
			Eif(records.chunks.byBlocks) {
				var argumentedChunks = chunks.filter(function(chunk) {
					return chunk.id === null
				}).map(function(chunk) {
					return {
						chunk: chunk,
						blockIdents: chunk.blocks.map(getDepBlockIdent).filter(Boolean)
					}
				}).filter(function(arg) {
					return arg.blockIdents.length > 0;
				});
				var blockIdentsCount = {};
				argumentedChunks.forEach(function(arg, idx) {
					arg.blockIdents.forEach(function(blockIdent) {
						var id = records.chunks.byBlocks[blockIdent]
						if(!id) return;
						var accessor = id + ":" + idx;
						blockIdentsCount[accessor] = (blockIdentsCount[accessor] || 0) + 1;
					});
				});
				blockIdentsCount = Object.keys(blockIdentsCount).map(function(accessor) {
					return [blockIdentsCount[accessor]].concat(accessor.split(":").map(Number));
				}).sort(function(a, b) {
					return b[0] - a[0];
				})
				blockIdentsCount.forEach(function(arg) {
					var id = arg[1];
					if(usedIds[id]) return;
					var idx = arg[2];
					var chunk = argumentedChunks[idx].chunk;
					if(chunk.id !== null) return;
					usedIds[id] = true;
					chunk.id = id;
				});
			}
		});
	});
};