let message="Hello CommonJS!"; module.exports.message=message; module.exports.add=(m,n)=>console.log(m+n);
var common=require("./common"); //读取common.js文件
console.log(common.message); //调用
common.add(100,200);
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script src="js/app.js"></script> </body> </html>
npm install -g browserify
browserify app.js > apps.js
生成apps.js文件html
apps.js文件内容:前端
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ var common=require("./common"); console.log(common.message); common.add(100,200); },{"./common":2}],2:[function(require,module,exports){ let message="Hello CommonJS!"; module.exports.message=message; module.exports.add=(m,n)=>console.log(m+n); },{}]},{},[1]);
index.html引用apps.jsnode
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script src="js/apps.js"></script> </body> </html>
运行结果:jquery
AMD 是 RequireJS 在推广过程当中对模块定义的规范化产出es6
AMD异步加载模块。它的模块支持对象 函数 构造器 字符串 JSON等各类类型的模块。npm
适用AMD规范适用define方法定义模块。数组
//定义模块 define(function(){ return{ message:"Hello AMD!", add:function(n1,n2){ return n1+n2; } } })
require(['amd'],function(amd){ console.log(amd.message); console.log(amd.add(100,200)) })
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="AMD/require.js" data-main="AMD/app.js"></script>
</body>
</html>
require(['amd','jquery'],function(amd){
console.log(amd.message);
console.log(amd.add(100,200));
$("body").text("引用了第三方插件")
})
实例:浏览器
导入Seajs库app
去官网下载最新的seajs文件,http://seajs.org/docs/#downloads异步
cmd.js
define(function(require,exports,module){ var obj={ msg:"Hello SeaJS!", show:()=>console.log(obj.msg) } exports.cmd=obj; })
app.js
seajs.config({ //Sea.js 的基础路径(修改这个就不是路径就不是相对于seajs文件了) base: './js/', //别名配置(用变量表示文件,解决路径层级过深和实现路径映射) alias: { 'jquery': 'CMD/jquery' }, //路径配置(用变量表示路径,解决路径层级过深的问题) paths: { 'm': 'CMD/' } }) seajs.use(["m/cmd.js",'jquery'],function(cmd,$){ cmd.cmd.show(); $("body").text("Hello CMD!"); })
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script src="js/sea.js"></script> <script src="js/CMD/app.js"></script> </body> </html>
es6.js
//定义模块 export let msg="Hello Es6(原生模块)"; export function add(n,m){ return n+m; }
es6.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script type="module"> //导入 import {msg,add} from './es6/es6.js'; console.log(add(100,200)); console.log(msg); </script> </body> </html>
UMD(Universal Module Definition)通用的模块定义、UMD等于CommonJS加上AMD。UMD的工做其实就是作了一个判断:
CommonJs和AMD风格同样流行,彷佛缺乏一个统一的规范。因此人们产生了这样的需求,但愿有支持两种风格的“通用”模式,因而通用模块规范(UMD)诞生了。
(function (global, factory) { if (typeof define === 'function' && (define.amd || define.cmd)) { // AMD规范. 注册一个匿名模块,兼容AMD与CMD define([], factory); } else if (typeof module === 'object' && module.exports) { //CommonJS规范,NodeJS运行环境 module.exports = factory(); } else { //浏览器全局对象注册 global.UMD = factory(); } }(this, function () { var msg = "UMD!"; //返回要导出的对象 return { show: function () { console.log("Hello " + msg); } }; }));
二、在CommonJS规范下运行
useUtils.js
var utils=require('./Utils.js'); utils.show();
运行结果:
在AMD规范下运行
app.js
require(['amd','jquery','../Utils.js'],function(amd,$,u){ console.log(amd.message); console.log(amd.add(100,200)); $("body").text("引用了第三方插件"); u.show(); })
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script src="require.js" data-main="app.js"></script> </body> </html>
运行结果:
三、在CMD规范下运行
app.js
seajs.use(["cmd.js",'jquery','../Utils.js'],function(cmd,$,u){ cmd.cmd.show(); $("body").text("Hello CMD!"); u.show(); })
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script src="sea.js"></script> <script src="app.js"></script> </body> </html>
运行结果:
四、原生浏览器环境运行
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script src="../Utils.js"></script> <script> UMD.show(); </script> </body> </html>
运行结果: