1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/**
* 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;
});