Skip to content

跟进 cmdize #1054

Closed
lifesinger opened this Issue Dec 16, 2013 · 13 comments

7 participants

@lifesinger
seajs member

记录,防止忘记

@kmvan
kmvan commented Dec 16, 2013

果然是BLOG。。

@popomore
seajs member

是 todo list

@lichunqiang

good tool

@kmvan
kmvan commented Dec 18, 2013

请问,什么是cmdize?

@popomore
seajs member

@afc163 现在可以做一个转成 commonjs 的 😄

@afc163
seajs member
afc163 commented Jan 24, 2014

反正就是去 define 。

@popomore popomore referenced this issue Feb 28, 2014
Closed

cjsify #1117

@xsbear
xsbear commented Mar 5, 2014

这可是个福音啊,现在觉得seajs的使用成本还是有点高

@army8735
seajs member
army8735 commented Mar 5, 2014

记录

词法分析出如果是commonjs写法直接返回(有module.exports.或module.exports=,且没有define)

有define为xmd,找到define语句,将amd的factory的依赖参数改写为函数体内的require
开头加入define重写,function define(id, deps, factory) {
重载factory...
var ret = factory(require, module, exports);
if(typeof ret != 'undefined') module.exports = ret;

普通js逻辑
分析出顶层var的变量名
开头加入window判断
if(typeof window == 'undefined') {
var window = {};
}
顶层匿名函数的实参this可以忽略,因为(function(global){})(this)中传入的global其实就是exports
结尾处遍历window新增的变量名,加上var的变量名,统一赋值给module.exports

jquery插件暂时没想到

@army8735
seajs member
army8735 commented Mar 5, 2014

另外对于没有var直接赋值的全局变量,browser和node端又不一样。
browser是声明在window上,node则是global变量。
可以在执行前后对比global变量新增的变量名,提取出来赋给module.exports并delete之。

ps
过于危险,考虑做语法分析记录上下文变量,将这种变量统一提取到头部var声明中。

@army8735 army8735 self-assigned this Mar 7, 2014
@army8735
seajs member
army8735 commented Mar 7, 2014

省略var声明的全局变量有了新办法,创建一个sandbox运行于脚本的上下文,检测运行结果增加的变量,为其做头部var声明。

@army8735
seajs member
army8735 commented Mar 7, 2014

唯一的难点jQuery插件实际上是属于普通依赖的问题。一般插件会直接使用jQuery或$这个变量,需要做的即是将没有任何声明直接使用的变量作为require添加到头部。

@army8735 army8735 added this to the 3.0.0 milestone Mar 7, 2014
@army8735
seajs member

https://github.com/army8735/ranma

TODO: 移入官方库中

@army8735 army8735 closed this Jun 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.