1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/silis-ModuleJS

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Это зеркальный репозиторий, синхронизируется ежедневно с исходного репозитория.
Клонировать/Скачать
Внести вклад в разработку кода
Синхронизировать код
Отмена
Подсказка: Поскольку Git не поддерживает пустые директории, создание директории приведёт к созданию пустого файла .keep.
Loading...
README.en.md

ModuleJS

Introduction

ModuleJS is a js module management compatible with CMD, AMD, UMD specifications.

File size

File name File size File description
module.min.js.zip 1.15k js code compression + zip compression for production and operation environments with higher network requirements
module.min.js 2.4k js code compression, used in production operations
module.js 7.3k js source code, used for development and testing

define module

  1. Define the module
define(function(){
    ...
})
  1. Define the alias module
define("myModule.js",[],function(){
    ...
})

Export module

  1. Return export module
define(function(){
    return ... //Return modules of any data type
})
  1. exports export module

-Depends on exports export module by default

define(function(require, exports, module){
    exports.sayHello = ...
})

-Specify dependent exports to export modules

define(["exports"], function(exports){
    exports.sayHello = ...
})
  1. module.exports export module

-The default export module depends on module.exports

define(function(require, exports, module){
    module.exports = ...
})

-Specify the dependent module.exports to export the module

define(["exports"], function(exports){
    module.exports = ...
})
  1. Global variable export module
(
    (typeof window == "object" && window) ||
    (typeof global == "object" && global)
).myModule = ... //Export modules of any data type

Import module

  1. Import modules synchronously

-Import a single module simultaneously

var myModule = require("myModule.js");

or

var myModule = require("myModule.js", false);

-Import multiple modules simultaneously

var myModule1 = require("myModule1.js");
var myModule2 = require("myModule2.js");

or

var myModule1 = require("myModule1.js", false);
var myModule2 = require("myModule2.js", false);
  1. Import modules asynchronously

-Import a single module asynchronously

require("myModule.js", function(myModule){
    ...
})

or

var myModulePromise = require("myModule.js", true);
myModulePromise.then(function(myModule){
    ...
})

-Import multiple modules asynchronously

require(["myModule1.js"...], function(myModule1...){
    ...
})

or

var myModulesPromise = require(["myModule1.js"...], true);
myModulesPromise.then(function(modules){
    var myModule1 = modules[0];
    ...
})

Dependent module

  1. Import dependent modules in advance
define(["dependentModule1.js"...], function(dependentModule1...){
    var mainModule = {
        subMudle1: dependentModule1
    };
    
    return mainModule;
})
  1. Import the synchronization dependency module after the push
define(function(){
    var mainModule = {
        subMudle1: require("dependentModule1.js")
    };

    return mainModule;
})
  1. Import asynchronous dependent modules after the push
define(function(){
    var mainModule = {};

    return require(["dependentModule1.js"...], function(dependentModule1...){
        mainModule.dependentModule1 = dependentModule1;
        return mainModule;
    })
})

AMD specification example

AMD specification refers to the asynchronous module definition (Asynchronous Module Definition), that is, the module is loaded asynchronously

AMD specification defines modules:

define(function(){

    return function(text){
        setTimeout(function(){
            document.body.innerText = text;
        }, 100);
    }

})

Code file: /example/print-amd-module.js

define(["print-amd-module.js"], function(print){

    return {
        sayHello:function()
        {
            print("Hi, the module defined by my AMD specification");
        }
    }

})

Code file: /example/hello-amd-module.js

Asynchronously load the modules defined by the AMD specification:

<html>
    <head>
        <script>
            require(["hello-amd-module.js"], function(helloAmdModule){
                helloAmdModule.sayHello();
            })
        </script>
    </head>
    <body>
        Loading asynchronous module...
    </body>
</html>

Code file: /example/async-load-amd-module-exmaple.html

CMD specification example

CMD specification refers to the Common Module Definition, that is, the module is loaded in a synchronous manner

CMD specification definition module:

define(function(require, exports, module){

    module.exports = function(text){
        setTimeout(function(){
            document.body.innerText = text;
        }, 100);
    }

})

Code file: /example/print-cmd-module.js

define(function(require, exports, module){
    
    exports.sayHello = function(){
        var print = require("print-cmd-module.js");
        print("Hi, the module defined by my CMD specification");
    }

})

Code file: /example/hello-cmd-module.js

Synchronously load the modules defined by the CMD specification:

<html>
    <head>
        <script>
            var helloAmdModule = require(["hello-cmd-module.js"]);
            helloAmdModule.sayHello();
        </script>
    </head>
    <body>
        Loading asynchronous module...
    </body>
</html>

Code file: /example/sync-load-cmd-module-exmaple.html

CMD specification example

((root, factory) => {
    if (typeof define === "function" && define.amd) {
        //AMD
        define(["dependentModule1", "dependentModule2"...], factory);
    } else if (typeof exports ==='object') {
        //CommonJS
        module.exports = factory(requie("dependentModule1"), requie("dependentModule2")...);
    } else {
        root.currentModule = factory(root.dependentModule1, root.dependentModule2);
    }
})(
    (typeof window == "object" && window) || (typeof global == "object" && global),
    (dependentModule1, dependentModule2...) => {
        //todo
    }
)

Комментарии ( 0 )

Вы можете оставить комментарий после Вход в систему

Введение

Описание недоступно Развернуть Свернуть
MIT
Отмена

Обновления (4)

все
9 дней назад

Участники (1)

все

Недавние действия

Загружен новый тег v1.0.3 4 месяца назад
Загружен новый тег v1.0.2 4 месяца назад
Загружен новый тег v1.0.1 4 месяца назад
Загружен новый тег v1.0.0 4 месяца назад
Создан репозиторий 4 месяца назад
Загрузить больше
Больше нет результатов для загрузки
1
https://gitlife.ru/oschina-mirror/silis-ModuleJS.git
git@gitlife.ru:oschina-mirror/silis-ModuleJS.git
oschina-mirror
silis-ModuleJS
silis-ModuleJS
v1.0.2