ilife5 / cat Goto Github PK
View Code? Open in Web Editor NEWnodejs <--> AMD 转换工具
License: MIT License
nodejs <--> AMD 转换工具
License: MIT License
moduleIdentifier ->
terms
terms ->
term / terms
term ->
camelCaseIdentifier . ..
参考: amd
此类模块使用exports导出方法,特点是无依赖
math.js (CommonJs Module)
exports.add = function() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
};
等价于:
math.js (AMD Module)
define([], function() {
return {
add : function() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
}
};
});
此类模块依赖其他模块,通过exports导出方法
increment.js (CommonJs Module)
var add = require('math').add;
exports.increment = function(val) {
return add(val, 1);
};
等价于
increment.js(AMD Module)
define(['math'], function(math) {
var add = math.add;
return {
increment : function(val) {
return add(val, 1);
}
};
});
program.js (CommonJs Module)
var inc = require('increment').increment;
var a = 1;
inc(a); // 2
program.js (AMD Module)
define(['increment'], function(increment) {
var inc = increment.increment;
var a = 1;
inc(a); //2
});
这里列出扩展过的CommonJs Module的使用方法及AMD Module的转换方法,扩展来源于fekit,avalon。
Calculation.js (extend CommonJs Module)
//将构造函数Calculation通过module.exports导出
function Calculation() {
}
Calculation.prototype.add = function(x, y) {
return x + y;
};
module.exports = Calculation;
Calculation.js (AMD Module)
define([], function() {
function Calculation() {
}
Calculation.prototype.add = function(x, y) {
return x + y;
};
return Calculation;
});
add.js (extend CommonJs Module)
//向avalon添加add方法
require('avalon');
function add(){
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
}
avalon.add = add;
add.js (AMD Module)
//如果avalon并没有
define(['avalon'], function(avalon) {
function add(){
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
}
avalon.add = add;
});
common.js (extend CommonJs Module)
require('avalon');
require('jquery');
require('./libs/json2');
common.js (AMD Module)
define(['avalon', 'jquery', './libs/json2'], function(avalon, jquery, json2) {});
biz.js (extend CommonJs Module)
var tpl = require('../templates/start'); //may be ../templates/start.string
//...
$container.append(tpl);
biz.js (AMD Module)
define(['text!../templates/start'], function (template) {
//...
$container.append(template);
});
alpha.js (AMD Module)
define("alpha", ["require", "exports", "beta"], function (require, exports, beta) {
exports.verb = function() {
return beta.verb();
}
});
转换为:
alpha.js (CommonJs Module)
var beta = require('beta');
exports.verb = function() {
return beta.verb();
};
alpha.js (AMD Module)
define(["alpha"], function (alpha) {
return {
verb: function(){
return alpha.verb() + 2;
}
};
});
转换为:
alpha.js (CommonJs Module)
var alpha = require('alpha');
exports.verb = function() {
return alpha.verb() + 2;
};
add.js (AMD Module)
define({
add: function(x, y){
return x + y;
}
});
转换为:
add.js (CommonJs Module)
exports.add = function(x, y) {
return x + y;
};
action.js (AMD Module)
define(function (require, exports, module) {
var a = require('a'),
b = require('b');
exports.action = function () {};
});
action.js (CommonJs Module)
var a = require('a'),
b = require('b');
exports.action = function () {};
biz.js (AMD Module)
define(['text!../templates/start'], function (template) {
//...
$container.append(template);
});
biz.js (CommonJs Module)
var tpl = require('../templates/start');
//...
$container.append(tpl);
alpha.js (AMD Module)
define("alpha", ["require", "exports", "beta"], function (require, exports, beta) {
exports.verb = function() {
return beta.verb();
}
});
转换为:
alpha.js (CommonJs Module)
var beta = require('beta');
exports.verb = function() {
return beta.verb();
};
alpha.js (AMD Module)
define(["alpha"], function (alpha) {
return {
verb: function(){
return alpha.verb() + 2;
}
};
});
转换为:
alpha.js (CommonJs Module)
var alpha = require('alpha');
exports.verb = function() {
return alpha.verb() + 2;
};
Math.js (AMD Module)
define({
add: function(x, y){
return x + y;
}
});
转换为:
Math.js (CommonJs Module)
exports.add = function(x, y) {
return x + y;
};
action.js (AMD Module)
define(function (require, exports, module) {
var a = require('a'),
b = require('b');
exports.action = function () {};
});
action.js (CommonJs Module)
var a = require('a'),
b = require('b');
exports.action = function () {};
biz.js (AMD Module)
define(['text!../templates/start'], function (template) {
//...
$container.append(template);
});
biz.js (CommonJs Module)
var tpl = require('../templates/start');
//...
$container.append(tpl);
math.js
exports.add = function() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
};
increment.js
var add = require('math').add;
exports.increment = function(val) {
return add(val, 1);
};
program.js
var inc = require('increment').increment;
var a = 1;
inc(a); // 2
module.id == "program";
math.js
define(function(require, exports, module) {
exports.add = function() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
};
});
increment.js
define(function(require, exports, module) {
var add = require('math').add;
exports.increment = function(val) {
return add(val, 1);
};
});
program.js
define(function(require, exports, module) {
var inc = require('increment').increment;
var a = 1;
inc(a); // 2
module.id == "program";
});
Wrapped modules with non-function factory
object-data.js
define({
foo: "bar"
});
array-data.js
define([
'foo',
'bar'
]);
string-data.js
define('foo bar');
具体约定如下:
具体约定如下:
此类模块使用exports导出方法,特点是无依赖
math.js (CommonJs Module)
exports.add = function() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
};
等价于:
math.js (AMD Module)
define([], function() {
return {
add : function() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
}
};
});
此类模块依赖其他模块,通过exports导出方法
increment.js (CommonJs Module)
var add = require('math').add;
exports.increment = function(val) {
return add(val, 1);
};
等价于
increment.js(AMD Module)
define(['math'], function(math) {
var add = math.add;
return {
increment : function(val) {
return add(val, 1);
}
};
});
program.js (CommonJs Module)
var inc = require('increment').increment;
var a = 1;
inc(a); // 2
program.js (AMD Module)
define(['increment'], function(increment) {
var inc = increment.increment;
var a = 1;
inc(a); //2
});
这里列出扩展过的CommonJs Module的使用方法及AMD Module的转换方法,扩展来源于fekit,avalon。
Calculation.js (extend CommonJs Module)
//将构造函数Calculation通过module.exports导出
function Calculation() {
}
Calculation.prototype.add = function(x, y) {
return x + y;
};
module.exports = Calculation;
Calculation.js (AMD Module)
define([], function() {
function Calculation() {
}
Calculation.prototype.add = function(x, y) {
return x + y;
};
return Calculation;
});
add.js (extend CommonJs Module)
//向avalon添加add方法
require('avalon');
function add(){
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
}
avalon.add = add;
add.js (AMD Module)
//如果avalon并没有
define(['avalon'], function(avalon) {
function add(){
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
}
avalon.add = add;
});
common.js (extend CommonJs Module)
require('avalon');
require('jquery');
require('./libs/json2');
common.js (AMD Module)
define(['avalon', 'jquery', './libs/json2'], function(avalon, jquery, json2) {});
biz.js (extend CommonJs Module)
var tpl = require('../templates/start'); //may be ../templates/start.string
//...
$container.append(tpl);
biz.js (AMD Module)
define(['text!../templates/start'], function (template) {
//...
$container.append(template);
});
alpha.js (AMD Module)
define("alpha", ["require", "exports", "beta"], function (require, exports, beta) {
exports.verb = function() {
return beta.verb();
}
});
转换为:
alpha.js (CommonJs Module)
var beta = require('beta');
exports.verb = function() {
return beta.verb();
};
alpha.js (AMD Module)
define(["alpha"], function (alpha) {
return {
verb: function(){
return alpha.verb() + 2;
}
};
});
转换为:
alpha.js (CommonJs Module)
var alpha = require('alpha');
exports.verb = function() {
return alpha.verb() + 2;
};
Math.js (AMD Module)
define({
add: function(x, y){
return x + y;
}
});
转换为:
Math.js (CommonJs Module)
exports.add = function(x, y) {
return x + y;
};
action.js (AMD Module)
define(function (require, exports, module) {
var a = require('a'),
b = require('b');
exports.action = function () {};
});
action.js (CommonJs Module)
var a = require('a'),
b = require('b');
exports.action = function () {};
biz.js (AMD Module)
define(['text!../templates/start'], function (template) {
//...
$container.append(template);
});
biz.js (CommonJs Module)
var tpl = require('../templates/start');
//...
$container.append(tpl);
In a module, there is a free variable "require", which conforms to the above definiton.
In a module, there is a free variable called "exports", that is an object that the module may add its API to as it executes.
modules must use the "exports" object as the only means of exporting.
In a module, there must be a free variable "module", that is an Object.
The "module" object must have a "id" property that is the top-level "id" of the module. The "id" property must be such that require(module.id) will return the exports object from which the module.id originated. (That is to say module.id can be passed to another module, and requiring that must return the original module). When feasible this property should be read-only, don't delete.
The "module" object may have a "uri" String that is the fully-qualified URI to the resource from which the module was created. The "uri" property must not exist in a sandbox.
math.js
exports.add = function() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
};
increment.js
var add = require('math').add;
exports.increment = function(val) {
return add(val, 1);
};
program.js
var inc = require('increment').increment;
var a = 1;
inc(a); // 2
module.id == "program";
A module is defined with define
keyword, which is a function.
define(factory);
define
function accepts a single argument, the module factory.factory
may be a function or other valid values.factory
is a function, the first three parameters of the function, if specified, must be "require", "exports", and "module", in that order.factory
is not a function, then the module's exports are set to that object.In a module, there are three free variables: require
, exports
and module
.
define(function(require, exports, module) {
// The module code goes here
});
require
Functionrequire
is a function
require
accepts a module identifier.require
returns the exported API of the foreign module.require
should return null.require.async
is a function
require.async
accepts a list of module identifiers and a optional callback function.exports
ObjectIn a module, there is a free variable called "exports", that is an object that the module may add its API to as it executes.
module
Objectmodule.uri
The full resolved uri to the module.
module.dependencies
A list of module identifiers that required by the module.
module.exports
The exported API of the module. It is the same as exports
object.
A typical sample
math.js
define(function(require, exports, module) {
exports.add = function() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
};
});
increment.js
define(function(require, exports, module) {
var add = require('math').add;
exports.increment = function(val) {
return add(val, 1);
};
});
program.js
define(function(require, exports, module) {
var inc = require('increment').increment;
var a = 1;
inc(a); // 2
module.id == "program";
});
Wrapped modules with non-function factory
object-data.js
define({
foo: "bar"
});
array-data.js
define([
'foo',
'bar'
]);
string-data.js
define('foo bar');
AMD代码
define(['text!../templates/start', 'jquery'], function (template, jquery) {
var $container = $('container');
$container.append(template);
});
在转换到nodejs模式时,如何处理text!../templates/start的情形
Calculation.js
//将构造函数Calculation通过module.exports导出
function Calculation() {
}
Calculation.prototype.add = function(x, y) {
return x + y;
};
module.exports = Calculation;
math.js
exports.add = function() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
};
increment.js
var add = require('math').add;
exports.increment = function(val) {
return add(val, 1);
};
add.js
//向avalon添加add方法
function add(){
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
}
avalon.add = add;
//引入avalon json2.js 及jquery
require('avalon');
require('json2');
require('jquery');
根据步骤操作,md文件里的例子都可以执行,并成功从AMD转为commonjs。
但最新的avalon就无法转换,尝试了avalon.js版本和avalon.shim版本,都无法生效?
创建一个id为"alpha"的模块,使用了require,exports,和id为"beta"的模块:
define("alpha", ["require", "exports", "beta"], function (require, exports, beta) {
exports.verb = function() {
return beta.verb();
//Or:
return require("beta").verb();
}
});
返回对象字面量的匿名模块:
define(["alpha"], function (alpha) {
return {
verb: function(){
return alpha.verb() + 2;
}
};
});
没有依赖的模块可以用来直接定义对象字面量:
define({
add: function(x, y){
return x + y;
}
});
使用简单CJS包装定义模块
define(function (require, exports, module) {
var a = require('a'),
b = require('b');
exports.action = function () {};
});
参考:AMD.md
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.