• 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
? • Dependencies change • Dependencies are transi;ve • Dependencies can vary by target environment • Many different types of dependencies (JS, CSS, LESS, etc.) • Order ma-ers
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:
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
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.
"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" ] } ]
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)
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.
"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
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... });
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
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