Using Lo-Dash and Underscore Syntax in RequireJS

In RequireJS, what's the best way to let some AMD models use Lo-Dash while others use Underscore at the same time?

+8
javascript lodash requirejs amd
source share
2 answers

I myself was able to solve the problem quite simply. In particular, use the lodash path for modules that require Lo-Dash and underscore for modules that need to be underlined:

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

Thus, both libraries can be used simultaneously without any interference.

Contrary to popular beliefs and claims, Lo-Dash is not an ideal replacement for Underscore.

+7
source share

The solution you already mentioned is an option (I think it’s better). I know about an alternative way, but I don’t necessarily think it is better, because it is more deceptive. You can reassign what "lodash" and "underscore" mean for different 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 can also do something like this:

 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' } } }); 

Although there are some negatives with this approach. There are some cool things. Namely, the ability to reassign dependencies of third parties can be considered a package (if this is a problem).

More details on how the map works: http://requirejs.org/docs/api.html#config-map

+5
source share

All Articles