Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone with Lodash instead of Underscore, and Browserify

I'm now trying to use Browserify, and I've got a problem with it.

I always use Backbone with Lodash instead of Underscore, so I wrote some shim scripts for Browserify:

shims/lodash.js:

'use strict';
/* global window,require,module */
require('../vendor/lodash.underscore-1.2.0');
module.exports = window._;

shims/backbone.js:

'use strict';
/* global window,require,module */
require('../vendor/backbone-1.0.0');
module.exports = window.Backbone;

app.coffee:

'use strict'
$ = require './shims/jquery'
_ = require './shims/underscore'
Backbone = require './shims/backbone'

I actually use grunt-coffeeify to build the Browserify modules, and it says an error below:

Running "coffeeify:source" (coffeeify) task
Warning: module "underscore" not found from "/Users/User/proj/src/js/vendor/backbone-1.0.0.js" Use --force to continue.

What should I change to work Backbone properly? Thanks in advance.

UPDATE

Somehow, it works with codes below:

shims/lodash.js:

'use strict';
/* global require,module */
module.exports = require('../vendor/lodash-1.2.0');

shims/backbone.js:

'use strict';
/* global window,require,module */
window.$ = require('./jquery');
window._ = require('./lodash');
module.exports = require('../vendor/backbone-1.0.0');

And comment out code below in backbone-1.0.0.js:

//if (!_ && (typeof require !== 'undefined')) _ = require('underscore');

This looks something wrong though...

like image 826
Japboy Avatar asked Apr 17 '13 10:04

Japboy


People also ask

Can I replace underscore with Lodash?

1, lodash published a "Underscore compatible" version. You can use that as a drop-in replacement.


1 Answers

b is an instance of browserify.

b.require('lodash', {expose: 'underscore'});

for jQuery, look at my version on github : https://github.com/amiorin/jquery

like image 103
Alberto Miorin Avatar answered Oct 17 '22 13:10

Alberto Miorin