static.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define('buildTools', [
  6. ], function () {
  7. 'use strict';
  8. var storage = window.localStorage,
  9. storeName = 'buildDisabled';
  10. return {
  11. isEnabled: storage.getItem(storeName) === null,
  12. /**
  13. * Removes base url from the provided string
  14. *
  15. * @param {String} url - Url to be processed.
  16. * @param {Object} config - RequiereJs config object.
  17. * @returns {String} String without base url.
  18. */
  19. removeBaseUrl: function (url, config) {
  20. var urlParts,
  21. baseUrlParts,
  22. baseUrl = config.baseUrl || '',
  23. index = url.indexOf(baseUrl);
  24. if (~index) {
  25. url = url.substring(baseUrl.length - index);
  26. } else {
  27. baseUrlParts = baseUrl.split('/');
  28. baseUrlParts = baseUrlParts.slice(0, -5); // slice area/vendor/theme/locale/empty chunk
  29. baseUrl = baseUrlParts.join('/');
  30. url = url.substring(baseUrl.length);
  31. urlParts = url.split('/');
  32. urlParts = urlParts.slice(5); // slice empty chunk/area/vendor/theme/locale/
  33. url = urlParts.join('/');
  34. }
  35. return url;
  36. },
  37. /**
  38. * Enables build usage.
  39. */
  40. on: function () {
  41. storage.removeItem(storeName);
  42. location.reload();
  43. },
  44. /**
  45. * Disables build usage.
  46. */
  47. off: function () {
  48. storage.setItem(storeName, 'true');
  49. location.reload();
  50. }
  51. };
  52. });
  53. /**
  54. * Module responsible for collecting statistics
  55. * data regarding modules that have been loader via bundle.
  56. */
  57. define('statistician', [
  58. ], function () {
  59. 'use strict';
  60. var storage = window.localStorage,
  61. stringify = JSON.stringify.bind(JSON);
  62. /**
  63. * Removes duplicated entries of array, returning new one.
  64. *
  65. * @param {Array} arr
  66. * @returns {Array}
  67. */
  68. function uniq(arr) {
  69. return arr.filter(function (entry, i) {
  70. return arr.indexOf(entry) >= i;
  71. });
  72. }
  73. /**
  74. * Takes first array passed, removes all
  75. * entries which further arrays contain.
  76. *
  77. * @returns {Array} Modified array
  78. */
  79. function difference() {
  80. var args = Array.prototype.slice.call(arguments),
  81. target = args.splice(0, 1)[0];
  82. return target.filter(function (entry) {
  83. return !args.some(function (arr) {
  84. return !!~arr.indexOf(entry);
  85. });
  86. });
  87. }
  88. /**
  89. * Stringifies 'data' parameter and sets it under 'key' namespace to localStorage.
  90. *
  91. * @param {*} data
  92. * @param {String} key
  93. */
  94. function set(data, key) {
  95. storage.setItem(key, stringify(data));
  96. }
  97. /**
  98. * Gets item from localStorage by 'key' parameter, JSON.parse's it if defined.
  99. * Else, returns empty array.
  100. *
  101. * @param {String} key
  102. * @returns {Array}
  103. */
  104. function getModules(key) {
  105. var plain = storage.getItem(key);
  106. return plain ? JSON.parse(plain) : [];
  107. }
  108. /**
  109. * Concats 'modules' array with one that was previously stored by 'key' parameter
  110. * in localStorage, removes duplicated entries from resulting array and writes
  111. * it to 'key' namespace of localStorage via 'set' function.
  112. *
  113. * @param {Array} modules
  114. * @param {String} key
  115. */
  116. function storeModules(modules, key) {
  117. var old = getModules(key);
  118. set(uniq(old.concat(modules)), key);
  119. }
  120. /**
  121. * Creates Blob, writes passed data to it, then creates ObjectURL string
  122. * with blob data. In parallel, creates 'a' element, writes resulting ObjectURL
  123. * to it's href property and fileName parameter as it's download prop.
  124. * Clicks on 'a' and cleans up file data.
  125. *
  126. * @param {String} fileName
  127. * @param {Object} data
  128. */
  129. function upload(fileName, data) {
  130. var a = document.createElement('a'),
  131. blob,
  132. url;
  133. a.style = 'display: none';
  134. document.body.appendChild(a);
  135. blob = new Blob([JSON.stringify(data)], {
  136. type: 'octet/stream'
  137. });
  138. url = window.URL.createObjectURL(blob);
  139. a.href = url;
  140. a.download = fileName;
  141. a.click();
  142. window.URL.revokeObjectURL(url);
  143. }
  144. return {
  145. /**
  146. * Stores keys of 'modules' object to localStorage under 'all' namespace.
  147. *
  148. * @param {Object} modules
  149. */
  150. collect: function (modules) {
  151. storeModules(Object.keys(modules), 'all');
  152. },
  153. /**
  154. * Wraps 'module' in empty array and stores it to localStorage by 'used' namespace.
  155. *
  156. * @param {String} module
  157. */
  158. utilize: function (module) {
  159. storeModules([module], 'used');
  160. },
  161. /**
  162. * Returns modules, stores under 'all' namespace in localStorage via
  163. * getModules function.
  164. *
  165. * @return {Array}
  166. */
  167. getAll: function () {
  168. return getModules('all');
  169. },
  170. /**
  171. * Returns modules, stores under 'used' namespace in localStorage via
  172. * getModules function.
  173. *
  174. * @return {Array}
  175. */
  176. getUsed: function () {
  177. return getModules('used');
  178. },
  179. /**
  180. * Returns difference between arrays stored under 'all' and 'used'.
  181. *
  182. * @return {Array}
  183. */
  184. getUnused: function () {
  185. var all = getModules('all'),
  186. used = getModules('used');
  187. return difference(all, used);
  188. },
  189. /**
  190. * Clears "all" and "used" namespaces of localStorage.
  191. */
  192. clear: function () {
  193. storage.removeItem('all');
  194. storage.removeItem('used');
  195. },
  196. /**
  197. * Create blob containing stats data and download it
  198. */
  199. export: function () {
  200. upload('Magento Bundle Statistics', {
  201. used: this.getUsed(),
  202. unused: this.getUnused(),
  203. all: this.getAll()
  204. });
  205. }
  206. };
  207. });
  208. /**
  209. * Extension of a requirejs 'load' method
  210. * to load files from a build object.
  211. */
  212. define('jsbuild', [
  213. 'module',
  214. 'buildTools',
  215. 'statistician'
  216. ], function (module, tools, statistician) {
  217. 'use strict';
  218. var build = module.config() || {};
  219. if (!tools.isEnabled) {
  220. return;
  221. }
  222. require._load = require.load;
  223. statistician.collect(build);
  224. /**
  225. * Overrides requirejs main loading method to provide
  226. * support of scripts initialization from a bundle object.
  227. *
  228. * @param {Object} context
  229. * @param {String} moduleName
  230. * @param {String} url
  231. */
  232. require.load = function (context, moduleName, url) {
  233. var relative = tools.removeBaseUrl(url, context.config),
  234. data = build[relative];
  235. if (data) {
  236. statistician.utilize(relative);
  237. new Function(data)();
  238. context.completeLoad(moduleName);
  239. } else {
  240. require._load.apply(require, arguments);
  241. }
  242. };
  243. });
  244. /**
  245. * Extension of a requirejs text plugin
  246. * to load files from a build object.
  247. */
  248. define('text', [
  249. 'module',
  250. 'buildTools',
  251. 'mage/requirejs/text'
  252. ], function (module, tools, text) {
  253. 'use strict';
  254. var build = module.config() || {};
  255. if (!tools.isEnabled) {
  256. return text;
  257. }
  258. text._load = text.load;
  259. /**
  260. * Overrides load method of a 'text' plugin to provide support
  261. * of loading files from a build object.
  262. *
  263. * @param {String} name
  264. * @param {Function} req
  265. * @param {Function} onLoad
  266. * @param {Object} config
  267. */
  268. text.load = function (name, req, onLoad, config) {
  269. var url = req.toUrl(name),
  270. relative = tools.removeBaseUrl(url, config),
  271. data = build[relative];
  272. data ?
  273. onLoad(data) :
  274. text._load.apply(text, arguments);
  275. };
  276. return text;
  277. });