SlideShare ist ein Scribd-Unternehmen logo
1 von 46
Downloaden Sie, um offline zu lesen
SeaJS	
  那些事儿
    seajs.org
      2012




                1
About	
  Me
•王保平	
  /	
  lifesinger	
  /	
  玉伯	
  /	
  射雕
•支付宝	
  -­‐	
  前端开发部	
  -­‐	
  基础技术组
•KISSY、SeaJS、Arale、布道、翻译




                                                2
Topics

•SeaJS	
  是什么
•核心设计与实现
•谈谈开源
•未来规划

                   3
I.	
  SeaJS	
  是什么



                     4
SeaJS
                  is
A	
  Module	
  Loader	
  for	
  the	
  Web

         SeaJS	
  是
适用于	
  Web	
  端的模块加载器


                                             5
以	
  seajs.org	
  为例




                       6
SeaJS	
  的应用场景
•SeaJS	
  是更自然的代码组织方式
•只要项目的	
  JS	
  文件超过	
  3	
  个,就适合用
•文件越多,则越适合
•误解:SeaJS	
  不适合小项目

          以	
  Jscex	
  为例

                                      7
II.	
  核心设计与实现



                 8
模块系统



       9
什么是系统


•   系统由个体组成

•   个体之间有关连,按照规则协同完成任务




https://github.com/seajs/seajs/issues/240
                                            10
模块系统的基本问题


• 系统成员:模块是什么?
• 系统通讯:模块之间如何交互?


                   11
模块定义规范

CommonJS	
                      AMD
Modules	
  /	
  1.1
                                         CMD
     Node	
  Modules
                                 Intel         ...

          CommonJS	
  
          Modules	
  /	
  2.0


                                                     12
CMD

•CMD	
  -­‐	
  Common	
  Module	
  Definition
•尽量与	
  CommonJS	
  Modules/1.1	
  以及	
  
  Node	
  Modules	
  的规范保持⼀一致

•同时考虑	
  Web	
  特性

                                               13
CMD
define(function(require,	
  exports,	
  module)	
  {

	
  	
  	
  	
  var	
  $	
  =	
  require(‘jquery’)
	
  	
  	
  	
  var	
  math	
  =	
  require(‘./math’)

	
  	
  	
  	
  exports.doSomething	
  =	
  ...

})


https://github.com/seajs/seajs/issues/242
                                                        14
模块加载器



        15
加载器的基本功能


• 模块定义规范的实现,这是模块系统的基础。
• 模块系统的启动与运行。

 https://github.com/seajs/seajs/issues/260
                                             16
Node	
  的实现
    var	
  math	
  =	
  require(‘./math’)

Step	
  1:	
  	
  	
  resolveFilename
Step	
  2:	
  	
  	
  load
Step	
  3:	
  	
  	
  compile
Step	
  4:	
  	
  	
  return	
  module.exports


https://github.com/joyent/node/blob/master/lib/module.js



                                                           17
从	
  Server	
  到	
  Web


• node_modules 查找不适合 Web 端
• 文件的同步读取不适合 Web 端
• Web 端的依赖需要提前获取


                              18
SeaJS	
  的实现
/*	
  a.js	
  */
define(function(require,	
  exports,	
  module)	
  {
	
  	
  	
  	
  var	
  b	
  =	
  require(‘./b’)                                /*	
  main.js	
  */
	
  	
  	
  	
  var	
  c	
  =	
  require(‘./c’)                                seajs.use(‘./a’)
	
  	
  	
  	
  //	
  ...
})

              Step	
  1:	
  	
  	
  解析	
  ‘./a’
              Step	
  2.1:	
  	
  	
  下载	
  	
  a
              Step	
  2.2:	
  	
  	
  执行	
  define,保存	
  a	
  的	
  factory
              Step	
  2.3:	
  	
  	
  得到依赖	
  b	
  和	
  c
              Step	
  2.4:	
  	
  	
  加载	
  b	
  和	
  c	
  
              Step	
  3:	
  	
  	
  	
  	
  	
  	
  执行	
  a	
  的	
  factory,得到	
  a	
  的	
  module.exports




                                                                                                             19
