Why is datatables defined as datatables.net and not datatables?

Why is datatables defined as datatables.net and not datatables?

retrospacemanretrospaceman Posts: 5Questions: 2Answers: 0

Just as the title states, in the datattables.js file why when datatablaes is defined is it called datatables.net?

Example code from the datatables.js file I downloaded (Not sure if this is relevant info, but it includes plugins / extensions: responsive, buttons):

/*! DataTables styling integration
 * ©2018 SpryMedia Ltd - datatables.net/license
 */

(function( factory ){
    if ( typeof define === 'function' && define.amd ) {
        // AMD
        define( ['jquery', 'datatables.net'], function ( $ ) {
            return factory( $, window, document );
        } );
    }
    else if ( typeof exports === 'object' ) {
        // CommonJS
        var jq = require('jquery');
        var cjsRequires = function (root, $) {
            if ( ! $.fn.dataTable ) {
                require('datatables.net')(root, $);
            }
        };

        if (typeof window === 'undefined') {
            module.exports = function (root, $) {
                if ( ! root ) {
                    // CommonJS environments without a window global must pass a
                    // root. This will give an error otherwise
                    root = window;
                }

                if ( ! $ ) {
                    $ = jq( root );
                }

                cjsRequires( root, $ );
                return factory( $, root, root.document );
            };
        }
        else {
            cjsRequires( window, jq );
            module.exports = factory( jq, window, window.document );
        }
    }
    else {
        // Browser
        factory( jQuery, window, document );
    }
}(function( $, window, document, undefined ) {
'use strict';
var DataTable = $.fn.dataTable;

return DataTable;
}));

My understanding of define and require are they pull in files with the given names, but datatables has no datables.net file?

To my eye it looks like its asking for a file that doesnt exist as there is no datatables.net

Why is not just defined / required as datatables?

I understand this is probably just a limitation of my understanding with how require and define work, would appreciate any knowledge that can be given, thankyou.

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    You are refering to:

    define( ['jquery', 'datatables.net'], function ( $ ) {
    

    in the AMD loader and:

    require('datatables.net')(root, $);
    

    in the CommonJS loader?

    They refer to the NPM package name so that Node.js and builders that utilise NPM, Yarn, etc (Webpack / Vite, ESBuild, etc) can look them up that way.

    In Node.js the CommonJS require can be used to refer to a file, but you need to indicate that it is referring to a file not a package name, typically by prefixing the string with ./.

    Also with the AMD loader, and specifically Require.js which implements that, you can map the names however you want. I made use of that in this blog post.

    Hope that helps a bit :)

    Allan

  • retrospacemanretrospaceman Posts: 5Questions: 2Answers: 0

    Amazing, thank-you for the info and quick response Allan :)

Sign In or Register to comment.