SlideShare a Scribd company logo
RequireJS for modular
   JavaScript code                     this presentation is about
                                       Require JS.

                                       * why use it
                                       * How it works

     Thomas Lundström, Softhouse       * How it’s used
                                       * How to start using it

         @thomaslundstrom              Let’s start with some of the
                                       problems I’ve seen (and been
                                       guilty of) in web apps that
                                       use JavaScript during my

              April 16, 2012
                                       tenure as a consultant


   Scandinavian Developer Conference
JS is big         Even if you don’t run a JS
                                 framework, just “some HTML and
                                 JS”, you often have rather large JS
                                 files - quite often spaghetti-like
                                 with a large number of functions
                                 calling each other.

                                 New JS-heavy apps often use one or
                                 more of the JS Frameworks that
                                 exist today, e.g. Backbone,
                                 Knockout, Ember, SproutCore etc,
                                 making it more and more important
                                 to think about componentizing and
                                 structuring your code.




• Web-apps today contain large amounts of
  JavaScript
Dependency
management is hard
                Top-level functions in the
                global namespace often lead to
                circular dependencies (which
                aren’t easily spotted - you
                basically need to wade through
                the JS code to find all
                dependencies).

                2 Files - 5 different functions
                calling each other
Dev/deploy conflict
                 Requirements conflict bet ween
                 development and deployment:

                 Devs want small discrete files
                 since it’s easier to debug and
                 test small units.
                 Additionally, using large files
                 with a script tag doesn’t scale
                 in larger apps with large
                 number of developers: e.g.
                 merge hell in version control

                 Deployment should be done on
                 large files since we don’t want
                 the browser to load lots of files
                 - the latency gives us slowly
                 loaded pages
Require tries to solve the problems with
            growing JS code bases by introducing the
            “module” abstraction.

            In its core, modules exposes an interface
            and has dependencies. You ask RequireJS for
            a module, and RequireJS traces and
            instantiates its dependencies (including
            transitive dependencies) before
            instantiating the module you wish to use.

            Why modules?
            Modules are (or, should be) small => easy to
            understand => easy to change + easy to test




RequireJS
Define
        This is a quite standard logger that every
        site needs. This one works with IE (where
        console.log throws an error if the
        development tools aren’t enabled).

        You use the define function to define a
        module. The API is called AMD -
        asyncronous module definition.

        The file name (modules/logger) is the
        name of the module.

        Dependencies are stated in the array in
        the beginning of the function call. This
        one has no dependencies - the array is
        empty

        The function @ param2 is a factory
        function that’s executed when we
        instantiate this module

        The returned object is fed as an argument
        to the module requiring this module as a
        dependency. Here, the factory returns an
        object containing only one function: log
        (msg).

        Note: we don’t clobber the global scope
        since everything in this module is hidden
        by the scope of the anonymous function.
Define   A module that takes t wo other
        modules as dependencies.
        The names of the dependencies are the
        module names, e.g. “modules/logger”
        from the previous slide

        The returned objects from the
        dependencies are injected into our
        factory function. (Logger returns an
        object containing the log function.)
Require


          The require() call is the main
          entry point to the modules
          defined by the define() call.

          The first argument is an
          array containing all module
          names we wish to load.

          The anonymous function is a
          callback that’s called after
          the required modules and
          their dependencies have been
          loaded. The args to the
          function is the required
          modules.
End result
             The different modules in
             the diagram have defined
             dependencies, a clean
             interface (i.e. the
             returned object) and are
             easy to unit-test.
Firebug



          An image from firebug with the
          modules from the previous slide.

          Current problem: we have too many
          files, leading to slow loading times.

          Require JS optimizer to the rescue!
Firebug (optimized)




           The RequireJS optimizer compiles a top-level module (in
           our case, “module1.js”) and its dependencies (recursively)
           together into one file.

           The file is also uglified (see example below).

           -> One file per top-level module with minimal footprint
legacy JS files = large,
                  spaghetti, global scope




Introducing RequireJS
  with legacy JS files
Single JS file   If you are using only one js file
                         (or you have no cross-refs
                         bet ween different stand-alone
                         js files): you’re in luck.



