Slide 1

Slide 1 text

RAPTORJS:  A  MODULAR  JAVASCRIPT   TOOLSET  BY  EBAY   Presented  by:   Patrick  Steele-­‐Idem   Presenta5on  Pla7orm  Engineer,  eBay     October  2012   h-p://raptorjs.org/  

Slide 2

Slide 2 text

RaptorJS  is  a  collec;on  of  tools  and  libraries  that   make  web  applica;on  development  easier  by   solving  common  problems.  

Slide 3

Slide 3 text

Which  Problems  and  Challenges     does  RaptorJS  Solve?   •  Sharing  code  across  environments   •  Suppor;ng  mul;ple  target  devices   •  Op;mizing  web  applica;ons   •  Build  tools  (yes,  build  tools  can  be  a  problem)   •  Managing  complex  dependencies   •  Downloading  modules  asynchronously  (efficiently)   •  Working  with  compiled  resources  such  as  LESS   •  Wri;ng  maintainable  JavaScript  code  

Slide 4

Slide 4 text

RaptorJS  is  a  toolset  designed  to  work   alongside  exis;ng  JavaScript  libraries—not   replace  them.       Since  it's  a  toolset  and  not  a  framework,  feel   free  to  use  only  what  you  need.  

Slide 5

Slide 5 text

RaptorJS  Is  Not:   •  A  replacement  for  jQuery/Backbone/etc.   •  A  UI  framework   •  A  replacement  module  loader  for  Node   •  An  MV**  framework  

Slide 6

Slide 6 text

Managing  dependencies  in  JavaScript     applica;ons  can  be  hard.       ?   •  Dependencies  change   •  Dependencies  are  transi;ve     •  Dependencies  can  vary  by  target  environment   •  Many  different  types  of  dependencies  (JS,  CSS,  LESS,  etc.)   •  Order  ma-ers  

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Should  the  users  of  your  code  have  to   understand  all  of  its  dependencies?     We  don’t  think  so.  

Slide 9

Slide 9 text

Introducing  RaptorJS!     •  A  packaging  system  and  an  op;mizer   •  An  asynchronous  module  loader  for  the  browser   •  A  module  loader  for  server-­‐side  JavaScript   •  A  lightweight  JavaScript  library  for  defining   modules,  classes,  mixins  and  enums   •  Plus!...Raptor  Templates  (awesome)  and  a  Widget   Framework   RaptorJS  is  a  set  of  tools  that  includes  the  following:  

Slide 10

Slide 10 text

Packaging  Code  

Slide 11

Slide 11 text

Packaged  Modules   JS   CSS   JS   LESS   moduleA   moduleB   JS   JS   JS   JS   JS   JS   JS   JS   moduleC   What  can  you  do  with  packages?   •  Share  code  with  others   •  Load  code  on  the  server   •  Deliver  code  to  the  browser  

Slide 12

Slide 12 text

Describing  Dependencies   How  do  you  describe  the  dependencies  of  a   JavaScript  module  or  UI  component?       Answer:  package.json     package.json  files  allow  rich  metadata  and   dependency  informa;on  to  be  defined  for     JavaScript  modules  and  UI  components.  

Slide 13

Slide 13 text

RaptorJS  packages  allow  for   environment-­‐specific  dependencies.  

Slide 14

Slide 14 text

Sample  package.json   {          "name":  "test",          "version":  "1.0",          "description":  "A  test  module",            "raptor":  {                  "dependencies":  [                          {"module":  "widgets"},                          "test.js",                          "test.css"                  ]                              }   }   Op;onal  metadata   These  files  are  always   included/loaded  (in  order)   This  dependency  is  ignored  unless  the   “server”  extension  is  enabled.   Package   extensions   This  module  depends  on  the  “widgets”  module                  ,                  "extensions":  [                          {                                  "name":  "server",                                  "dependencies":  [                                          "test_server.js"                                  ]                          }                  ]  

Slide 15

Slide 15 text

Delivering  Code  

Slide 16

Slide 16 text

