SlideShare a Scribd company logo
Efficient Beautiful
Maintainable Modular
Javascript Codebase
   with RequireJS
    HelsinkiJS 2012 June
     Mikko Ohtamaa
Bio
       Mikko Ohtamaa
    moo9000 @ Twitter
http://opensourcehacker.com
Audience


• Front end developers working with
  medium and large size Javascript
  codebases(> 2k lines of code)
Grab the example code


• https://github.com/miohtama/require-js-
  mooapp-tutorial
Javascript module
         systems

• Global namespace objects:YUI, ExtJS,
  jQuery
• Node: CommonJS
• RequireJS
RequireJS

• Javascript module loader for client-side
• Version 2.0 still fresh
• MIT
Asynchronous Module
  Definition (AMD)
• No wait for <script> tag to complete
  before loading the next Javascript file
• UMD (Universal Module Definition):
  boilerplate which makes your JS file to co-
  operate with several module systems
• https://github.com/umdjs/umd
Y U Require JS?
Benefit #1: More
maintainable code base
Benefit #2: parallel
 loading of non-
 interdependent
     modules
Benefit #3: automatic,
  more optimized,
    minification
Benefit #4: avoiding
     globals
 Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJS 2012 June Mikko Ohtamaa
 Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJS 2012 June Mikko Ohtamaa
 Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJS 2012 June Mikko Ohtamaa
Using RequireJS
Basic JS development
       challenges

• Maintaining internal code dependencies
• Integrating third party libraries
• Exporting packages for production and
  distribution
Example codebase: mooapp
# Fancy designer stuff
./css
./css/colorpicker.css
./images

# Entry point
./index.html

# Our code
./mooapp/main.js
./mooapp/moo.js
./mooapp/mooficator.js

# Something we stole from the internets
./thirdparty/colorpicker.js
./thirdparty/jquery.js
./thirdparty/require.js
Defining a module
 • define(deps, creator) - RequireJS global
    function
 • deps: Array of modules we depend on
 • creator: function() which takes argument 1
    modules as parameters
 • creator returns module exports
define([“jquery”, “js/internalmod”], function($,
internalmod) {
    return { someVar : 5 }
});
moo.js
mooficator.js
Configuring RequireJS
• require() and require.config()
• Base URL
• Paths where modules can be found
• Shim and dependencies for non-AMD-
  compatible modules
• Do in JS so can be later consumed by
  optimizer
main.js
HTML file
 is now clean
index.html
RequireJS limitations

  • Source code tree SHOULD match module
    paths
  • Shim existing libraries
  • Not file:// compatible
$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
Deployment
Let’s get the party started
r.js - the optimizer
• Analyzes require() and define()
  dependencies of a JS entry point
• Will merge and bundle everything to one
  minified JS file preserving the correct
  dependency loading order
• No globals > every var can be minified >
  more compact minification
The Magic
  define() and require() functions perform correctly in
the merged bundle and do not actually need separate JS
                          files
No HTML changes on
   production needed!
...because main.js is the only JS reference in index.html...
Makefile
Bundled

$ ls -lh dist/mooapp
total 148K
-rw-r--r-- 1 moo staff 147K Jun 28 17:12 main.js

$ ls -lh mooapp
-rw-r--r-- 1 moo staff 1.5K Jun 28 17:11 main.js
-rw-r--r-- 1 moo staff 1.3K Jun 28 16:20 moo.js
-rw-r--r-- 1 moo staff 840 Jun 28 16:18 mooficator.js
Thank You

         @moo9000
http://opensourcehacker.com

More Related Content

PPTX
Introduction to require js
PPT
Getting started with angular js
PPTX
Webpack Introduction
PPTX
Webpack
PDF
JSConf US 2014: Building Isomorphic Apps
PDF
Isomorphic JavaScript: #DevBeat Master Class
PDF
Bundle your modules with Webpack
PDF
General Assembly Workshop: Advanced JavaScript
Introduction to require js
Getting started with angular js
Webpack Introduction
Webpack
JSConf US 2014: Building Isomorphic Apps
Isomorphic JavaScript: #DevBeat Master Class
Bundle your modules with Webpack
General Assembly Workshop: Advanced JavaScript

What's hot (20)