• One file = one module   This requires that no-one
                         accesses the JS functions that
                         are defined in the single JS file
                         (e.g. literal click handlers on
                         buttons/links etc).

                         It’s rather uncommon, sadly.

                         BUT: If we have this we can
                         rather easily introduce
                         RequireJS in the application.

                         Let the entire file be your module
                         as a start.
                         Then, refactor small pieces of
                         functionality into sub-modules
                         as you go along.
Interconnected files



The problem here is the circular dependency bet ween file A and B at: file_a_2() -> file_b_2() -> file_a_1().

This state is rather common. There is no super-simple solution if we have these circular dependencies bet ween
file A and B.

This issue doesn’t only occur if we have different functions calling each other. We could also have one file and
a literal click handler on a button/a-link with the function name stated in HTML.

To start solving this issue, we can modularize and export only the globally used functions. (In next slide.)
Interconnected files




in the t wo highlighted rows we export the cross-ref’d functions to the global namespace. These global functions are then called at
lines 9 and 13 in file A and B respectively.

The next step is to refactor out file_a_1 and file_b_2 into separate modules. This is left as an excercise to the reader. The reason for
      extracting this is that we don’t want to use circular references bet ween modules.
Thanks!                RequireJS is Open Source,
                                    dev @ GitHub

                                    I’ll post this presentation
                                    + some samples at my


• RequireJS.org                     blog.




• Thomas Lundström, Softhouse
• thomas.lundstrom@softhouse.se
• @thomaslundstrom
• http://blog.thomaslundstrom.com

More Related Content

PDF
Workflow Yapceu2010
PPT
Java presentation
PPTX
Advance java Online Training in Hyderabad
PPTX
Advance Java Topics (J2EE)
PPTX
Project Presentation on Advance Java
DOCX
Java J2EE Complete Syllabus Checklist
KEY
CommonJS Everywhere (Wakanday 2011)
PDF
Perl6 DBDI YAPC::EU 201008
Workflow Yapceu2010
Java presentation
Advance java Online Training in Hyderabad
Advance Java Topics (J2EE)
Project Presentation on Advance Java
Java J2EE Complete Syllabus Checklist
CommonJS Everywhere (Wakanday 2011)
Perl6 DBDI YAPC::EU 201008

What's hot (20)

PPT
MVC Demystified: Essence of Ruby on Rails
PDF
From User Action to Framework Reaction
PDF
Open Social Summit Korea
PPTX
JSP overview
PPSX
RequireJS
PDF
Metaprogramming JavaScript
PPT
Jsp ppt
PPTX
Advance java prasentation
PPTX
Jsp Introduction Tutorial
PDF
The curious Life of JavaScript - Talk at SI-SE 2015
PPTX
Asynchronous Module Definition (AMD) used for Dependency Injection (DI) and MVVM
PDF
Advanced java programming-contents
PPTX
basic core java up to operator
PPTX
Client-Side Packages
PDF
netbeans
PPT
Java EE Introduction
PDF
Java interview questions
PPSX
JSP - Part 1
PPT
JavaScript Modules in Practice
MVC Demystified: Essence of Ruby on Rails
From User Action to Framework Reaction
Open Social Summit Korea
JSP overview
RequireJS
Metaprogramming JavaScript
Jsp ppt
Advance java prasentation
Jsp Introduction Tutorial
The curious Life of JavaScript - Talk at SI-SE 2015
Asynchronous Module Definition (AMD) used for Dependency Injection (DI) and MVVM
Advanced java programming-contents
basic core java up to operator
Client-Side Packages
netbeans
Java EE Introduction
Java interview questions
JSP - Part 1
JavaScript Modules in Practice
Ad

Viewers also liked (9)

PDF
Modularize JavaScript with RequireJS
PDF
Require.JS
PDF
Meet Handlebar
KEY
Requirejs
PPT
Require JS
PPTX
Require js
PDF
Using RequireJS with CakePHP
PDF
PDF
Module, AMD, RequireJS
Modularize JavaScript with RequireJS
Require.JS
Meet Handlebar
Requirejs
Require JS
Require js
Using RequireJS with CakePHP
Module, AMD, RequireJS
Ad

