Share
Embed
Transcript
27 slides
-
What is Mantri? ✓ Manages Web Application's Dependencies ✓ Leaves
no footprint on production ✓ Offers a complete workflow ✓ IE6+ #mantrijs mantrijs.com -
What Dependencies? • Multiple Javascript Files • Each file can
depend on other files • Order of loading • Order of evaluation • Order of concatenating and building #mantrijs mantrijs.com -
Why Manage? • Automatic dependency resolution • Enables large scale
applications • Collaboration between large teams • Code scalability • Better readability #mantrijs mantrijs.com -
Page Load Breakout #mantrijs mantrijs.com mantriConf .json Vendor Libs deps.js
Your Application Mantri Runtime Finishes Parsing Synchronous XHR document.write ('<script>...'); Mantri Runtime Starts Parsing document.write ('<script>...'); Synchronous XHR -
Mantri 50k feet View $ mantri deps $ mantri build
Resolves Deps ✓ Concats app to one file ✓ Strips require statements ✓ Compiles app ✓ Optionally wraps app ✓ Minifies third-party ✓ Concatenates all ✓ #mantrijs mantrijs.com -
Mantri Declarations goog.provide('app.model.User'); goog.require('app.helpers'); app.model.User = function() { // ...
goog.provide('app'); goog.require('app.model.User'); goog.require('app.view.main'); var user = new app.model.User(); goog.provide('app.view.main'); goog.require('app.view.front); goog.require('app.view.about); goog.require('app.view.login); #mantrijs mantrijs.com -
Mantri Uses Namespaces goog.provide('app.views.frontpage'); // equals to var app =
app || {}; app.views = app.views || {}; app.views.frontpage = app.views.frontpage || {}; // and... window.app === app; #mantrijs mantrijs.com -
The HTML Document <script src="js/libs/mantri.web.js"></script> <script data-require="app" ... <script data-config="/mantriConf.json"
... <script data-deps="/deps.js" ... #mantrijs mantrijs.com -
Required Files mantriConf.json • Declare third-party libs • Dependency Config
• Build Config deps.js • Auto generated • Command line • Each time a declaration changes #mantrijs mantrijs.com -
Application Wrapping "outputWrapper": "(function(){<%=output%>})();" • IIFEs are cool • "Hides"
all previously global variables • Explicit exposing #mantrijs mantrijs.com -
Module Definitions CommonJS • Used in node.js • Supported in
Browser • Uses exports and module. exports keywords AMD Asynchronous Module Definition • Extended browser support • Uses function factories • Encapsulates each file in an anon function [[ harmony:modules ]] ES6 • Spec is not out yet • Doesn't support existing definitions (yet?) • Not a module loader #mantrijs mantrijs.com -
Universal Module Definitions (function (root, factory) { if (typeof exports
=== 'object') { module.exports = factory(); } else if (typeof define === 'function' && define.amd) { define(factory); } else { root.returnExports = factory(); } }(this, function(){return app;})); UMD module definitions can work anywhere, be it in the client, on the server or anywhere else. ...or how you can expose your library as a CommonJS or AMD module. https://github.com/umdjs/umd/ #mantrijs mantrijs.com -
• Debugging from console • Dead easy stubbing and mocking
for testing • Increases visibility and maintainability • Scales smoothly • Modern build flows decouple development from production Using Namespaces mantrijs.com #mantrijs mantrijs.com -
So Why Not Modules? • The Web is not the
server. Nor it will ever be. • Module Definitions are not Module Loaders. • Debugging requires inspector with break points. • Stubbing for Testing is really hard. • Nasty problems, lengthy troubleshooting. • Overhead. ~+7.5% minified, ~+5.5% gzip #mantrijs mantrijs.com -
• Can become verbose app.ui.combo.EventType Use an alias • Namespace
conflicts Use a wrapping IIFE • Exposes internal methods Use a wrapping IIFE • Modules are the future Use a wrapping IIFE, UMD • Modules are cool Why not Namespaces? mantrijs.com #mantrijs mantrijs.com -
Namespaces vs Modules goog.provide('app.views.main''); goog.require('app.views.frontpage'); /* win */ #mantrijs mantrijs.com
Tokens • requires build • large scale ready • calculates once per build Filenames • no build requirement • can't move files or folders easily • calculates on each page load define('viewMain, ['app/views/frontpage'], function (viewFrontpage) { /* .. */ }); -
• Synchronous, app loads before DomContentLoaded • No footprint on
production • Simple provide and require statements • Available on NPM Mantri Specifications #mantrijs mantrijs.com -
Mantri Specifications • Wraps around Google Closure Tools • Uses
Grunt Task Manager • Abstracts the complexity away • Low entry barrier that scales seamlessly #mantrijs mantrijs.com