jam
jam copied to clipboard
jam loading lodash for backbone dependency instead of underscore
Hi, I started a new nodejs/express app and then used jam to set up a backbone app with require.js.
Here is what I had in package.json.
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app"
},
"dependencies": {
"express": "3.0.4",
"jade": "*",
"nodemon": "*"
},
"jam": {
"packageDir": "public/lib",
"baseUrl": "public",
"dependencies": {
"backbone": "*",
"jquery": "*"
}
}
}
Then I run 'jam install' and it installs backbone, jquery, and lodash (no underscore).
I tried to just use lodash but have this issue with require.js https://github.com/jrburke/requirejs/issues/557
So just to be sure everything was set up right I added lodash to the dependencies
"jam": {
"packageDir": "public/lib",
"baseUrl": "public",
"dependencies": {
"lodash": "*",
"backbone": "*",
"jquery": "*"
}
and run 'jam install' again and it installs underscore (no lodash). ???
I thought 'ok maybe it's updated the require.config.js file to use lodash as a replacement of underscore but no. It actually installed underscore along side the already installed lodash and added it to the 'packages' config object so now my app works but only because it is loading both lodash and underscore.
Any idea what is going on here?
Thanks
Another issue here is that even though jam is installing lodash, it is also installing a version of backbone that defines underscore. See the solution here. https://github.com/jrburke/requirejs/issues/557
Now maybe this is something I should know or is in documentation somewhere but I'm thinking either jam should install the other version of backbone that doesn't define underscore or it should add the map object pointing underscore to lodash when it creates the require.config.js file, or there should be some clear documented example of this specific scenario somewhere.
Thanks
I should also add that the solution makes the app work in the web browser but when I try to compile with jam it once again complains about not being able to find underscore so even if I try to include require.config.js it does not know to map underscore to lodash.
I finally figured out Jam is adding the contents of require.config.js to the bottom of require.js. Didn't see anything about that in the docs. But reqardless, even after starting over fresh, it installs lodash for backbone by default but then doesn't do anything about the fact that backbone.js is looking for underscore.
Manually adding this in require.js (not in the require.config.js) makes the app work
"map": {
"*": {
"underscore" : "lodash"
}
}
};
if (typeof require !== "undefined" && require.config) {
require.config({packages: jam.packages, shim: jam.shim, map: jam.map});
}
else {
var require = {packages: jam.packages, shim: jam.shim, map: jam.map};
}
But I still had to edit backbone.js to make jam compile work line: 12 - 15
factory(root, exports, require('lodash'));
} else if (typeof define === 'function' && define.amd) {
// AMD
define(['lodash', 'jquery', 'exports'], function(_, $, exports) {
+1 for supporting an included map
-configuration in package.json
to avoid installing two conflicting libraries.
Perhaps it's a bit backwards and really should be in the dependency config for a package. With which I mean it would be great to be able to OR
a dependency. That way, I could depend on lodash
, but if it's already present I could just as well go with underscore
.
But I think it's probably easier if I can configure it myself.
I think @tbranyen recently updated the Backbone package to use Lo-dash instead of Underscore. @tbranyen, perhaps you could look into this? ...if it's not a package-specific issue then I'll look into whether it might be a bug in Jam's dependency resolution.