Step	
  1:	
  路径解析
                                                      seajs.use({
               require(‘jquery’)	
  	
  	
  	
  
                                                      	
  	
  	
  	
  alias:	
  {
                               parseAlias
                                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  ‘jquery’:	
  ‘jquery/1.7.2/jquery.js’
                                                      	
  	
  	
  	
  },
      require(‘jquery/1.7.2/jquery.js’)	
  	
  	
  

                                   id2uri             	
  	
  	
  	
  map:	
  [
                                                      	
  	
  	
  	
  	
  	
  	
  	
  [	
  
http://example.com/libs/jquery/1.7.2/jquery.js        	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  /^.*jquery.js$/,	
  
                                                      	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  ‘http://localhost/path/to/jquery.js’
                               parseMap               	
  	
  	
  	
  	
  	
  	
  	
  ]
                                                      	
  	
  	
  	
  ]
      http://localhost/path/to/jquery.js
                                                      })




                                                                                                                                     20
Step	
  2:	
  模块加载

        script	
  异步获取

获取文件                      plugin-­‐text

        XHR	
  同步或异步获取    plugin-­‐less
                         plugin-­‐coffee
                          plugin-­‐json
                              ...




                                          21
如何得到依赖?
          factory.toString()	
  +	
  正则匹配
https://github.com/seajs/seajs/blob/master/src/util-­‐deps.js



                       require(‘./xxx’)

    Rule	
  1:	
  	
  	
  factory	
  第⼀一个参数的命名必须是	
  require
    Rule	
  2:	
  	
  	
  require	
  函数只能接收字符串值
    Rule	
  3:	
  	
  	
  不要覆盖	
  require

         http://seajs.org/docs/zh-­‐cn/rules.html


                                                                22
依赖的回调树

            e       f
                        g
    h           d
b       c


    a




                            23
循环依赖
            e
    h           d
b       c           g

                f
    a




                        24
加载时的循环等待

            isCircularWaiting(module,	
  uri)



https://github.com/seajs/seajs/blob/master/src/module.js




                                                           25
编译时的循环等待
    if	
  (module.status	
  ===	
  STATUS.COMPILING)	
  {
    	
  	
  	
  	
  return	
  module.exports
    }


https://github.com/seajs/seajs/blob/master/src/module.js




                                                            26
Step	
  3:	
  代码编译

                                                      module.require	
  =	
  require
/*	
  a.js	
  */                                      module.exports	
  =	
  {}
define(function(require,	
  exports,	
  module)	
  {
	
  	
  	
  	
  var	
  b	
  =	
  require(‘./b’)       module.factory.call(
	
  	
  	
  	
  var	
  c	
  =	
  require(‘./c’)       	
  	
  	
  	
  	
  	
  	
  	
  window,	
  
	
  	
  	
  	
  //	
  ...                             	
  	
  	
  	
  	
  	
  	
  	
  module.require,	
  
})                                                    	
  	
  	
  	
  	
  	
  	
  	
  module.exports,
                                                      	
  	
  	
  	
  	
  	
  	
  	
  module
                                                      )




                                                                                                            27
编译前后
    factory

                 通过	
  factory.toString	
  拿到源码   plugin-­‐codelint

  factory.call

                 在反馈给用户之前可以做修改                     seajs.modify

module.exports

                                                  1.	
  紧急修复	
  bug
                                                  2.	
  测试	
  mock
                                                  3.	
  ...




                                                                      28
实现小结

• 路径解析:id -- uri
• 依赖加载:toString / onload / ...
• 代码编译:factory.call


                                 29
SeaJS	
  的可靠性



                30
SeaJS	
  的基本假设

    A	
  	
  -­‐-­‐-­‐	
  表示	
  a.js	
  执行时的时间
    a	
  	
  -­‐-­‐-­‐	
  表示	
  a.js	
  的	
  onload	
  /	
  onerror	
  时的时间

    开发时,SeaJS	
  要求:A	
  与	
  a	
  紧相邻
    上线后,SeaJS	
  要求:A	
  <	
  a


http://seajs.org/test/research/onload-­‐order/test.html
    https://github.com/seajs/seajs/issues/130

                                                                              31
疯狂的测试用例


http://seajs.org/test/runner.html

          PC、Mobile

理论上是个浏览器就应该可以跑


                                    32
已有哪些公司在用




           33
More

•seajs.log	
  /	
  seajs.cache	
  /	
  seajs.find	
  /	
  
   seajs.modify