PDF
Node js (runtime environment + js library) platform
PPTX
Advanced Technology Days 15 - Visual Studio Productivity
PDF
Building Isomorphic Apps (JSConf.Asia 2014)
KEY
Yahoo! scale Node.js
PPTX
Packing for the Web with Webpack
PDF
Complete MVC on NodeJS
PDF
Skinny Framework 1.0.0
PDF
Intro to Sails.js
PDF
JavaScript MV* Framework - Making the Right Choice
PDF
Server Side Apocalypse, JS
PPTX
Mvvm knockout vs angular
PPTX
Vue js for beginner
PPTX
Bundling your front-end with Webpack
PPTX
Introduction to node.js
PPTX
PPTX
CQ5 Development Setup, Maven Build and Deployment
PPT
Nodejs - Building a RESTful API
PDF
Deep dive into Vue.js
PPTX
[Blibli Brown Bag] Nodejs - The Other Side of Javascript
PPTX
Angular js tutorial slides
Node js (runtime environment + js library) platform
Advanced Technology Days 15 - Visual Studio Productivity
Building Isomorphic Apps (JSConf.Asia 2014)
Yahoo! scale Node.js
Packing for the Web with Webpack
Complete MVC on NodeJS
Skinny Framework 1.0.0
Intro to Sails.js
JavaScript MV* Framework - Making the Right Choice
Server Side Apocalypse, JS
Mvvm knockout vs angular
Vue js for beginner
Bundling your front-end with Webpack
Introduction to node.js
CQ5 Development Setup, Maven Build and Deployment
Nodejs - Building a RESTful API
Deep dive into Vue.js
[Blibli Brown Bag] Nodejs - The Other Side of Javascript
Angular js tutorial slides
Ad

Viewers also liked (20)

PDF
Cool like a Frontend Developer: Grunt, RequireJS, Bower and other Tools
PDF
Introduction to Backbone.js
PDF
Introduction à Marionette
PDF
AngularJS vs. Ember.js vs. Backbone.js
PDF
Intro to Backbone.js by Azat Mardanov for General Assembly
PDF
Introduction to Backbone.js
PPT
Backbone.js
PPTX
Backbone And Marionette : Take Over The World
PPTX
MVC & backbone.js
PPTX
Backbone/Marionette recap [2015]
PDF
Introduction to Marionette Collective
PPTX
Introduction to Backbone.js & Marionette.js
PPTX
Marionette talk 2016
PDF
Client-side MVC with Backbone.js
PDF
introduction to Marionette.js (jscafe14)
PDF
Marionette: the Backbone framework
PPTX
Backbone.js
PDF
PPTX
Introduction to Backbone.js
PPSX
RequireJS
Cool like a Frontend Developer: Grunt, RequireJS, Bower and other Tools
Introduction to Backbone.js
Introduction à Marionette
AngularJS vs. Ember.js vs. Backbone.js
Intro to Backbone.js by Azat Mardanov for General Assembly
Introduction to Backbone.js
Backbone.js
Backbone And Marionette : Take Over The World
MVC & backbone.js
Backbone/Marionette recap [2015]
Introduction to Marionette Collective
Introduction to Backbone.js & Marionette.js
Marionette talk 2016
Client-side MVC with Backbone.js
introduction to Marionette.js (jscafe14)
Marionette: the Backbone framework
Backbone.js
Introduction to Backbone.js
RequireJS
Ad

Similar to Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJS 2012 June Mikko Ohtamaa (20)

PDF
Dependency Management with RequireJS
PDF
JavaScript Dependencies, Modules & Browserify
PDF
Using RequireJS for Modular JavaScript Code
PDF
Asynchronous Module Definition (AMD)
PDF
JavaScript Modules Done Right
PDF
Module, AMD, RequireJS
PDF
Advanced Node.JS Meetup
PDF
Require js training
KEY
Modules and EmbedJS
PDF
PLOG - Modern Javascripting with Plone
PPT
JavaScript Modules in Practice
PPTX
uRequire@greecejs: An introduction to http://uRequire.org
PPTX
Intro to Node.js (v1)
PDF
An introduction to Node.js
PDF
Modular JavaScript
PPTX
Javascript first-class citizenery
PDF
Require.JS
PDF
PDF
Requirejs
Dependency Management with RequireJS
JavaScript Dependencies, Modules & Browserify
Using RequireJS for Modular JavaScript Code
Asynchronous Module Definition (AMD)
JavaScript Modules Done Right
Module, AMD, RequireJS
Advanced Node.JS Meetup
Require js training
Modules and EmbedJS
PLOG - Modern Javascripting with Plone
JavaScript Modules in Practice
uRequire@greecejs: An introduction to http://uRequire.org
Intro to Node.js (v1)
An introduction to Node.js
Modular JavaScript
Javascript first-class citizenery
Require.JS
Requirejs

More from Mikko Ohtamaa (19)

PDF
Websauna - introduction to the best Python web framework
PDF
Operations Security - SF Bitcoin Hackday March 2015
PDF
Operations security - SyPy Dec 2014 (Sydney Python users)
PDF
Operations security (OPSEC)
PDF
Plone, battle-scarred community with battle tanks
PDF
World Plone Day 2013
PDF
Test lol
PDF
Writing the docs
KEY
Solving problems one Plone package at a time
KEY
Saving Plone from Plone agony
PDF
VVV validation and linting tool
PDF
Plone IDE - the future of Plone development
PDF
Javascript - How to avoid the bad parts
PDF
The Easy Way - Plone Conference 2011
KEY
Mobile Landscape 2011
PDF
Mobiilimarkkinoinnin mahdollisuudet nyt
PDF
The World Outside Plone
PPTX
mFabrik Case Studies
PPTX
Building HTML based mobile phone applications
Websauna - introduction to the best Python web framework
Operations Security - SF Bitcoin Hackday March 2015
Operations security - SyPy Dec 2014 (Sydney Python users)
Operations security (OPSEC)
Plone, battle-scarred community with battle tanks
World Plone Day 2013
Test lol
Writing the docs
Solving problems one Plone package at a time
Saving Plone from Plone agony
VVV validation and linting tool
Plone IDE - the future of Plone development
Javascript - How to avoid the bad parts
The Easy Way - Plone Conference 2011
Mobile Landscape 2011
Mobiilimarkkinoinnin mahdollisuudet nyt
The World Outside Plone
mFabrik Case Studies
Building HTML based mobile phone applications

