a zip code crypto-currency system good for red ONLY

RecordIdsPlugin.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const identifierUtils = require("./util/identifier");
  7. class RecordIdsPlugin {
  8. apply(compiler) {
  9. compiler.plugin("compilation", compilation => {
  10. compilation.plugin("record-modules", (modules, records) => {
  11. if(!records.modules) records.modules = {};
  12. if(!records.modules.byIdentifier) records.modules.byIdentifier = {};
  13. if(!records.modules.usedIds) records.modules.usedIds = {};
  14. modules.forEach(function(module) {
  15. if(!module.portableId) module.portableId = identifierUtils.makePathsRelative(compiler.context, module.identifier());
  16. const identifier = module.portableId;
  17. records.modules.byIdentifier[identifier] = module.id;
  18. records.modules.usedIds[module.id] = module.id;
  19. });
  20. });
  21. compilation.plugin("revive-modules", (modules, records) => {
  22. if(!records.modules) return;
  23. if(records.modules.byIdentifier) {
  24. const usedIds = {};
  25. modules.forEach(function(module) {
  26. if(module.id !== null) return;
  27. if(!module.portableId) module.portableId = identifierUtils.makePathsRelative(compiler.context, module.identifier());
  28. const identifier = module.portableId;
  29. const id = records.modules.byIdentifier[identifier];
  30. if(id === undefined) return;
  31. if(usedIds[id]) return;
  32. usedIds[id] = true;
  33. module.id = id;
  34. });
  35. }
  36. compilation.usedModuleIds = records.modules.usedIds;
  37. });
  38. function getDepBlockIdent(chunk, block) {
  39. const ident = [];
  40. if(block.chunks.length > 1)
  41. ident.push(block.chunks.indexOf(chunk));
  42. while(block.parent) {
  43. const p = block.parent;
  44. const idx = p.blocks.indexOf(block);
  45. const l = p.blocks.length - 1;
  46. ident.push(`${idx}/${l}`);
  47. block = block.parent;
  48. }
  49. if(!block.identifier) return null;
  50. ident.push(identifierUtils.makePathsRelative(compiler.context, block.identifier()));
  51. return ident.reverse().join(":");
  52. }
  53. compilation.plugin("record-chunks", (chunks, records) => {
  54. records.nextFreeChunkId = compilation.nextFreeChunkId;
  55. if(!records.chunks) records.chunks = {};
  56. if(!records.chunks.byName) records.chunks.byName = {};
  57. if(!records.chunks.byBlocks) records.chunks.byBlocks = {};
  58. records.chunks.usedIds = {};
  59. chunks.forEach(chunk => {
  60. const name = chunk.name;
  61. const blockIdents = chunk.blocks.map(getDepBlockIdent.bind(null, chunk)).filter(Boolean);
  62. if(name) records.chunks.byName[name] = chunk.id;
  63. blockIdents.forEach((blockIdent) => {
  64. records.chunks.byBlocks[blockIdent] = chunk.id;
  65. });
  66. records.chunks.usedIds[chunk.id] = chunk.id;
  67. });
  68. });
  69. compilation.plugin("revive-chunks", (chunks, records) => {
  70. if(!records.chunks) return;
  71. const usedIds = {};
  72. if(records.chunks.byName) {
  73. chunks.forEach(function(chunk) {
  74. if(chunk.id !== null) return;
  75. if(!chunk.name) return;
  76. const id = records.chunks.byName[chunk.name];
  77. if(id === undefined) return;
  78. if(usedIds[id]) return;
  79. usedIds[id] = true;
  80. chunk.id = id;
  81. });
  82. }
  83. if(records.chunks.byBlocks) {
  84. const argumentedChunks = chunks.filter(chunk => chunk.id === null).map(chunk => ({
  85. chunk,
  86. blockIdents: chunk.blocks.map(getDepBlockIdent.bind(null, chunk)).filter(Boolean)
  87. })).filter(arg => arg.blockIdents.length > 0);
  88. let blockIdentsCount = {};
  89. argumentedChunks.forEach((arg, idx) => {
  90. arg.blockIdents.forEach(blockIdent => {
  91. const id = records.chunks.byBlocks[blockIdent];
  92. if(typeof id !== "number") return;
  93. const accessor = `${id}:${idx}`;
  94. blockIdentsCount[accessor] = (blockIdentsCount[accessor] || 0) + 1;
  95. });
  96. });
  97. blockIdentsCount = Object.keys(blockIdentsCount).map(accessor => [blockIdentsCount[accessor]].concat(accessor.split(":").map(Number))).sort((a, b) => b[0] - a[0]);
  98. blockIdentsCount.forEach(function(arg) {
  99. const id = arg[1];
  100. if(usedIds[id]) return;
  101. const idx = arg[2];
  102. const chunk = argumentedChunks[idx].chunk;
  103. if(chunk.id !== null) return;
  104. usedIds[id] = true;
  105. chunk.id = id;
  106. });
  107. }
  108. compilation.usedChunkIds = records.chunks.usedIds;
  109. });
  110. });
  111. }
  112. }
  113. module.exports = RecordIdsPlugin;