•plugin-­‐text	
  /	
  plugin-­‐json	
  /	
  plugin-­‐combo	
  /
   plugin-­‐coffee	
  /	
  plugin-­‐less	
  /	
  plugin-­‐
   livereload	
  /	
  plugin-­‐codelint	
  /	
  ...

•seajs.pluginSDK

                                                                   34
III.	
  谈谈开源



               35
开源的目的

• 把好的东西分享出来
• 让好的东西变得更好
• 其他⼀一切皆是浮云


              36
开源中最重要的
• ⼀一个优秀、靠谱的想法
• 疯狂而持久的坚持
开源项目起步时,梦想都很丰满,但
现实都很骨感。很多人等不到后天的
太阳,经常离开于明天的晚上。



                   37
IV.	
  未来规划



              38
SeaJS	
  v1.2.0
https://github.com/seajs/seajs/issues/190


  SeaJS	
  v1.2	
  will	
  release	
  SOON	
  !



                                                  39
SeaJS	
  v1.3.0

https://github.com/seajs/seajs/issues/225




                                            40
SPM	
  1.0
will	
  release	
  at	
  Jul	
  30

    No	
  跳票	
  again!


                                     41
不仅仅是模块加载器

•JavaScript	
  模块生态圈之梦
•Arale	
  的尝试
•SPM	
  仓库


                         42
Questions?



             43
与	
  RequireJS	
  对比

•API	
  设计上比	
  RequireJS	
  更优秀
•实现上比	
  RequireJS	
  更优秀
•理念上比	
  RequireJS	
  更优秀
•更懂中国人

                                   44
向	
  CommonJS	
  /	
  NodeJS	
  /	
  
UnCommonJS	
  /	
  FlyScript	
  /	
  
RequireJS	
  /	
  ...	
  致敬!!!




                                        45
});
seajs.org




            46

Weitere ähnliche Inhalte

Ähnlich wie SeaJS 那些事儿

Node js实践
Node js实践Node js实践
Node js实践myzykj
 
Kissy design
Kissy designKissy design
Kissy designyiming he
 
J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架wtxidian
 
July.2011.w3ctech
July.2011.w3ctechJuly.2011.w3ctech
July.2011.w3ctechKai Cui
 
J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架fangdeng
 
基于Seajs的项目构建
基于Seajs的项目构建基于Seajs的项目构建
基于Seajs的项目构建Zhang Xiaoxue
 
Intro-to-SeaJS
Intro-to-SeaJSIntro-to-SeaJS
Intro-to-SeaJSlifesinger
 
建立前端开发团队 (Front-end Development Environment)
建立前端开发团队 (Front-end Development Environment)建立前端开发团队 (Front-end Development Environment)
建立前端开发团队 (Front-end Development Environment)Joseph Chiang
 
NodeJS基礎教學&簡介
NodeJS基礎教學&簡介NodeJS基礎教學&簡介
NodeJS基礎教學&簡介GO LL
 
Javascript autoload
Javascript autoloadJavascript autoload
Javascript autoloadjay li
 
使用kslite支持第三方内容开发
使用kslite支持第三方内容开发使用kslite支持第三方内容开发
使用kslite支持第三方内容开发leneli
 
Spring 2.x 中文
Spring 2.x 中文Spring 2.x 中文
Spring 2.x 中文Guo Albert
 
a glance of Javascript module
a glance of Javascript modulea glance of Javascript module
a glance of Javascript modulezipeng zhang
 
Template mb-kao
Template mb-kaoTemplate mb-kao
Template mb-kaoxwcoder
 
OPOA in Action -- 使用MagixJS简化WebAPP开发
OPOA in Action -- 使用MagixJS简化WebAPP开发OPOA in Action -- 使用MagixJS简化WebAPP开发
OPOA in Action -- 使用MagixJS简化WebAPP开发leneli
 
Programming in Objective-C
Programming in Objective-CProgramming in Objective-C
Programming in Objective-CRyan Chung
 
Introduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xIntroduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xBo-Yi Wu
 
Node.js在淘宝的应用实践
Node.js在淘宝的应用实践Node.js在淘宝的应用实践
Node.js在淘宝的应用实践taobao.com
 
模块加载策略 - 2012 SDCC, 北京
模块加载策略 - 2012 SDCC, 北京模块加载策略 - 2012 SDCC, 北京
模块加载策略 - 2012 SDCC, 北京Joseph Chiang
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Wade Huang
 

