init
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Sample configuration:
|
||||
*
|
||||
require.config({
|
||||
"config": {
|
||||
"baseUrlInterceptor": {
|
||||
"Magento_Ui/js/lib/knockout/bindings/collapsible.js": "../../../../frontend/Magento/luma/en_US/"
|
||||
}
|
||||
}
|
||||
});
|
||||
*/
|
||||
|
||||
/* global jsSuffixRegExp */
|
||||
/* eslint-disable max-depth */
|
||||
define('baseUrlInterceptor', [
|
||||
'module'
|
||||
], function (module) {
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* RequireJS Context object
|
||||
*/
|
||||
var ctx = require.s.contexts._,
|
||||
|
||||
/**
|
||||
* Original function
|
||||
*
|
||||
* @type {Function}
|
||||
*/
|
||||
origNameToUrl = ctx.nameToUrl,
|
||||
|
||||
/**
|
||||
* Original function
|
||||
*
|
||||
* @type {Function}
|
||||
*/
|
||||
newContextConstr = require.s.newContext;
|
||||
|
||||
/**
|
||||
* Remove dots from URL
|
||||
*
|
||||
* @param {Array} ary
|
||||
*/
|
||||
function trimDots(ary) {
|
||||
var i, part, length = ary.length;
|
||||
|
||||
for (i = 0; i < length; i++) {
|
||||
part = ary[i];
|
||||
|
||||
if (part === '.') {
|
||||
ary.splice(i, 1);
|
||||
i -= 1;
|
||||
} else if (part === '..') {
|
||||
if (i === 1 && (ary[2] === '..' || ary[0] === '..')) {
|
||||
//End of the line. Keep at least one non-dot
|
||||
//path segment at the front so it can be mapped
|
||||
//correctly to disk. Otherwise, there is likely
|
||||
//no path mapping for a path starting with '..'.
|
||||
//This can still fail, but catches the most reasonable
|
||||
//uses of ..
|
||||
break;
|
||||
} else if (i > 0) {
|
||||
ary.splice(i - 1, 2);
|
||||
i -= 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize URL string (remove '/../')
|
||||
*
|
||||
* @param {String} name
|
||||
* @param {String} baseName
|
||||
* @param {Object} applyMap
|
||||
* @param {Object} localContext
|
||||
* @returns {*}
|
||||
*/
|
||||
function normalize(name, baseName, applyMap, localContext) {
|
||||
var lastIndex,
|
||||
baseParts = baseName && baseName.split('/'),
|
||||
normalizedBaseParts = baseParts;
|
||||
|
||||
//Adjust any relative paths.
|
||||
if (name && name.charAt(0) === '.') {
|
||||
//If have a base name, try to normalize against it,
|
||||
//otherwise, assume it is a top-level require that will
|
||||
//be relative to baseUrl in the end.
|
||||
if (baseName) {
|
||||
//Convert baseName to array, and lop off the last part,
|
||||
//so that . matches that 'directory' and not name of the baseName's
|
||||
//module. For instance, baseName of 'one/two/three', maps to
|
||||
//'one/two/three.js', but we want the directory, 'one/two' for
|
||||
//this normalization.
|
||||
normalizedBaseParts = baseParts.slice(0, baseParts.length - 1);
|
||||
name = name.split('/');
|
||||
lastIndex = name.length - 1;
|
||||
|
||||
// If wanting node ID compatibility, strip .js from end
|
||||
// of IDs. Have to do this here, and not in nameToUrl
|
||||
// because node allows either .js or non .js to map
|
||||
// to same file.
|
||||
if (localContext.nodeIdCompat && jsSuffixRegExp.test(name[lastIndex])) {
|
||||
name[lastIndex] = name[lastIndex].replace(jsSuffixRegExp, '');
|
||||
}
|
||||
|
||||
name = normalizedBaseParts.concat(name);
|
||||
trimDots(name);
|
||||
name = name.join('/');
|
||||
} else if (name.indexOf('./') === 0) {
|
||||
// No baseName, so this is ID is resolved relative
|
||||
// to baseUrl, pull off the leading dot.
|
||||
name = name.substring(2);
|
||||
}
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get full url.
|
||||
*
|
||||
* @param {Object} context
|
||||
* @param {String} url
|
||||
* @return {String}
|
||||
*/
|
||||
function getUrl(context, url) {
|
||||
var baseUrl = context.config.baseUrl,
|
||||
newConfig = context.config,
|
||||
modulePath = url.replace(baseUrl, ''),
|
||||
newBaseUrl,
|
||||
rewrite = module.config()[modulePath];
|
||||
|
||||
if (!rewrite) {
|
||||
return url;
|
||||
}
|
||||
|
||||
newBaseUrl = normalize(rewrite, baseUrl, undefined, newConfig);
|
||||
|
||||
return newBaseUrl + modulePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace original function.
|
||||
*
|
||||
* @returns {*}
|
||||
*/
|
||||
ctx.nameToUrl = function () {
|
||||
return getUrl(ctx, origNameToUrl.apply(ctx, arguments));
|
||||
};
|
||||
|
||||
/**
|
||||
* Replace original function.
|
||||
*
|
||||
* @return {*}
|
||||
*/
|
||||
require.s.newContext = function () {
|
||||
var newCtx = newContextConstr.apply(require.s, arguments),
|
||||
newOrigNameToUrl = newCtx.nameToUrl;
|
||||
|
||||
/**
|
||||
* New implementation of native function.
|
||||
*
|
||||
* @returns {String}
|
||||
*/
|
||||
newCtx.nameToUrl = function () {
|
||||
return getUrl(newCtx, newOrigNameToUrl.apply(newCtx, arguments));
|
||||
};
|
||||
|
||||
return newCtx;
|
||||
};
|
||||
});
|
||||
|
||||
require(['baseUrlInterceptor'], function () {
|
||||
'use strict';
|
||||
|
||||
});
|
||||
@@ -0,0 +1,256 @@
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
define('mixins', [
|
||||
'module'
|
||||
], function (module) {
|
||||
'use strict';
|
||||
var contexts = require.s.contexts,
|
||||
defContextName = '_',
|
||||
defContext = contexts[defContextName],
|
||||
unbundledContextName = '$',
|
||||
unbundledContext = contexts[unbundledContextName] = require.s.newContext(unbundledContextName),
|
||||
defaultConfig = defContext.config,
|
||||
unbundledConfig = {
|
||||
baseUrl: defaultConfig.baseUrl,
|
||||
paths: defaultConfig.paths,
|
||||
shim: defaultConfig.shim,
|
||||
config: defaultConfig.config,
|
||||
map: defaultConfig.map
|
||||
},
|
||||
rjsMixins;
|
||||
|
||||
/**
|
||||
* Prepare a separate context where modules are not assigned to bundles
|
||||
* so we are able to get their true path and corresponding mixins.
|
||||
*/
|
||||
unbundledContext.configure(unbundledConfig);
|
||||
|
||||
/**
|
||||
* Checks if specified string contains
|
||||
* a plugin spacer '!' substring.
|
||||
*
|
||||
* @param {String} name - Name, path or alias of a module.
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
function hasPlugin(name) {
|
||||
return !!~name.indexOf('!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds 'mixins!' prefix to the specified string.
|
||||
*
|
||||
* @param {String} name - Name, path or alias of a module.
|
||||
* @returns {String} Modified name.
|
||||
*/
|
||||
function addPlugin(name) {
|
||||
return 'mixins!' + name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes base url from the provided string.
|
||||
*
|
||||
* @param {String} url - Url to be processed.
|
||||
* @param {Object} config - Contexts' configuration object.
|
||||
* @returns {String} String without base url.
|
||||
*/
|
||||
function removeBaseUrl(url, config) {
|
||||
var baseUrl = config.baseUrl || '',
|
||||
index = url.indexOf(baseUrl);
|
||||
|
||||
if (~index) {
|
||||
url = url.substring(baseUrl.length - index);
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts url (without baseUrl prefix)
|
||||
* from a module name ignoring the fact that it may be bundled.
|
||||
*
|
||||
* @param {String} name - Name, path or alias of a module.
|
||||
* @param {Object} config - Context's configuration.
|
||||
* @returns {String}
|
||||
*/
|
||||
function getPath(name, config) {
|
||||
var url = unbundledContext.require.toUrl(name);
|
||||
|
||||
return removeBaseUrl(url, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if specified string represents a relative path (../).
|
||||
*
|
||||
* @param {String} name - Name, path or alias of a module.
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
function isRelative(name) {
|
||||
return !!~name.indexOf('./');
|
||||
}
|
||||
|
||||
/**
|
||||
* Iteratively calls mixins passing to them
|
||||
* current value of a 'target' parameter.
|
||||
*
|
||||
* @param {*} target - Value to be modified.
|
||||
* @param {...Function} mixins - List of mixins to apply.
|
||||
* @returns {*} Modified 'target' value.
|
||||
*/
|
||||
function applyMixins(target) {
|
||||
var mixins = Array.prototype.slice.call(arguments, 1);
|
||||
|
||||
mixins.forEach(function (mixin) {
|
||||
target = mixin(target);
|
||||
});
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
rjsMixins = {
|
||||
|
||||
/**
|
||||
* Loads specified module along with its' mixins.
|
||||
* This method is called for each module defined with "mixins!" prefix
|
||||
* in its name that was added by processNames method.
|
||||
*
|
||||
* @param {String} name - Module to be loaded.
|
||||
* @param {Function} req - Local "require" function to use to load other modules.
|
||||
* @param {Function} onLoad - A function to call with the value for name.
|
||||
* @param {Object} config - RequireJS configuration object.
|
||||
*/
|
||||
load: function (name, req, onLoad, config) {
|
||||
var path = getPath(name, config),
|
||||
mixins = this.getMixins(path),
|
||||
deps = [name].concat(mixins);
|
||||
|
||||
req(deps, function () {
|
||||
onLoad(applyMixins.apply(null, arguments));
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Retrieves list of mixins associated with a specified module.
|
||||
*
|
||||
* @param {String} path - Path to the module (without base URL).
|
||||
* @returns {Array} An array of paths to mixins.
|
||||
*/
|
||||
getMixins: function (path) {
|
||||
var config = module.config() || {},
|
||||
mixins;
|
||||
|
||||
// Fix for when urlArgs is set.
|
||||
if (path.indexOf('?') !== -1) {
|
||||
path = path.substring(0, path.indexOf('?'));
|
||||
}
|
||||
mixins = config[path] || {};
|
||||
|
||||
return Object.keys(mixins).filter(function (mixin) {
|
||||
return mixins[mixin] !== false;
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Checks if specified module has associated with it mixins.
|
||||
*
|
||||
* @param {String} path - Path to the module (without base URL).
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
hasMixins: function (path) {
|
||||
return this.getMixins(path).length;
|
||||
},
|
||||
|
||||
/**
|
||||
* Modifies provided names prepending to them
|
||||
* the 'mixins!' plugin prefix if it's necessary.
|
||||
*
|
||||
* @param {(Array|String)} names - Module names, paths or aliases.
|
||||
* @param {Object} context - Current RequireJS context.
|
||||
* @returns {Array|String}
|
||||
*/
|
||||
processNames: function (names, context) {
|
||||
var config = context.config;
|
||||
|
||||
/**
|
||||
* Prepends 'mixin' plugin to a single name.
|
||||
*
|
||||
* @param {String} name
|
||||
* @returns {String}
|
||||
*/
|
||||
function processName(name) {
|
||||
var path = getPath(name, config);
|
||||
|
||||
if (!hasPlugin(name) && (isRelative(name) || rjsMixins.hasMixins(path))) {
|
||||
return addPlugin(name);
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
return typeof names !== 'string' ?
|
||||
names.map(processName) :
|
||||
processName(names);
|
||||
}
|
||||
};
|
||||
|
||||
return rjsMixins;
|
||||
});
|
||||
|
||||
require([
|
||||
'mixins'
|
||||
], function (mixins) {
|
||||
'use strict';
|
||||
|
||||
var contexts = require.s.contexts,
|
||||
defContextName = '_',
|
||||
defContext = contexts[defContextName],
|
||||
unbundledContextName = '$',
|
||||
unbundledContext = contexts[unbundledContextName],
|
||||
originalContextRequire = defContext.require,
|
||||
originalContextConfigure = defContext.configure,
|
||||
processNames = mixins.processNames;
|
||||
|
||||
/**
|
||||
* Wrap default context's require function which gets called every time
|
||||
* module is requested using require call. The upside of this approach
|
||||
* is that deps parameter is already normalized and guaranteed to be an array.
|
||||
*/
|
||||
defContext.require = function (deps, callback, errback) {
|
||||
deps = processNames(deps, defContext);
|
||||
|
||||
return originalContextRequire(deps, callback, errback);
|
||||
};
|
||||
|
||||
/**
|
||||
* Wrap original context configuration to update unbundled context,
|
||||
* that way it is able to respect any changes done after mixins module has initialized.
|
||||
*/
|
||||
defContext.configure = function (cfg) {
|
||||
originalContextConfigure(cfg);
|
||||
unbundledContext.configure(cfg);
|
||||
};
|
||||
|
||||
/**
|
||||
* Copy properties of original 'require' method.
|
||||
*/
|
||||
Object.keys(originalContextRequire).forEach(function (key) {
|
||||
defContext.require[key] = originalContextRequire[key];
|
||||
});
|
||||
|
||||
/**
|
||||
* Wrap shift method from context's definitions queue.
|
||||
* Items are added to the queue when a new module is defined and taken
|
||||
* from it every time require call happens.
|
||||
*/
|
||||
defContext.defQueue.shift = function () {
|
||||
var queueItem = Array.prototype.shift.call(this),
|
||||
lastDeps = queueItem && queueItem[1];
|
||||
|
||||
if (Array.isArray(lastDeps)) {
|
||||
queueItem[1] = processNames(queueItem[1], defContext);
|
||||
}
|
||||
|
||||
return queueItem;
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,164 @@
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
define([
|
||||
'underscore',
|
||||
'domReady!'
|
||||
], function (_) {
|
||||
'use strict';
|
||||
|
||||
var context = require.s.contexts._,
|
||||
execCb = context.execCb,
|
||||
registry = context.registry,
|
||||
callbacks = [],
|
||||
retries = 10,
|
||||
updateDelay = 1,
|
||||
ready,
|
||||
update;
|
||||
|
||||
/**
|
||||
* Checks if provided callback already exists in the callbacks list.
|
||||
*
|
||||
* @param {Object} callback - Callback object to be checked.
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
function isSubscribed(callback) {
|
||||
return !!_.findWhere(callbacks, callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if provided module is rejected during load.
|
||||
*
|
||||
* @param {Object} module - Module to be checked.
|
||||
* @return {Boolean}
|
||||
*/
|
||||
function isRejected(module) {
|
||||
return registry[module.id] && (registry[module.id].inited || registry[module.id].error);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if provided module had path fallback triggered.
|
||||
*
|
||||
* @param {Object} module - Module to be checked.
|
||||
* @return {Boolean}
|
||||
*/
|
||||
function isPathFallback(module) {
|
||||
return registry[module.id] && registry[module.id].events.error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if provided module has unresolved dependencies.
|
||||
*
|
||||
* @param {Object} module - Module to be checked.
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
function isPending(module) {
|
||||
if (!module.depCount) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return module.depCount >
|
||||
_.filter(module.depMaps, isRejected).length + _.filter(module.depMaps, isPathFallback).length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if requirejs's registry object contains pending modules.
|
||||
*
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
function hasPending() {
|
||||
return _.some(registry, isPending);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if 'resolver' module is in ready
|
||||
* state and that there are no pending modules.
|
||||
*
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
function isReady() {
|
||||
return ready && !hasPending();
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes provided callback handler.
|
||||
*
|
||||
* @param {Object} callback
|
||||
*/
|
||||
function invoke(callback) {
|
||||
callback.handler.call(callback.ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets 'resolver' module to a ready state
|
||||
* and invokes pending callbacks.
|
||||
*/
|
||||
function resolve() {
|
||||
ready = true;
|
||||
|
||||
callbacks.splice(0).forEach(invoke);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops 'ready' flag and runs the update process.
|
||||
*/
|
||||
function tick() {
|
||||
ready = false;
|
||||
|
||||
update(retries);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds callback which will be invoked
|
||||
* when all of the pending modules are initiated.
|
||||
*
|
||||
* @param {Function} handler - 'Ready' event handler function.
|
||||
* @param {Object} [ctx] - Optional context with which handler
|
||||
* will be invoked.
|
||||
*/
|
||||
function subscribe(handler, ctx) {
|
||||
var callback = {
|
||||
handler: handler,
|
||||
ctx: ctx
|
||||
};
|
||||
|
||||
if (!isSubscribed(callback)) {
|
||||
callbacks.push(callback);
|
||||
|
||||
if (isReady()) {
|
||||
_.defer(tick);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for all modules to be initiated
|
||||
* and invokes pending callbacks if it's so.
|
||||
*
|
||||
* @param {Number} [retry] - Number of retries
|
||||
* that will be used to repeat the 'update' function
|
||||
* invokation in case if there are no pending requests.
|
||||
*/
|
||||
update = _.debounce(function (retry) {
|
||||
if (!hasPending()) {
|
||||
retry ? update(--retry) : resolve();
|
||||
}
|
||||
}, updateDelay);
|
||||
|
||||
/**
|
||||
* Overrides requirejs's original 'execCb' method
|
||||
* in order to track pending modules.
|
||||
*
|
||||
* @returns {*} Result of original method call.
|
||||
*/
|
||||
context.execCb = function () {
|
||||
var exported = execCb.apply(context, arguments);
|
||||
|
||||
tick();
|
||||
|
||||
return exported;
|
||||
};
|
||||
|
||||
return subscribe;
|
||||
});
|
||||
@@ -0,0 +1,388 @@
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
var storageShim = {
|
||||
_data: {},
|
||||
|
||||
/**
|
||||
* Sets value of the specified item.
|
||||
*
|
||||
* @param {String} key - Key of the property.
|
||||
* @param {*} value - Properties' value.
|
||||
*/
|
||||
setItem: function (key, value) {
|
||||
'use strict';
|
||||
|
||||
this._data[key] = value + '';
|
||||
},
|
||||
|
||||
/**
|
||||
* Retrieves specified item.
|
||||
*
|
||||
* @param {String} key - Key of the property to be retrieved.
|
||||
*/
|
||||
getItem: function (key) {
|
||||
'use strict';
|
||||
|
||||
return this._data[key];
|
||||
},
|
||||
|
||||
/**
|
||||
* Removes specified item.
|
||||
*
|
||||
* @param {String} key - Key of the property to be removed.
|
||||
*/
|
||||
removeItem: function (key) {
|
||||
'use strict';
|
||||
|
||||
delete this._data[key];
|
||||
},
|
||||
|
||||
/**
|
||||
* Removes all items.
|
||||
*/
|
||||
clear: function () {
|
||||
'use strict';
|
||||
|
||||
this._data = {};
|
||||
}
|
||||
};
|
||||
|
||||
define('buildTools', [
|
||||
], function () {
|
||||
'use strict';
|
||||
|
||||
var storage,
|
||||
storeName = 'buildDisabled';
|
||||
|
||||
try {
|
||||
storage = window.localStorage;
|
||||
} catch (e) {
|
||||
storage = storageShim;
|
||||
}
|
||||
|
||||
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,
|
||||
stringify = JSON.stringify.bind(JSON);
|
||||
|
||||
try {
|
||||
storage = window.localStorage;
|
||||
} catch (e) {
|
||||
storage = storageShim;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
});
|
||||
@@ -0,0 +1,163 @@
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
/* inspired by http://github.com/requirejs/text */
|
||||
/*global XDomainRequest */
|
||||
|
||||
define(['module'], function (module) {
|
||||
'use strict';
|
||||
|
||||
var xmlRegExp = /^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im,
|
||||
bodyRegExp = /<body[^>]*>\s*([\s\S]+)\s*<\/body>/im,
|
||||
stripReg = /!strip$/i,
|
||||
defaultConfig = module.config && module.config() || {};
|
||||
|
||||
/**
|
||||
* Strips <?xml ...?> declarations so that external SVG and XML documents can be
|
||||
* added to a document without worry.
|
||||
* Also, if the string is an HTML document, only the part inside the body tag is returned.
|
||||
*
|
||||
* @param {String} external
|
||||
* @returns {String}
|
||||
*/
|
||||
function stripContent(external) {
|
||||
var matches;
|
||||
|
||||
if (!external) {
|
||||
return '';
|
||||
}
|
||||
|
||||
matches = external.match(bodyRegExp);
|
||||
external = matches ?
|
||||
matches[1] :
|
||||
external.replace(xmlRegExp, '');
|
||||
|
||||
return external;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that url match current location
|
||||
*
|
||||
* @param {String} url
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
function sameDomain(url) {
|
||||
var uProtocol, uHostName, uPort,
|
||||
xdRegExp = /^([\w:]+)?\/\/([^\/\\]+)/i,
|
||||
location = window.location,
|
||||
match = xdRegExp.exec(url);
|
||||
|
||||
if (!match) {
|
||||
return true;
|
||||
}
|
||||
uProtocol = match[1];
|
||||
uHostName = match[2];
|
||||
|
||||
uHostName = uHostName.split(':');
|
||||
uPort = uHostName[1] || '';
|
||||
uHostName = uHostName[0];
|
||||
|
||||
return (!uProtocol || uProtocol === location.protocol) &&
|
||||
(!uHostName || uHostName.toLowerCase() === location.hostname.toLowerCase()) &&
|
||||
(!uPort && !uHostName || uPort === location.port);
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {XMLHttpRequest|XDomainRequest|null}
|
||||
*/
|
||||
function createRequest(url) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
|
||||
if (!sameDomain(url) && typeof XDomainRequest !== 'undefined') {
|
||||
xhr = new XDomainRequest();
|
||||
}
|
||||
|
||||
return xhr;
|
||||
}
|
||||
|
||||
/**
|
||||
* XHR requester. Returns value to callback.
|
||||
*
|
||||
* @param {String} url
|
||||
* @param {Function} callback
|
||||
* @param {Function} fail
|
||||
* @param {Object} headers
|
||||
*/
|
||||
function getContent(url, callback, fail, headers) {
|
||||
var xhr = createRequest(url),
|
||||
header;
|
||||
|
||||
xhr.open('GET', url);
|
||||
|
||||
/*eslint-disable max-depth */
|
||||
if ('setRequestHeader' in xhr && headers) {
|
||||
for (header in headers) {
|
||||
if (headers.hasOwnProperty(header)) {
|
||||
xhr.setRequestHeader(header.toLowerCase(), headers[header]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
xhr.onreadystatechange = function () {
|
||||
var status, err;
|
||||
|
||||
//Do not explicitly handle errors, those should be
|
||||
//visible via console output in the browser.
|
||||
if (xhr.readyState === 4) {
|
||||
status = xhr.status || 0;
|
||||
|
||||
if (status > 399 && status < 600) {
|
||||
//An http 4xx or 5xx error. Signal an error.
|
||||
err = new Error(url + ' HTTP status: ' + status);
|
||||
err.xhr = xhr;
|
||||
|
||||
if (fail) {
|
||||
fail(err);
|
||||
}
|
||||
} else {
|
||||
callback(xhr.responseText);
|
||||
|
||||
if (defaultConfig.onXhrComplete) {
|
||||
defaultConfig.onXhrComplete(xhr, url);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/*eslint-enable max-depth */
|
||||
|
||||
if (defaultConfig.onXhr) {
|
||||
defaultConfig.onXhr(xhr, url);
|
||||
}
|
||||
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
/**
|
||||
* Main method used by RequireJs.
|
||||
*
|
||||
* @param {String} name - has format: some.module.filext!strip
|
||||
* @param {Function} req
|
||||
* @param {Function|undefined} onLoad
|
||||
*/
|
||||
function loadContent(name, req, onLoad) {
|
||||
|
||||
var toStrip = stripReg.test(name),
|
||||
url = req.toUrl(name.replace(stripReg, '')),
|
||||
headers = defaultConfig.headers;
|
||||
|
||||
getContent(url, function (content) {
|
||||
content = toStrip ? stripContent(content) : content;
|
||||
onLoad(content);
|
||||
}, onLoad.error, headers);
|
||||
}
|
||||
|
||||
return {
|
||||
load: loadContent,
|
||||
get: getContent
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user