Similar to Using RequireJS for Modular JavaScript Code (20)

KEY
AMD - Why, What and How
PPTX
JS Essence
PDF
Dependency Management with RequireJS
PDF
JavaScript Dependencies, Modules & Browserify
PDF
High quality Front-End
PDF
Require js training
PDF
PLOG - Modern Javascripting with Plone
PPTX
Javascript first-class citizenery
PPTX
Javascript best practices
PDF
Modular JavaScript
KEY
Single Page Applications - Desert Code Camp 2012
PDF
DownTheRabbitHole.js – How to Stay Sane in an Insane Ecosystem
PDF
PDF
Expert JavaScript Programming
PDF
Building a JavaScript Module Framework at Gilt
PPTX
Modular javascript
PDF
Javascript Dependency Management
PDF
Asynchronous Module Definition (AMD)
PPTX
JS & NodeJS - An Introduction
PDF
DownTheRabbitHole.js – How to Stay Sane in an Insane Ecosystem
AMD - Why, What and How
JS Essence
Dependency Management with RequireJS
JavaScript Dependencies, Modules & Browserify
High quality Front-End
Require js training
PLOG - Modern Javascripting with Plone
Javascript first-class citizenery
Javascript best practices
Modular JavaScript
Single Page Applications - Desert Code Camp 2012
DownTheRabbitHole.js – How to Stay Sane in an Insane Ecosystem
Expert JavaScript Programming
Building a JavaScript Module Framework at Gilt
Modular javascript
Javascript Dependency Management
Asynchronous Module Definition (AMD)
JS & NodeJS - An Introduction
DownTheRabbitHole.js – How to Stay Sane in an Insane Ecosystem

More from Thomas Lundström (6)

PDF
Railsify your web development
PDF
Bdd for Web Applications at TelecomCity DevCon 2010
PDF
Ruby for C#-ers (ScanDevConf 2010)
PDF
Bdd For Web Applications from Scandinavian Developer Conference 2010
PDF
BDD approaches for web development at Agile Testing Days 2009
PDF
Cucumber mot .NET-kod
Railsify your web development
Bdd for Web Applications at TelecomCity DevCon 2010
Ruby for C#-ers (ScanDevConf 2010)
Bdd For Web Applications from Scandinavian Developer Conference 2010
BDD approaches for web development at Agile Testing Days 2009
Cucumber mot .NET-kod

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Advanced IT Governance
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
cuic standard and advanced reporting.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Big Data Technologies - Introduction.pptx
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
Electronic commerce courselecture one. Pdf
Per capita expenditure prediction using model stacking based on satellite ima...
MYSQL Presentation for SQL database connectivity
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Advanced IT Governance
Chapter 2 Digital Image Fundamentals.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Spectral efficient network and resource selection model in 5G networks
Sensors and Actuators in IoT Systems using pdf
GamePlan Trading System Review: Professional Trader's Honest Take
NewMind AI Weekly Chronicles - August'25 Week I
cuic standard and advanced reporting.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Big Data Technologies - Introduction.pptx
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Review of recent advances in non-invasive hemoglobin estimation
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Advanced methodologies resolving dimensionality complications for autism neur...

