2 Star 4 Fork 1

十里 / ModuleJS

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

ModuleJS

介绍

ModuleJS,一个兼容CMD、AMD、UMD规范的js模块管理。

安装

npm i silis-modulejs

文件大小

文件名 文件大小 文件说明
module.min.js.zip 1.18k js代码压缩 + zip压缩,用于网络要求更高的生产运营环境
module.min.js 2.4k js代码压缩,用于生产运营时使用
module.js 7.3k js源代码,用于开发测试时使用

定义模块(define module)

  1. 定义模块
define(function(){
    ...
})
  1. 定义别名模块
define("myModule.js",[],function(){
    ...
})

导出模块(export module)

  1. return导出模块
define(function(){
    return ... //返回任意数据类型的模块
})
  1. exports导出模块
  • 默认依赖exports导出模块
define(function(require, exports, module){
    exports.sayHello = ...
})
  • 指定依赖exports导出模块
define(["exports"], function(exports){
    exports.sayHello = ...
})
  1. module.exports导出模块
  • 默认依赖module.exports导出模块
define(function(require, exports, module){
    module.exports = ...
})
  • 指定依赖module.exports导出模块
define(["exports"], function(exports){
    module.exports = ...
})
  1. 全局变量导出模块
(
    (typeof window == "object" && window) ||
    (typeof global == "object" && global)
).myModule = ... //导出任意数据类型的模块

导入模块(import module)

  1. 同步导入模块
  • 同步导入单个模块
var myModule = require("myModule.js");

var myModule = require("myModule.js", false);
  • 同步导入多个模块
var myModule1 = require("myModule1.js");
var myModule2 = require("myModule2.js");

var myModule1 = require("myModule1.js", false);
var myModule2 = require("myModule2.js", false);
  1. 异步导入模块
  • 异步导入单个模块
require("myModule.js", function(myModule){
    ...
})

var myModulePromise = require("myModule.js", true);
myModulePromise.then(function(myModule){
    ...
})
  • 异步导入多个模块
require(["myModule1.js"...], function(myModule1...){
    ...
})

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

依赖模块(dependent module)

  1. 提前导入依赖模块
define(["dependentModule1.js"...], function(dependentModule1...){
    var mainModule = {
        subMudle1: dependentModule1
    };
    
    return mainModule;
})
  1. 推后导入同步依赖模块
define(function(){
    var mainModule = {
        subMudle1: require("dependentModule1.js")
    };

    return mainModule;
})
  1. 推后导入异步依赖模块
define(function(){
    var mainModule = {};

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

AMD规范示例

AMD规范指异步模块定义(Asynchronous Module Definition),即通过异步方式加载模块

AMD规范定义模块:

define(function(){

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

})

代码文件:/example/print-amd-module.js

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

    return {
        sayHello:function()
        {
            print("Hi,我的AMD规范定义的模块");
        }
    }

})

代码文件:/example/hello-amd-module.js

异步方式加载AMD规范定义的模块:

<html>
    <head>
        <script>
            require(["hello-amd-module.js"], function(helloAmdModule){
                helloAmdModule.sayHello();
            })
        </script>
    </head>
    <body>
        加载异步模块中...
    </body>
</html>

代码文件:/example/async-load-amd-module-exmaple.html

CMD规范示例

CMD规范指同步模块定义(Common Module Definition),即通过同步方式加载模块

CMD规范定义模块:

define(function(require, exports, module){

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

})

代码文件:/example/print-cmd-module.js

define(function(require, exports, module){
    
    exports.sayHello = function(){
        var print = require("print-cmd-module.js");
        print("Hi,我的CMD规范定义的模块");
    }

})

代码文件:/example/hello-cmd-module.js

同步方式加载CMD规范定义的模块:

<html>
    <head>
        <script>
            var helloAmdModule = require(["hello-cmd-module.js"]);
            helloAmdModule.sayHello();
        </script>
    </head>
    <body>
        加载异步模块中...
    </body>
</html>

代码文件:/example/sync-load-cmd-module-exmaple.html

CMD规范示例

((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
    }
)
MIT License Copyright (c) 2021 https://gitee.com/silis/ModuleJS Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

ModuleJS,一个兼容require CMD AMD UMD规范的js模块管理 展开 收起
JavaScript
MIT
取消

发行版 (4)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/silis/ModuleJS.git
git@gitee.com:silis/ModuleJS.git
silis
ModuleJS
ModuleJS
master

搜索帮助