Recently uploaded (20)

PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Modernizing your data center with Dell and AMD
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Electronic commerce courselecture one. Pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
KodekX | Application Modernization Development
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
cuic standard and advanced reporting.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
CIFDAQ's Market Insight: SEC Turns Pro Crypto
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Modernizing your data center with Dell and AMD
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The Rise and Fall of 3GPP – Time for a Sabbatical?
Electronic commerce courselecture one. Pdf
A Presentation on Artificial Intelligence
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Digital-Transformation-Roadmap-for-Companies.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
The AUB Centre for AI in Media Proposal.docx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
KodekX | Application Modernization Development
Spectral efficient network and resource selection model in 5G networks
cuic standard and advanced reporting.pdf
Big Data Technologies - Introduction.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Per capita expenditure prediction using model stacking based on satellite ima...

Beautiful Maintainable ModularJavascript Codebase with RequireJS - HelsinkiJS 2012 June Mikko Ohtamaa

  • 1. Efficient Beautiful Maintainable Modular Javascript Codebase with RequireJS HelsinkiJS 2012 June Mikko Ohtamaa
  • 2. Bio Mikko Ohtamaa moo9000 @ Twitter http://opensourcehacker.com
  • 3. Audience • Front end developers working with medium and large size Javascript codebases(> 2k lines of code)
  • 4. Grab the example code • https://github.com/miohtama/require-js- mooapp-tutorial
  • 5. Javascript module systems • Global namespace objects:YUI, ExtJS, jQuery • Node: CommonJS • RequireJS
  • 6. RequireJS • Javascript module loader for client-side • Version 2.0 still fresh • MIT
  • 7. Asynchronous Module Definition (AMD) • No wait for <script> tag to complete before loading the next Javascript file • UMD (Universal Module Definition): boilerplate which makes your JS file to co- operate with several module systems • https://github.com/umdjs/umd
  • 10. Benefit #2: parallel loading of non- interdependent modules
  • 11. Benefit #3: automatic, more optimized, minification
  • 17. Basic JS development challenges • Maintaining internal code dependencies • Integrating third party libraries • Exporting packages for production and distribution
  • 18. Example codebase: mooapp # Fancy designer stuff ./css ./css/colorpicker.css ./images # Entry point ./index.html # Our code ./mooapp/main.js ./mooapp/moo.js ./mooapp/mooficator.js # Something we stole from the internets ./thirdparty/colorpicker.js ./thirdparty/jquery.js ./thirdparty/require.js
  • 19. Defining a module • define(deps, creator) - RequireJS global function • deps: Array of modules we depend on • creator: function() which takes argument 1 modules as parameters • creator returns module exports define([“jquery”, “js/internalmod”], function($, internalmod) { return { someVar : 5 } });
  • 21. Configuring RequireJS • require() and require.config() • Base URL • Paths where modules can be found • Shim and dependencies for non-AMD- compatible modules • Do in JS so can be later consumed by optimizer
  • 23. HTML file is now clean
  • 25. RequireJS limitations • Source code tree SHOULD match module paths • Shim existing libraries • Not file:// compatible $ python -m SimpleHTTPServer Serving HTTP on 0.0.0.0 port 8000 ...
  • 26. Deployment Let’s get the party started
  • 27. r.js - the optimizer • Analyzes require() and define() dependencies of a JS entry point • Will merge and bundle everything to one minified JS file preserving the correct dependency loading order • No globals > every var can be minified > more compact minification
  • 28. The Magic define() and require() functions perform correctly in the merged bundle and do not actually need separate JS files
  • 29. No HTML changes on production needed! ...because main.js is the only JS reference in index.html...
  • 31. Bundled $ ls -lh dist/mooapp total 148K -rw-r--r-- 1 moo staff 147K Jun 28 17:12 main.js $ ls -lh mooapp -rw-r--r-- 1 moo staff 1.5K Jun 28 17:11 main.js -rw-r--r-- 1 moo staff 1.3K Jun 28 16:20 moo.js -rw-r--r-- 1 moo staff 840 Jun 28 16:18 mooficator.js
  • 32. Thank You @moo9000 http://opensourcehacker.com

Editor's Notes