UI for Zipcoin Blue

JsonpMainTemplatePlugin.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. class JsonpMainTemplatePlugin {
  8. apply(mainTemplate) {
  9. mainTemplate.plugin("local-vars", function(source, chunk) {
  10. if(chunk.chunks.length > 0) {
  11. return this.asString([
  12. source,
  13. "",
  14. "// objects to store loaded and loading chunks",
  15. "var installedChunks = {",
  16. this.indent(
  17. chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n")
  18. ),
  19. "};"
  20. ]);
  21. }
  22. return source;
  23. });
  24. mainTemplate.plugin("jsonp-script", function(_, chunk, hash) {
  25. const chunkFilename = this.outputOptions.chunkFilename;
  26. const chunkMaps = chunk.getChunkMaps();
  27. const crossOriginLoading = this.outputOptions.crossOriginLoading;
  28. const chunkLoadTimeout = this.outputOptions.chunkLoadTimeout;
  29. const scriptSrcPath = this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
  30. hash: `" + ${this.renderCurrentHashCode(hash)} + "`,
  31. hashWithLength: length => `" + ${this.renderCurrentHashCode(hash, length)} + "`,
  32. chunk: {
  33. id: "\" + chunkId + \"",
  34. hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`,
  35. hashWithLength(length) {
  36. const shortChunkHashMap = Object.create(null);
  37. Object.keys(chunkMaps.hash).forEach(chunkId => {
  38. if(typeof chunkMaps.hash[chunkId] === "string")
  39. shortChunkHashMap[chunkId] = chunkMaps.hash[chunkId].substr(0, length);
  40. });
  41. return `" + ${JSON.stringify(shortChunkHashMap)}[chunkId] + "`;
  42. },
  43. name: `" + (${JSON.stringify(chunkMaps.name)}[chunkId]||chunkId) + "`
  44. }
  45. });
  46. return this.asString([
  47. "var script = document.createElement('script');",
  48. "script.type = 'text/javascript';",
  49. "script.charset = 'utf-8';",
  50. "script.async = true;",
  51. `script.timeout = ${chunkLoadTimeout};`,
  52. crossOriginLoading ? `script.crossOrigin = ${JSON.stringify(crossOriginLoading)};` : "",
  53. `if (${this.requireFn}.nc) {`,
  54. this.indent(`script.setAttribute("nonce", ${this.requireFn}.nc);`),
  55. "}",
  56. `script.src = ${this.requireFn}.p + ${scriptSrcPath};`,
  57. `var timeout = setTimeout(onScriptComplete, ${chunkLoadTimeout});`,
  58. "script.onerror = script.onload = onScriptComplete;",
  59. "function onScriptComplete() {",
  60. this.indent([
  61. "// avoid mem leaks in IE.",
  62. "script.onerror = script.onload = null;",
  63. "clearTimeout(timeout);",
  64. "var chunk = installedChunks[chunkId];",
  65. "if(chunk !== 0) {",
  66. this.indent([
  67. "if(chunk) {",
  68. this.indent("chunk[1](new Error('Loading chunk ' + chunkId + ' failed.'));"),
  69. "}",
  70. "installedChunks[chunkId] = undefined;"
  71. ]),
  72. "}"
  73. ]),
  74. "};",
  75. ]);
  76. });
  77. mainTemplate.plugin("require-ensure", function(_, chunk, hash) {
  78. return this.asString([
  79. "var installedChunkData = installedChunks[chunkId];",
  80. "if(installedChunkData === 0) {",
  81. this.indent([
  82. "return new Promise(function(resolve) { resolve(); });"
  83. ]),
  84. "}",
  85. "",
  86. "// a Promise means \"currently loading\".",
  87. "if(installedChunkData) {",
  88. this.indent([
  89. "return installedChunkData[2];"
  90. ]),
  91. "}",
  92. "",
  93. "// setup Promise in chunk cache",
  94. "var promise = new Promise(function(resolve, reject) {",
  95. this.indent([
  96. "installedChunkData = installedChunks[chunkId] = [resolve, reject];"
  97. ]),
  98. "});",
  99. "installedChunkData[2] = promise;",
  100. "",
  101. "// start chunk loading",
  102. "var head = document.getElementsByTagName('head')[0];",
  103. this.applyPluginsWaterfall("jsonp-script", "", chunk, hash),
  104. "head.appendChild(script);",
  105. "",
  106. "return promise;"
  107. ]);
  108. });
  109. mainTemplate.plugin("require-extensions", function(source, chunk) {
  110. if(chunk.chunks.length === 0) return source;
  111. return this.asString([
  112. source,
  113. "",
  114. "// on error function for async loading",
  115. `${this.requireFn}.oe = function(err) { console.error(err); throw err; };`
  116. ]);
  117. });
  118. mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
  119. if(chunk.chunks.length > 0) {
  120. var jsonpFunction = this.outputOptions.jsonpFunction;
  121. return this.asString([
  122. source,
  123. "",
  124. "// install a JSONP callback for chunk loading",
  125. `var parentJsonpFunction = window[${JSON.stringify(jsonpFunction)}];`,
  126. `window[${JSON.stringify(jsonpFunction)}] = function webpackJsonpCallback(chunkIds, moreModules, executeModules) {`,
  127. this.indent([
  128. "// add \"moreModules\" to the modules object,",
  129. "// then flag all \"chunkIds\" as loaded and fire callback",
  130. "var moduleId, chunkId, i = 0, resolves = [], result;",
  131. "for(;i < chunkIds.length; i++) {",
  132. this.indent([
  133. "chunkId = chunkIds[i];",
  134. "if(installedChunks[chunkId]) {",
  135. this.indent("resolves.push(installedChunks[chunkId][0]);"),
  136. "}",
  137. "installedChunks[chunkId] = 0;"
  138. ]),
  139. "}",
  140. "for(moduleId in moreModules) {",
  141. this.indent([
  142. "if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {",
  143. this.indent(this.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")),
  144. "}"
  145. ]),
  146. "}",
  147. "if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules, executeModules);",
  148. "while(resolves.length) {",
  149. this.indent("resolves.shift()();"),
  150. "}",
  151. this.entryPointInChildren(chunk) ? [
  152. "if(executeModules) {",
  153. this.indent([
  154. "for(i=0; i < executeModules.length; i++) {",
  155. this.indent(`result = ${this.requireFn}(${this.requireFn}.s = executeModules[i]);`),
  156. "}"
  157. ]),
  158. "}",
  159. "return result;",
  160. ] : ""
  161. ]),
  162. "};"
  163. ]);
  164. }
  165. return source;
  166. });
  167. mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) {
  168. const hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
  169. const hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
  170. const crossOriginLoading = this.outputOptions.crossOriginLoading;
  171. const hotUpdateFunction = this.outputOptions.hotUpdateFunction;
  172. const currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), {
  173. hash: `" + ${this.renderCurrentHashCode(hash)} + "`,
  174. hashWithLength: length => `" + ${this.renderCurrentHashCode(hash, length)} + "`,
  175. chunk: {
  176. id: "\" + chunkId + \""
  177. }
  178. });
  179. const currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), {
  180. hash: `" + ${this.renderCurrentHashCode(hash)} + "`,
  181. hashWithLength: length => `" + ${this.renderCurrentHashCode(hash, length)} + "`
  182. });
  183. const runtimeSource = Template.getFunctionContent(require("./JsonpMainTemplate.runtime.js"))
  184. .replace(/\/\/\$semicolon/g, ";")
  185. .replace(/\$require\$/g, this.requireFn)
  186. .replace(/\$crossOriginLoading\$/g, crossOriginLoading ? `script.crossOrigin = ${JSON.stringify(crossOriginLoading)}` : "")
  187. .replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
  188. .replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
  189. .replace(/\$hash\$/g, JSON.stringify(hash));
  190. return `${source}
  191. function hotDisposeChunk(chunkId) {
  192. delete installedChunks[chunkId];
  193. }
  194. var parentHotUpdateCallback = this[${JSON.stringify(hotUpdateFunction)}];
  195. this[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
  196. });
  197. mainTemplate.plugin("hash", function(hash) {
  198. hash.update("jsonp");
  199. hash.update("4");
  200. hash.update(`${this.outputOptions.filename}`);
  201. hash.update(`${this.outputOptions.chunkFilename}`);
  202. hash.update(`${this.outputOptions.jsonpFunction}`);
  203. hash.update(`${this.outputOptions.hotUpdateFunction}`);
  204. });
  205. }
  206. }
  207. module.exports = JsonpMainTemplatePlugin;