What are the 'module' and 'define' in jQuery source code?

I tried to read and understand jQuery source code. But I can not find information about the next part. I tried to understand the comments next to him, but I can not get any useful value from this.

if ( typeof module === "object" && module && typeof module.exports === "object" ) {
    // Expose jQuery as module.exports in loaders that implement the Node
    // module pattern (including browserify). Do not create the global, since
    // the user will be storing it themselves locally, and globals are frowned
    // upon in the Node module world.
    module.exports = jQuery;
} else {
    // Otherwise expose jQuery to the global object as usual
    window.jQuery = window.$ = jQuery;

    // Register as a named AMD module, since jQuery can be concatenated with other
    // files that may use define, but not via a proper concatenation script that
    // understands anonymous AMD modules. A named AMD is safest and most robust
    // way to register. Lowercase jquery is used because AMD module names are
    // derived from file names, and jQuery is normally delivered in a lowercase
    // file name. Do this after creating the global so that if an AMD module wants
    // to call noConflict to hide this version of jQuery, it will work.
    if ( typeof define === "function" && define.amd ) {
        define( "jquery", [], function () { return jQuery; } );
    }

I also debugged the code and found that moduleand definenot undefined.

I wondered where it came from moduleand define? How can I understand that? What is it used for?

+6
source share
3 answers

This part is a UMD shell .

Umd wrapper?

, . , .

, jQuery , , . , , (commonJS AMD).

?

ES5 , (, import my.module java). , .

- script HTML (.. window), . , , ( , ).

, 3 , , . :

  1. commonJS, , - , browserify webpack. , module.exports ( var module = require('modulePath');).
  2. AMD (Asynchronous Module Definition), , , RequireJS. , define (define , ).
  3. , , ES6, JavaScript ( , babel, ). ( ES6 ).
+4

define ( define.amd) RequireJS. module, Node.js( ). , jQuery , , , .

, , jQuery, , jQuery.

+1

Hint in the comment: "Set jQuery as module.exports in loaders that implement the Node module template." This applies to the Node.js module template, see http://www.sitepoint.com/understanding-module-exports-exports-node-js/ for more information.

0
source

Source: https://habr.com/ru/post/1612552/


All Articles