123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- define('buildTools', [
- ], function () {
- 'use strict';
- var storage = window.localStorage,
- storeName = 'buildDisabled';
- return {
- isEnabled: storage.getItem(storeName) === null,
- /**
- * Removes base url from the provided string
- *
- * @param {String} url - Url to be processed.
- * @param {Object} config - RequiereJs config object.
- * @returns {String} String without base url.
- */
- removeBaseUrl: function (url, config) {
- var urlParts,
- baseUrlParts,
- baseUrl = config.baseUrl || '',
- index = url.indexOf(baseUrl);
- if (~index) {
- url = url.substring(baseUrl.length - index);
- } else {
- baseUrlParts = baseUrl.split('/');
- baseUrlParts = baseUrlParts.slice(0, -5); // slice area/vendor/theme/locale/empty chunk
- baseUrl = baseUrlParts.join('/');
- url = url.substring(baseUrl.length);
- urlParts = url.split('/');
- urlParts = urlParts.slice(5); // slice empty chunk/area/vendor/theme/locale/
- url = urlParts.join('/');
- }
- return url;
- },
- /**
- * Enables build usage.
- */
- on: function () {
- storage.removeItem(storeName);
- location.reload();
- },
- /**
- * Disables build usage.
- */
- off: function () {
- storage.setItem(storeName, 'true');
- location.reload();
- }
- };
- });
- /**
- * Module responsible for collecting statistics
- * data regarding modules that have been loader via bundle.
- */
- define('statistician', [
- ], function () {
- 'use strict';
- var storage = window.localStorage,
- stringify = JSON.stringify.bind(JSON);
- /**
- * Removes duplicated entries of array, returning new one.
- *
- * @param {Array} arr
- * @returns {Array}
- */
- function uniq(arr) {
- return arr.filter(function (entry, i) {
- return arr.indexOf(entry) >= i;
- });
- }
- /**
- * Takes first array passed, removes all
- * entries which further arrays contain.
- *
- * @returns {Array} Modified array
- */
- function difference() {
- var args = Array.prototype.slice.call(arguments),
- target = args.splice(0, 1)[0];
- return target.filter(function (entry) {
- return !args.some(function (arr) {
- return !!~arr.indexOf(entry);
- });
- });
- }
- /**
- * Stringifies 'data' parameter and sets it under 'key' namespace to localStorage.
- *
- * @param {*} data
- * @param {String} key
- */
- function set(data, key) {
- storage.setItem(key, stringify(data));
- }
- /**
- * Gets item from localStorage by 'key' parameter, JSON.parse's it if defined.
- * Else, returns empty array.
- *
- * @param {String} key
- * @returns {Array}
- */
- function getModules(key) {
- var plain = storage.getItem(key);
- return plain ? JSON.parse(plain) : [];
- }
- /**
- * Concats 'modules' array with one that was previously stored by 'key' parameter
- * in localStorage, removes duplicated entries from resulting array and writes
- * it to 'key' namespace of localStorage via 'set' function.
- *
- * @param {Array} modules
- * @param {String} key
- */
- function storeModules(modules, key) {
- var old = getModules(key);
- set(uniq(old.concat(modules)), key);
- }
- /**
- * Creates Blob, writes passed data to it, then creates ObjectURL string
- * with blob data. In parallel, creates 'a' element, writes resulting ObjectURL
- * to it's href property and fileName parameter as it's download prop.
- * Clicks on 'a' and cleans up file data.
- *
- * @param {String} fileName
- * @param {Object} data
- */
- function upload(fileName, data) {
- var a = document.createElement('a'),
- blob,
- url;
- a.style = 'display: none';
- document.body.appendChild(a);
- blob = new Blob([JSON.stringify(data)], {
- type: 'octet/stream'
- });
- url = window.URL.createObjectURL(blob);
- a.href = url;
- a.download = fileName;
- a.click();
- window.URL.revokeObjectURL(url);
- }
- return {
- /**
- * Stores keys of 'modules' object to localStorage under 'all' namespace.
- *
- * @param {Object} modules
- */
- collect: function (modules) {
- storeModules(Object.keys(modules), 'all');
- },
- /**
- * Wraps 'module' in empty array and stores it to localStorage by 'used' namespace.
- *
- * @param {String} module
- */
- utilize: function (module) {
- storeModules([module], 'used');
- },
- /**
- * Returns modules, stores under 'all' namespace in localStorage via
- * getModules function.
- *
- * @return {Array}
- */
- getAll: function () {
- return getModules('all');
- },
- /**
- * Returns modules, stores under 'used' namespace in localStorage via
- * getModules function.
- *
- * @return {Array}
- */
- getUsed: function () {
- return getModules('used');
- },
- /**
- * Returns difference between arrays stored under 'all' and 'used'.
- *
- * @return {Array}
- */
- getUnused: function () {
- var all = getModules('all'),
- used = getModules('used');
- return difference(all, used);
- },
- /**
- * Clears "all" and "used" namespaces of localStorage.
- */
- clear: function () {
- storage.removeItem('all');
- storage.removeItem('used');
- },
- /**
- * Create blob containing stats data and download it
- */
- export: function () {
- upload('Magento Bundle Statistics', {
- used: this.getUsed(),
- unused: this.getUnused(),
- all: this.getAll()
- });
- }
- };
- });
- /**
- * Extension of a requirejs 'load' method
- * to load files from a build object.
- */
- define('jsbuild', [
- 'module',
- 'buildTools',
- 'statistician'
- ], function (module, tools, statistician) {
- 'use strict';
- var build = module.config() || {};
- if (!tools.isEnabled) {
- return;
- }
- require._load = require.load;
- statistician.collect(build);
- /**
- * Overrides requirejs main loading method to provide
- * support of scripts initialization from a bundle object.
- *
- * @param {Object} context
- * @param {String} moduleName
- * @param {String} url
- */
- require.load = function (context, moduleName, url) {
- var relative = tools.removeBaseUrl(url, context.config),
- data = build[relative];
- if (data) {
- statistician.utilize(relative);
- new Function(data)();
- context.completeLoad(moduleName);
- } else {
- require._load.apply(require, arguments);
- }
- };
- });
- /**
- * Extension of a requirejs text plugin
- * to load files from a build object.
- */
- define('text', [
- 'module',
- 'buildTools',
- 'mage/requirejs/text'
- ], function (module, tools, text) {
- 'use strict';
- var build = module.config() || {};
- if (!tools.isEnabled) {
- return text;
- }
- text._load = text.load;
- /**
- * Overrides load method of a 'text' plugin to provide support
- * of loading files from a build object.
- *
- * @param {String} name
- * @param {Function} req
- * @param {Function} onLoad
- * @param {Object} config
- */
- text.load = function (name, req, onLoad, config) {
- var url = req.toUrl(name),
- relative = tools.removeBaseUrl(url, config),
- data = build[relative];
- data ?
- onLoad(data) :
- text._load.apply(text, arguments);
- };
- return text;
- });
|