Description
Many Magento 2 RequireJS modules start with a check for the presence of RequireJS' "define" function. I wonder if this check is (still) necessary, or that it may be removed.
As far as I can tell "require.js" is the first JavaScript file that is loaded on every Magento 2 page, both frontend and backend. It is loaded synchronously, so all other JavaScript must be executed after this file has completed its execution. It is this file that ensures that the "define" function exists, and that it contains the 'amd' attribute.
Is the above code a remnant of the past in which the presence of RequireJS was uncertain, or is there still an important use case that is underdocumented?
Preconditions (*)
- This issue occurs in all Magento 2 versions to date.
Steps to reproduce (*)
- Visit translate.js or any other JavaScript file in Magento that contains the following code
if (typeof define === 'function' && define.amd) {
https://github.com/magento/magento2/blob/2.3-develop/lib/web/mage/translate.js
Expected result (*)
- If the check serves no function, I suggest it be removed from over 100 files in Magento 2 code. It takes up space, time, and makes these files harder to understand for a student of Magento.
define([
'jquery',
'mage/mage'
], function($){
// module body
});
Actual result (*)
- The code now looks like this:
(function (factory) {
if (typeof define === 'function' && define.amd) {
define([
'jquery',
'mage/mage'
], factory);
} else {
factory(jQuery);
}
}(function ($) {
// module body
})