a zip code crypto-currency system good for red ONLY

NodeMainTemplatePlugin.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Template = require("../Template");
  7. module.exports = class NodeMainTemplatePlugin {
  8. constructor(asyncChunkLoading) {
  9. this.asyncChunkLoading = asyncChunkLoading;
  10. }
  11. apply(mainTemplate) {
  12. const asyncChunkLoading = this.asyncChunkLoading;
  13. mainTemplate.plugin("local-vars", function(source, chunk) {
  14. if(chunk.chunks.length > 0) {
  15. return this.asString([
  16. source,
  17. "",
  18. "// object to store loaded chunks",
  19. "// \"0\" means \"already loaded\"",
  20. "var installedChunks = {",
  21. this.indent(chunk.ids.map((id) => `${id}: 0`).join(",\n")),
  22. "};"
  23. ]);
  24. }
  25. return source;
  26. });
  27. mainTemplate.plugin("require-extensions", function(source, chunk) {
  28. if(chunk.chunks.length > 0) {
  29. return this.asString([
  30. source,
  31. "",
  32. "// uncatched error handler for webpack runtime",
  33. `${this.requireFn}.oe = function(err) {`,
  34. this.indent([
  35. "process.nextTick(function() {",
  36. this.indent("throw err; // catch this error by using System.import().catch()"),
  37. "});"
  38. ]),
  39. "};"
  40. ]);
  41. }
  42. return source;
  43. });
  44. mainTemplate.plugin("require-ensure", function(_, chunk, hash) {
  45. const chunkFilename = this.outputOptions.chunkFilename;
  46. const chunkMaps = chunk.getChunkMaps();
  47. const insertMoreModules = [
  48. "var moreModules = chunk.modules, chunkIds = chunk.ids;",
  49. "for(var moduleId in moreModules) {",
  50. this.indent(this.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")),
  51. "}"
  52. ];
  53. if(asyncChunkLoading) {
  54. return this.asString([
  55. "// \"0\" is the signal for \"already loaded\"",
  56. "if(installedChunks[chunkId] === 0)",
  57. this.indent([
  58. "return Promise.resolve();"
  59. ]),
  60. "// array of [resolve, reject, promise] means \"currently loading\"",
  61. "if(installedChunks[chunkId])",
  62. this.indent([
  63. "return installedChunks[chunkId][2];"
  64. ]),
  65. "// load the chunk and return promise to it",
  66. "var promise = new Promise(function(resolve, reject) {",
  67. this.indent([
  68. "installedChunks[chunkId] = [resolve, reject];",
  69. "var filename = __dirname + " + this.applyPluginsWaterfall("asset-path", JSON.stringify(`/${chunkFilename}`), {
  70. hash: `" + ${this.renderCurrentHashCode(hash)} + "`,
  71. hashWithLength: (length) => `" + ${this.renderCurrentHashCode(hash, length)} + "`,
  72. chunk: {
  73. id: "\" + chunkId + \"",
  74. hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
  75. hashWithLength: (length) => {
  76. const shortChunkHashMap = {};
  77. Object.keys(chunkMaps.hash).forEach((chunkId) => {
  78. if(typeof chunkMaps.hash[chunkId] === "string")
  79. shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(0, length);
  80. });
  81. return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
  82. },
  83. name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "`
  84. }
  85. }) + ";",
  86. "require('fs').readFile(filename, 'utf-8', function(err, content) {",
  87. this.indent([
  88. "if(err) return reject(err);",
  89. "var chunk = {};",
  90. "require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)" +
  91. "(chunk, require, require('path').dirname(filename), filename);"
  92. ].concat(insertMoreModules).concat([
  93. "var callbacks = [];",
  94. "for(var i = 0; i < chunkIds.length; i++) {",
  95. this.indent([
  96. "if(installedChunks[chunkIds[i]])",
  97. this.indent([
  98. "callbacks = callbacks.concat(installedChunks[chunkIds[i]][0]);"
  99. ]),
  100. "installedChunks[chunkIds[i]] = 0;"
  101. ]),
  102. "}",
  103. "for(i = 0; i < callbacks.length; i++)",
  104. this.indent("callbacks[i]();")
  105. ])),
  106. "});"
  107. ]),
  108. "});",
  109. "return installedChunks[chunkId][2] = promise;"
  110. ]);
  111. } else {
  112. const request = this.applyPluginsWaterfall("asset-path", JSON.stringify(`./${chunkFilename}`), {
  113. hash: `" + ${this.renderCurrentHashCode(hash)} + "`,
  114. hashWithLength: (length) => `" + ${this.renderCurrentHashCode(hash, length)} + "`,
  115. chunk: {
  116. id: "\" + chunkId + \"",
  117. hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
  118. hashWithLength: (length) => {
  119. const shortChunkHashMap = {};
  120. Object.keys(chunkMaps.hash).forEach((chunkId) => {
  121. if(typeof chunkMaps.hash[chunkId] === "string")
  122. shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(0, length);
  123. });
  124. return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
  125. },
  126. name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "`
  127. }
  128. });
  129. return this.asString([
  130. "// \"0\" is the signal for \"already loaded\"",
  131. "if(installedChunks[chunkId] !== 0) {",
  132. this.indent([
  133. `var chunk = require(${request});`
  134. ].concat(insertMoreModules).concat([
  135. "for(var i = 0; i < chunkIds.length; i++)",
  136. this.indent("installedChunks[chunkIds[i]] = 0;")
  137. ])),
  138. "}",
  139. "return Promise.resolve();"
  140. ]);
  141. }
  142. });
  143. mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) {
  144. const hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
  145. const hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
  146. const chunkMaps = chunk.getChunkMaps();
  147. const currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), {
  148. hash: `" + ${this.renderCurrentHashCode(hash)} + "`,
  149. hashWithLength: (length) => `" + ${this.renderCurrentHashCode(hash, length)} + "`,
  150. chunk: {
  151. id: "\" + chunkId + \"",
  152. hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
  153. hashWithLength: (length) => {
  154. const shortChunkHashMap = {};
  155. Object.keys(chunkMaps.hash).forEach((chunkId) => {
  156. if(typeof chunkMaps.hash[chunkId] === "string")
  157. shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(0, length);
  158. });
  159. return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
  160. },
  161. name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "`
  162. }
  163. });
  164. const currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), {
  165. hash: `" + ${this.renderCurrentHashCode(hash)} + "`,
  166. hashWithLength: (length) => `" + ${this.renderCurrentHashCode(hash, length)} + "`
  167. });
  168. return Template.getFunctionContent(asyncChunkLoading ? require("./NodeMainTemplateAsync.runtime.js") : require("./NodeMainTemplate.runtime.js"))
  169. .replace(/\$require\$/g, this.requireFn)
  170. .replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
  171. .replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename);
  172. });
  173. mainTemplate.plugin("hash", function(hash) {
  174. hash.update("node");
  175. hash.update("3");
  176. hash.update(this.outputOptions.filename + "");
  177. hash.update(this.outputOptions.chunkFilename + "");
  178. });
  179. }
  180. };