Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Lo-Dash and Underscore simulatenously in RequireJS environment

In a RequireJS environment, what's the best way to allow some AMD modules to use Lo-Dash while others simultaneously use Underscore?

like image 428
Donald Taylor Avatar asked Jul 14 '15 16:07

Donald Taylor


2 Answers

I was able to solve the problem myself fairly simply. Specifically use the lodash path for modules that require Lo-Dash and underscore for modules that require "underscore":

require.config({
  paths: {
    'underscore': 'path-to-my-underscore-file',
    'lodash': 'path-to-my-lodash-file'
  }
});

In this way, the two libraries can be used simultaneously without any interference.

Contrary to popular belief and claims, Lo-Dash is not a perfect drop-in replacement for Underscore.

like image 191
Donald Taylor Avatar answered Nov 14 '22 17:11

Donald Taylor


The pathing solution you already mentioned is an option (I think the better one). I know of an alternative way, but I don't necessarily think it is better since it is more deceptive. You could remap what "lodash" and "underscore" mean for various packages.

requirejs.config({
    paths: {
        'underscore': 'path-to-my-underscore-file',
        'lodash': 'path-to-my-lodash-file'
    },
    map: {
        'some/lodash_compatible_module': {
            'underscore': 'lodash'
        },
        'some/lodash_compatible_folder': {
            'underscore': 'lodash'
        },
        'some/oldmodule_or_folder': {
            'underscore': 'underscore'
        }
    }
});

If you want to create a facade you could also do something like this as well:

requirejs.config({
    paths: {
        utils: 'lodash',
        'underscore': 'path-to-my-underscore-file',
        'lodash': 'path-to-my-lodash-file'
    },
    map: {
        'some/lodash_compatible_module': {
            'utils': 'lodash'
        },
        'some/lodash_compatible_folder': {
            'utils': 'lodash'
        },
        'some/oldmodule_or_folder': {
            'utils': 'underscore'
        }
    }
});

While there are some negatives with this approach. There are some cool things. Namely being able to remap what third party dependencies may think a package is (if that is a problem).

For more info on how map works check out: http://requirejs.org/docs/api.html#config-map

like image 39
Parris Avatar answered Nov 14 '22 17:11

Parris