r.js: Understand requirejs's r.js optimizer
Asked Answered
M

1

0

im trying to figure out if its a good idea to use r.js from require.js to generate a build.js file out of my javascript files to only load one file on production. But im wondering if this is a good idea in my case as it seems its simply loading absolutely everything into the DOM using up a lot of memory. Maybe i got it wrong.

Look at one of my backbone based views i implemented using require.js. As i know, r.js will take this file and optimize and put it into the final build.js file with all the others views, models and collections defined the same way.

define(function (require) {
  "use strict";

  var $                   = require('jquery'),
      _                   = require('underscore'),
      Backbone            = require('backbone'),
      tpl                 = require('text!tpl/start.html'),
      Mustache            = require('mustache'),
      template            = Mustache.compile(tpl);

  return Backbone.View.extend({
    initialize: function () {
    },

    render: function () {
      this.$el.html(template());
      return this;
    },

    events: {
      'click #submit': "start"
    },

    start: function (event) {
       // stuff to do ...
    }
  });
});

I guess the final build.js will simply take my code above and concat them into it, right? So this means when i update my index.html loading require.js then my build.js file, it will simply load all the views, models and collections into the DOM. Doesn't take that up too much memory, since i don't need everything to be loaded from the beginning. Does it make sense to call require('path/to/my/view') when its loaded already via the build.js file?

Mononucleosis answered 9/12, 2013 at 11:34 Comment(1)
By default (unless you set findNestedDependencies to true) r.js only includes modules that would be required on startup anyway. The only difference would be that these resources would be loaded in a single HTTP request, as opposed to one request per module.Jallier
R
2

It might indeed not make sense for your application to optimize all modules into one single file. r.js supports this kind of scenario. What you have to do is use the modules option and define as many output files as you need. A generic illustration of this could be:

modules: [
    {
        name: 'main', 
        exclude: [ "viewA", "viewB" ],
    },
    {
        name: 'viewA',
        exclude: ['core']
    },
    {
        name: 'viewB',
        exclude: ['core']
    }
]

The setup above assumes that the source to be optimized has modules named main, viewA and viewB. It would create 3 optimized bundles: a core that contains all core modules that would be loaded in any case, viewA which represents a group of modules to be loaded in circumstances so and so, and viewB which represents a group of modules to be loaded in other circumstances.

How you should setup your modules parameter depends on the specifics of your application.

Roentgenograph answered 9/12, 2013 at 11:53 Comment(1)
didn't know about that one. that gives me more "power" of how to get my file optimized. thanks.Mononucleosis

© 2022 - 2024 — McMap. All rights reserved.