Ähnlich wie SeaJS 那些事儿 (20)

Node js实践
Node js实践Node js实践
Node js实践
 
Kissy design
Kissy designKissy design
Kissy design
 
J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架
 
July.2011.w3ctech
July.2011.w3ctechJuly.2011.w3ctech
July.2011.w3ctech
 
J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架J engine -构建高性能、可监控的前端应用框架
J engine -构建高性能、可监控的前端应用框架
 
基于Seajs的项目构建
基于Seajs的项目构建基于Seajs的项目构建
基于Seajs的项目构建
 
Intro-to-SeaJS
Intro-to-SeaJSIntro-to-SeaJS
Intro-to-SeaJS
 
建立前端开发团队 (Front-end Development Environment)
建立前端开发团队 (Front-end Development Environment)建立前端开发团队 (Front-end Development Environment)
建立前端开发团队 (Front-end Development Environment)
 
NodeJS基礎教學&簡介
NodeJS基礎教學&簡介NodeJS基礎教學&簡介
NodeJS基礎教學&簡介
 
Javascript autoload
Javascript autoloadJavascript autoload
Javascript autoload
 
使用kslite支持第三方内容开发
使用kslite支持第三方内容开发使用kslite支持第三方内容开发
使用kslite支持第三方内容开发
 
Spring 2.x 中文
Spring 2.x 中文Spring 2.x 中文
Spring 2.x 中文
 
a glance of Javascript module
a glance of Javascript modulea glance of Javascript module
a glance of Javascript module
 
Template mb-kao
Template mb-kaoTemplate mb-kao
Template mb-kao
 
OPOA in Action -- 使用MagixJS简化WebAPP开发
OPOA in Action -- 使用MagixJS简化WebAPP开发OPOA in Action -- 使用MagixJS简化WebAPP开发
OPOA in Action -- 使用MagixJS简化WebAPP开发
 
Programming in Objective-C
Programming in Objective-CProgramming in Objective-C
Programming in Objective-C
 
Introduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.xIntroduction to MVC of CodeIgniter 2.1.x
Introduction to MVC of CodeIgniter 2.1.x
 
Node.js在淘宝的应用实践
Node.js在淘宝的应用实践Node.js在淘宝的应用实践
Node.js在淘宝的应用实践
 
模块加载策略 - 2012 SDCC, 北京
模块加载策略 - 2012 SDCC, 北京模块加载策略 - 2012 SDCC, 北京
模块加载策略 - 2012 SDCC, 北京
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有
 

Mehr von lifesinger

让开发也懂前端
让开发也懂前端让开发也懂前端
让开发也懂前端lifesinger
 
SeaJS - 跨环境模块化开发实践
SeaJS - 跨环境模块化开发实践SeaJS - 跨环境模块化开发实践
SeaJS - 跨环境模块化开发实践lifesinger
 
Progressive Enhancement
Progressive EnhancementProgressive Enhancement
Progressive Enhancementlifesinger
 
KISSY Mechanism
KISSY MechanismKISSY Mechanism
KISSY Mechanismlifesinger
 
The Beauty Of Refactoring
The Beauty Of RefactoringThe Beauty Of Refactoring
The Beauty Of Refactoringlifesinger
 
Closure Compiler vs YUICompressor
Closure Compiler vs YUICompressorClosure Compiler vs YUICompressor
Closure Compiler vs YUICompressorlifesinger
 

Mehr von lifesinger (6)

让开发也懂前端
让开发也懂前端让开发也懂前端
让开发也懂前端
 
SeaJS - 跨环境模块化开发实践
SeaJS - 跨环境模块化开发实践SeaJS - 跨环境模块化开发实践
SeaJS - 跨环境模块化开发实践
 
Progressive Enhancement
Progressive EnhancementProgressive Enhancement
Progressive Enhancement
 
KISSY Mechanism
KISSY MechanismKISSY Mechanism
KISSY Mechanism
 
The Beauty Of Refactoring
The Beauty Of RefactoringThe Beauty Of Refactoring
The Beauty Of Refactoring
 
Closure Compiler vs YUICompressor
Closure Compiler vs YUICompressorClosure Compiler vs YUICompressor
Closure Compiler vs YUICompressor
 

SeaJS 那些事儿