Here is my situation using Backbone and Handlebars with Requirejs.
I follow the style of the CommonJS style definition, because I was more comfortable with it:
define(function(require) { var Backbone = require('Backbone') var Item = require('model/item')
And this is my requirejs configuration:
require.config({ baseUrl: "/javascripts/", paths: { jquery: 'components/jquery/jquery', underscore: 'components/underscore/underscore', backbone: 'components/backbone/backbone', handlebars: 'components/handlebars/handlebars', text: 'components/text/text' }, shim: { underscore: { exports: "_" }, handlebars : { exports: "Handlebars" }, backbone: { deps: ['underscore', 'jquery'], exports: 'Backbone' } } });
Before optimization, everything works smoothly, no problems arise.
But after optimization with r.js dependencies seems to break. I would like to use Almond js in production, so here is my build file:
({ baseUrl: ".", paths: { jquery: "components/jquery/jquery", underscore: "components/underscore/underscore", handlebars: "components/handlebars/handlebars", backbone: "components/backbone/backbone", text: "components/text/text" }, // we use almond minimal amd module loader name: "components/almond/almond", // the application entry point include: ['app/init'], // we need to teel almond to require app/init insertRequire: ['app/init'], out: "main.js", cjsTranslate: true, wrap: true, optimize: "none" })
Now, when I run optimized javascript in the browser, all I get is error messages telling me that jQuery and Handlebars are undefined (nor Backbone.$ , Of course).
A simple workaround was to force jQuery to load and assign it a Backbone, for example:
var $ = require('jQuery') var Backbone = require('Backbone') Backbone.$ = $
But that sounds very stupid and redundant to me. I feel like I'm doing something wrong, but I canβt understand that.
After optimization, Handlebars do not load as dependencies. If I force download (as in jQuery), an error message appears during the build process saying that the fs module (npm package) could not be found.
I googled, but found only this topic in Google groups ( https://groups.google.com/forum/?fromgroups=#!topic/requirejs/lYwXS-3qjXg ), which seems to be related to my problem, even if suggested solutions do not work at all.