The  RaptorJS  Op;mizer   •  Generates  op;mized  JS/CSS  bundles  and  the   HTML  markup  to  include  them  in  your  pages   •  Compiles,  minifies  and  bundles  dependencies   •  Source  code  agnos;c   •  Generates  op;mized  bundles  with  checksums   (cache  forever)   •  Configurable  and  extensible   •  Use  at  build-­‐;me  or  run;me   •  Runs  under  Node  (Rhino  support  later)  

Slide 17

Slide 17 text

RaptorJS  Op;mizer   Bundle  Configura;on                                                                                                                          

Slide 18

Slide 18 text

RaptorJS  Op;mizer   Raptor  Templates  Taglib                                                                                                                                                                                                                                            Test  Page                                                                                        Hello  World!                                                          

Slide 19

Slide 19 text

RaptorJS  Op;mizer   Raptor  Templates  Taglib  (output)                              Test  Page                                                        Hello  World!                                

Slide 20

Slide 20 text

The  RaptorJS  Async  Loader   •  Allows  you  to  download  addi;onal  resources  aher   the  ini;al  page  load   •  Extremely  lightweight  and    simple  implementa;on   •  U;lizes  metadata  generated  by  the  RaptorJS   Op;mizer   •  Metadata  includes  dependency  informa;on  and  URLs  (with   checksums)   •  Parallel  downloading   •  Immediate  execu;on  

Slide 21

Slide 21 text

RaptorJS  Op;mizer   Async  Loader  Metadata   {          "my-­‐module":  {                  "js":  ["/static/page1-­‐async-­‐body-­‐d9103c2a.js"],                  "css":  ["/static/page1-­‐async-­‐head-­‐1929e414.css",                                    "/static/bundle2-­‐head-­‐c17b7d9b.css"],                  "requires":  ["another-­‐module"]          },            "another-­‐module":  {                  "js":  ["/static/bundle2-­‐body-­‐f642f7bc.js"]          }   }  

Slide 22

Slide 22 text

Wri;ng  Code  

Slide 23

Slide 23 text

Object-­‐oriented  Programming   RaptorJS  provides  support  for  modules,  classes,   mixins  and  enums.       RaptorJS  classes  support  inheritance.     All  objects  are  lazily  ini;alized  and  created  using  a   factory  func;on.  The  factory  func;on  allows  each   object  to  have  a  private  namespace  using  func;on   closures.  

Slide 24

Slide 24 text