Using RequireJS for Modular JavaScript Code

  • 1. RequireJS for modular JavaScript code this presentation is about Require JS. * why use it * How it works Thomas Lundström, Softhouse * How it’s used * How to start using it @thomaslundstrom Let’s start with some of the problems I’ve seen (and been guilty of) in web apps that use JavaScript during my April 16, 2012 tenure as a consultant Scandinavian Developer Conference
  • 2. JS is big Even if you don’t run a JS framework, just “some HTML and JS”, you often have rather large JS files - quite often spaghetti-like with a large number of functions calling each other. New JS-heavy apps often use one or more of the JS Frameworks that exist today, e.g. Backbone, Knockout, Ember, SproutCore etc, making it more and more important to think about componentizing and structuring your code. • Web-apps today contain large amounts of JavaScript
  • 3. Dependency management is hard Top-level functions in the global namespace often lead to circular dependencies (which aren’t easily spotted - you basically need to wade through the JS code to find all dependencies). 2 Files - 5 different functions calling each other
  • 4. Dev/deploy conflict Requirements conflict bet ween development and deployment: Devs want small discrete files since it’s easier to debug and test small units. Additionally, using large files with a script tag doesn’t scale in larger apps with large number of developers: e.g. merge hell in version control Deployment should be done on large files since we don’t want the browser to load lots of files - the latency gives us slowly loaded pages
  • 5. Require tries to solve the problems with growing JS code bases by introducing the “module” abstraction. In its core, modules exposes an interface and has dependencies. You ask RequireJS for a module, and RequireJS traces and instantiates its dependencies (including transitive dependencies) before instantiating the module you wish to use. Why modules? Modules are (or, should be) small => easy to understand => easy to change + easy to test RequireJS
  • 6. Define This is a quite standard logger that every site needs. This one works with IE (where console.log throws an error if the development tools aren’t enabled). You use the define function to define a module. The API is called AMD - asyncronous module definition. The file name (modules/logger) is the name of the module. Dependencies are stated in the array in the beginning of the function call. This one has no dependencies - the array is empty The function @ param2 is a factory function that’s executed when we instantiate this module The returned object is fed as an argument to the module requiring this module as a dependency. Here, the factory returns an object containing only one function: log (msg). Note: we don’t clobber the global scope since everything in this module is hidden by the scope of the anonymous function.
  • 7. Define A module that takes t wo other modules as dependencies. The names of the dependencies are the module names, e.g. “modules/logger” from the previous slide The returned objects from the dependencies are injected into our factory function. (Logger returns an object containing the log function.)
  • 8. Require The require() call is the main entry point to the modules defined by the define() call. The first argument is an array containing all module names we wish to load. The anonymous function is a callback that’s called after the required modules and their dependencies have been loaded. The args to the function is the required modules.
  • 9. End result The different modules in the diagram have defined dependencies, a clean interface (i.e. the returned object) and are easy to unit-test.
  • 10. Firebug An image from firebug with the modules from the previous slide. Current problem: we have too many files, leading to slow loading times. Require JS optimizer to the rescue!
  • 11. Firebug (optimized) The RequireJS optimizer compiles a top-level module (in our case, “module1.js”) and its dependencies (recursively) together into one file. The file is also uglified (see example below). -> One file per top-level module with minimal footprint
  • 12. legacy JS files = large, spaghetti, global scope Introducing RequireJS with legacy JS files
  • 13. Single JS file If you are using only one js file (or you have no cross-refs bet ween different stand-alone js files): you’re in luck. • One file = one module This requires that no-one accesses the JS functions that are defined in the single JS file (e.g. literal click handlers on buttons/links etc). It’s rather uncommon, sadly. BUT: If we have this we can rather easily introduce RequireJS in the application. Let the entire file be your module as a start. Then, refactor small pieces of functionality into sub-modules as you go along.
  • 14. Interconnected files The problem here is the circular dependency bet ween file A and B at: file_a_2() -> file_b_2() -> file_a_1(). This state is rather common. There is no super-simple solution if we have these circular dependencies bet ween file A and B. This issue doesn’t only occur if we have different functions calling each other. We could also have one file and a literal click handler on a button/a-link with the function name stated in HTML. To start solving this issue, we can modularize and export only the globally used functions. (In next slide.)
  • 15. Interconnected files in the t wo highlighted rows we export the cross-ref’d functions to the global namespace. These global functions are then called at lines 9 and 13 in file A and B respectively. The next step is to refactor out file_a_1 and file_b_2 into separate modules. This is left as an excercise to the reader. The reason for extracting this is that we don’t want to use circular references bet ween modules.
  • 16. Thanks! RequireJS is Open Source, dev @ GitHub I’ll post this presentation + some samples at my • RequireJS.org blog. • Thomas Lundström, Softhouse • [email protected] • @thomaslundstrom • http://blog.thomaslundstrom.com