How to mixin Underscore plugins in RequireJS?

后端 未结 3 699
自闭症患者
自闭症患者 2020-12-28 09:28

What is the right way to execute code on Underscore when it gets loaded? I am trying to execute the below code to extend the _ exported namespace automatically when modules

3条回答
  •  醉话见心
    2020-12-28 10:18

    Do you require underscorestring somewhere? Because if it isn't required, it won't be loaded. I managed to get it working with almost exactly the same code you posted:

    require.config({
        paths: {
            underscore: [
                '//raw.github.com/documentcloud/underscore/master/underscore-min'
            ,   'lib/underscore'
            ]
        ,   underscorestring: 'https://raw.github.com/epeli/underscore.string/master/dist/underscore.string.min'
        }
    ,   shim: {
            underscore: { exports: '_' },
            underscorestring: {
                deps: ['underscore'],
                init: function(_) { 
                    _.mixin(_.str.exports());
                    return _; // guess, this is not needed.
                }
            }
        }
    ,   exclude: ['underscore']
    });
    
    require(['underscore', 'underscorestring'], function(_) {
        console.log( _.chars("i'm a happy string.") );
    });
    

提交回复
热议问题