Object-­‐oriented  Programming   Lazy  vs.  Non-­‐Lazy  Ini;aliza;on   var  myModule  =  (function()  {        //NOT  Lazy     }());  //ß  Self-­‐executing  function     registeredModules['myModule']  =  function()  {          //Lazy   }  

Slide 25

Slide 25 text

Object-­‐oriented  Programming   Defining  Modules   raptor.define  (          "shapes",          function(raptor)  {                                    var  Rectangle  =  raptor.require("shapes.Rectangle"),                          Circle  =  raptor.require("shapes.Circle");                                    return  {                          createRectangle:  function(width,  height)  {                                  return  new  Rectangle(width,  height);                          },                                                    createCircle:  function(radius)  {                                  return  new  Circle(radius);                          },                                                    getTotalArea:  function(shapes)  {                                  var  area  =  0;                                  raptor.forEach(shapes,  function(shape)  {                                          area  +=  shape.getArea();                                  });                                  return  area;                          }                  };          });   Module  name   Factory   Func;on   “Imports”   and   private   variables   Module   proper;es  

Slide 26

Slide 26 text

Object-­‐oriented  Programming   Defining  Modules   raptor.define  (          "shapes",          function(raptor)  {                                    var  Rectangle  =  raptor.require("shapes.Rectangle"),                          Circle  =  raptor.require("shapes.Circle");                                    return  {                          createRectangle:  function(width,  height)  {                                  return  new  Rectangle(width,  height);                          },                                                    createCircle:  function(radius)  {                                  return  new  Circle(radius);                          },                                                    getTotalArea:  function(shapes)  {                                  var  area  =  0;                                  raptor.forEach(shapes,  function(shape)  {                                          area  +=  shape.getArea();                                  });                                  return  area;                          }                  };          });   Module  name   Factory   Func;on   “Imports”   and   private   variables   Module   proper;es  

Slide 27

Slide 27 text

Object-­‐oriented  Programming   Prototype-­‐based  Inheritance   raptor.define  (          “shapes.Rectangle”,          “shapes.Shape”,          function()  {                  var  Rectangle  =  function(width,  height)  {                          Rectangle.superclass.constructor.call(                                  this,  “rect”);                          this.width  =  width;                          this.height  =  height;                  };                                    Rectangle.prototype  =  {                          getArea:  function()  {                                  return  this.width  *  this.height;                          }                  };                                    return  Rectangle;          });   Superclass  name   Returned  class  gets  augmented  with   "superclass"—a  reference  to  the   prototype  of  the  superclass  

Slide 28

Slide 28 text

Object-­‐oriented  Programming   Mixins   raptor.defineMixin(          “shapes.CircleMixins”,          function()  {                  area:  function()  {                          return  Math.PI  *  this.radius  *  this.radius;                  },                  scale:  function(percentage)  {                          this.radius  *=  percentage;                  }          });     raptor.defineClass(          “ui.CircleButton”,          {  "mixins":  ["shapes.CircleMixins”]  },          function()  {                  ...          });   Mixin  name   An  array  of  mixins  to  apply  to   the  class  prototype  

Slide 29

Slide 29 text

Object-­‐oriented  Programming   Extending  Objects  with  Mixins   raptor.extend(          "cookies",          function()  {                                    return  {                          getCookie:  function(name)  {                                  var  cookiesStr  =  document.cookies;                                  ...                          }                  };          });   Name  of  module  or   class  being  extended   Mixins  are  lazily  applied  when  the   target  object  is  first  ini;alized  

Slide 30

Slide 30 text

Object-­‐oriented  Programming   Using  Modules  and  Classes   //  1)  Obtaining  a  reference  to  the  “shapes”  module   var  shapes  =  raptor.require("shapes");     //  2)  Creating  an  instance  of  a  Rectangle   var  rect  =  shapes.createRectangle(5,  6);     //  3)  Obtaining  a  reference  to  the  “shapes.Circle”  class:   var  Circle  =  raptor.require("shapes.Circle");   //NOTE:  Circle  is  a  reference  to  the  constructor  function     //  4)  Creating  a  new  instance  of  a  Circle:   var  circle  =  new  Circle(25);     //  5)  Loading  modules  asynchronously:   raptor.require(          ["moduleA",  "moduleB"],            function(moduleA,  moduleB)  {                  //Do  something  with  the  loaded  modules...          });    

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Design  Philosophies   Cross-­‐environment   Same  APIs  in  mul;ple  environments   API  Documenta;on   Automa;cally  generated  documenta;on   Reusable  Code   Packaging  and  dependency  management   Object  Oriented   Modules,  classes  and  mixins   Easily  Testable   Server-­‐side  tes;ng  and  sta;c  code  analysis   Lightweight  and  Fast   Lazy  ini;aliza;on,  ;ny  footprint  and     clean  syntax   Framework  Agnos;c   Use  RaptorJS  with  your  favorite  libraries   Modular  and  Adap;ve   Load  only  what  you  need  

Slide 33

Slide 33 text

That’s  all…for  now.  Ques;ons?     Contact  Informa   Twi-er:  @psteeleidem     Open  Source!   h-p://raptorjs.org/  

Slide 34

Slide 34 text

Backup  Slides  

Slide 35

Slide 35 text

Op;mizing  Web  Applica;ons   •  All  resources  should  be  served  up  by  a  CDN  or   caching  proxy   •  All  URLs  should  include  a  checksum   –  NOTE:  Accurate  checksums  cannot  be  generated   from  the  client   •  All  resources  should  be  set  to  cache   "permanently"  (no  revalidate)   •  Keep  bundles  consistent  across  pages!   •  Don’t  rely  on  a  loader  for  all  applica;on  code   •  Lazily  load  when  possible   •  Loading  ini;al  applica;on  code  should  not   depend  on  a  loader  to  be  loaded  first