其实本文的标题应该是「为何我不推荐使用 AMD 的 Simplified CommonJS wrapping」,但太长了很差看,为了美观我只能砍掉一截。 node
它是什么?
为了复用已有的 CommonJS 模块,AMD 规定了 Simplified CommonJS wrapping,而后 RequireJS 实现了它(前后顺序不必定对)。它提供了相似于 CommonJS 的模块定义方式,以下:python
define(
function
(require, exports, module) {
var
A = require(
'a'
);
return
function
() {};
});
|
这样,模块的依赖能够像 CommonJS 同样「就近定义」。但就是这个看上去一箭双鵰的作法,给你们带来了不少困扰。git
它作了什么?
因为 RequireJS 是最流行的 AMD 加载器,后续讨论都基于 RequireJS 进行。github
直接看 RequireJS 这部分逻辑:
//If no name, and callback is a function, then figure out if it a
//CommonJS thing with dependencies.
if
(!deps && isFunction(callback)) {
deps = [];
if
(callback.length) {
callback
.toString()
.replace(commentRegExp,
''
)
.replace(cjsRequireRegExp,
function
(match, dep) {
deps.push(dep);
});
deps = (callback.length === 1 ? [
'require'
] : [
'require'
,
'exports'
,
'module'
]).concat(deps);
}
}
|
能够看到,为了支持 CommonJS Wrapper 这种写法,define 函数里须要作这些事情:
- 经过
factory.toString()
拿到 factory 的源码; - 去掉源码中的注释(避免匹配到注释掉的依赖模块);
- 经过正则匹配
require
的方式获得依赖信息;
写模块时要把 require
当成保留字。模块加载器和构建工具都要实现上述逻辑。
对于 RequireJS,本文最开始定义的模块,最终会变成:
define([
'a'
],
function
(require, exports, module) {
var
A = require(
'a'
);
return
function
() {};
});
|
等价于:
define([
'a'
],
function
(A) {
return
function
() {};
});
|
结论是,CommonJS Wrapper 只是书写上兼容了 CommonJS 的写法,模块运行逻辑并不会改变。
AMD 运行策略
AMD 运行时核心思想是「Early Executing」,也就是提早执行依赖。这个好理解:
//main.js
define([
'a'
,
'b'
],
function
(A, B) {
//运行至此,a.js 和 b.js 已下载完成(运行于浏览器的 Loader 必须如此);
//A、B 两个模块已经执行完,直接可用(这是 AMD 的特性);
return
function
() {};
});
|
我的以为,AMD 的这个特性有好有坏:
首先,尽早执行依赖能够尽早发现错误。上面的代码中,假如 a 模块中抛异常,那么 main.js 在调用 factory 方法以前必定会收到错误,factory 不会执行;若是按需执行依赖,结果是:1)没有进入使用 a 模块的分支时,不会发生错误;2)出错时,main.js 的 factory 方法极可能执行了一半。
另外,尽早执行依赖一般能够带来更好的用户体验,也容易产生浪费。例如模块 a 依赖了另一个须要异步加载数据的模块 b,尽早执行 b 可让等待时间更短,同时若是 b 最后没被用到,带宽和内存开销就浪费了;这种场景下,按需执行依赖能够避免浪费,可是带来更长的等待时间。
我我的更倾向于 AMD 这种作法。举一个不太恰当的例子:Chrome 和 Firefox 为了更好的体验,对于某些类型的文件,点击下载地址后会询问是否保存,这时候实际上已经开始了下载。有时候等了好久才点确认,会开心地发现文件已经下好;若是点取消,浏览器会取消下载,已下载的部分就浪费了。
了解到 AMD 这个特性后,再来看一段代码:
//mod1.js
define(
function
() {
console.log(
'require module: mod1'
);
return
{
hello:
function
() {
console.log(
"hello mod1"
);
}
};
});
|
//mod2.js
define(
function
() {
console.log(
'require module: mod2'
);
return
{
hello:
function
() {
console.log(
"hello mod2"
);
}
};
});
|
//main.js
define([
'mod1'
,
'mod2'
],
function
(mod1, mod2) {
//运行至此,mod1.js 和 mod2.js 已经下载完成;
//mod一、mod2 两个模块已经执行完,直接可用;
console.log(
'require module: main'
);
mod1.hello();
mod2.hello();
return
{
hello:
function
() {
console.log(
'hello main'
);
}
};
});
|
<!--index.html-->
<
script
>
require(['main'], function(main) {
main.hello();
});
</
script
>
|
在本地测试,一般结果是这样的:
require module: mod1
require module: mod2
require module: main
hello mod1
hello mod2
hello main
|
这个结果符合预期。可是这就是所有吗?用 Fiddler 把 mod1.js 请求 delay 200
再测试,此次输出:
require module: mod2
require module: mod1
require module: main
hello mod1
hello mod2
hello main
|
这是由于 main.js 中 mod1 和 mod2 两个模块并行加载,且加载完就执行,因此前两行输出顺序取决于哪一个 js 先加载完。若是必定要让 mod2 在 mod1 以后执行,须要在 define 模块时申明依赖,或者经过 require.config 配置依赖:
require.config({
shim: {
'mod2'
: {
deps : [
'mod1'
]
}
}
});
|
严重问题!
咱们再回过头来看 CommonJS Wrapper 会带来什么问题。前面说过,AMD 规范中,上面的 main.js
等价于这样:
//main.js
define(
function
(require, exports, module) {
//运行至此,mod1.js 和 mod2.js 已经下载完成;
console.log(
'require module: main'
);
var
mod1 = require(
'./mod1'
);
//这里才执行 mod1 ?
mod1.hello();
var
mod2 = require(
'./mod2'
);
//这里才执行 mod2 ?
mod2.hello();
return
{
hello:
function
() {
console.log(
'hello main'
);
}
};
});
|
这种「就近」书写的依赖,很是容易让人认为 main.js 执行到对应 require 语句时才执行 mod1 或 mod2,但这是错误的,由于 CommonJS Wrapper 并不会改变 AMD「尽早执行」依赖的本质!
实际上,对于按需执行依赖的加载器,如 SeaJS,上述代码结果必定是:
require module: main
require module: mod1
hello mod1
require module: mod2
hello mod2
hello main
|
因而,了解过 CommonJS 或 CMD 模块规范的同窗,看到使用 CommonJS Wrapper 方式写的 AMD 模块,容易产生理解误差,从而误认为 RequireJS 有 bug。
我以为「尽早执行」或「按需执行」两种策略没有明显的优劣之分,但 AMD 这种「模仿别人写法,却提供不同的特性」这个作法十分愚蠢。这年头,作本身最重要!
其余问题
还有一个小问题也顺带提下:默认状况下,定义 AMD 模块时经过参数传入依赖列表,简单可依赖。而用了 CommonJS Wrapper 以后,RequireJS 须要经过正则从 factory.toString()
中提取依赖,复杂并容易出错。如 RequireJS 下这段代码会出错:
define(
function
(require, exports, module) {
'/*'
;
var
mod1 = require(
'mod1'
),
mod2 = require(
'mod2'
);
'*/'
;
mod1.hello();
});
//Uncaught Error: Module name "mod1" has not been loaded yet for context: _
|
固然,这个由于 RequireJS 的正则没写好,把正常语句当注释给过滤了,SeaJS 用的正则处理上述代码没问题,同时复杂了许多。
虽然实际项目中很难出现上面这样的代码,但若是放弃对脑残的 CommonJS Wrapper 支持后,再写 AMD 加载器就更加简单可靠。例如雨夜带刀同窗写的 seed,代码十分简洁;构建工具一般基于字符串分析,仍然须要过滤注释,但能够采用 uglifyjs 压缩等取巧的方法。
考虑到不是每一个 AMD Loader 都支持 CommonJS Wrapper,用参数定义依赖也能保证更好的模块通用性。至于「就近」定义依赖,我一直以为无关紧要,咱们写 php 或 python 时,include 和 import 都会放在顶部,这样看代码时能一目了然地看到全部依赖,修改起来也方便。