SlideShare a Scribd company logo
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

More Related Content

PDF
YUI is Sexy (for JSDC.tw)
PDF
YUI 教學 - 前端工程開發實務訓練
PDF
Kissy模块化实践
PDF
YUI is Sexy - 使用 YUI 作為開發基礎
PDF
SeaJS - 前端模块化开发探索与网站性能优化实践
PDF
前端工程開發實務訓練
PDF
Top100summit前端的云时代支付宝前端平台架构 王保平
PDF
开放式类库的构建
YUI is Sexy (for JSDC.tw)
YUI 教學 - 前端工程開發實務訓練
Kissy模块化实践
YUI is Sexy - 使用 YUI 作為開發基礎
SeaJS - 前端模块化开发探索与网站性能优化实践
前端工程開發實務訓練
Top100summit前端的云时代支付宝前端平台架构 王保平
开放式类库的构建

Similar to SeaJS 那些事儿 (20)

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

More from lifesinger (6)

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

SeaJS 那些事儿