Skip to content

JavaScript modules contain unnecessary define checks #22747

Closed
@patrick-bigbridge

Description

@patrick-bigbridge

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 (*)

  1. This issue occurs in all Magento 2 versions to date.

Steps to reproduce (*)

  1. 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 (*)

  1. 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 (*)

  1. 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
})

Metadata

Metadata

Assignees

Labels

CleanupComponent: Framework/RequireJsEvent: cd19latamIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.3.xThe issue has been reproduced on latest 2.3 release

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions