In a RequireJS environment, what's the best way to allow some AMD modules to use Lo-Dash while others simultaneously use Underscore?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With