underscore.string icon indicating copy to clipboard operation
underscore.string copied to clipboard

AMD define call does not work with RequireJS

Open jordandh opened this issue 14 years ago • 12 comments

It is possible this does not work with other amd libraries.

Passing null as the second parameter to define does not define the module with RequireJS. The following works for me in RequireJS 1.0.8 and RequireJS 2.0.1:

define('underscore.string', _s);

jordandh avatar Jun 06 '12 01:06 jordandh

I am also running into the same problem. Can we get a confirm whether or not there will be a fix? Require JS 1.0.8.

I think the problem is because of then null used in the define. (RequireJS 1.0.8)

define = function (name, deps, callback) {
     var node, context;
     //Allow for anonymous functions

    if (typeof name !== 'string') {
        //Adjust args appropriately
        callback = deps;
        deps = name;
        name = null;
    }

    //This module may not have dependencies
    if (!isArray(deps)) {
        callback = deps;
        deps = [];
    }

...

If you look, since null is not an array, it is basically assuming null is the callback. Should change to use define(function() { }); or define("underscore.string", [], function() {});

adrian-chang avatar Jun 09 '12 03:06 adrian-chang

I guess this fix is not uploaded to the official website. Look: http://epeli.github.com/underscore.string/lib/underscore.string.js

I took a time to figure out what was happening.

wlepinski avatar Jul 13 '12 15:07 wlepinski

I use requirejs 2.02 and shim.. but can't make the package to load..? Any idea?

require.config({
    baseUrl:'.',
    paths: {
        underscore:         'underscore-min',
        underscore_string:  'underscore.string.min'
    },
    shim: {
        underscore_string: {
            deps: ['underscore'],
            exports: '_s'
        },
        underscore: {
            exports: '_'
        }
    }
});


define(['underscore',  'underscore_string'], function () {  
    console.log ('See if we can use _s', _s)
});

cardinal27513 avatar Jul 18 '12 02:07 cardinal27513

I'm having the same issue…I can't seem to get it to load.

miwillhite avatar Jul 26 '12 22:07 miwillhite

I just got it working by removing the name argument in the AMD define function. So instead of:

define('underscore.string', [], function() { ... })

Just do:

define([], function() { ... })

Then I don't even need the shim, I can just require it as a dependency in my module.

miwillhite avatar Jul 26 '12 22:07 miwillhite

Instead of shimming you can use Burke's amd version of underscore here https://github.com/amdjs/underscore

jordandh avatar Jul 26 '12 22:07 jordandh

I'm actually thinking about removing ADM declaration altogether. Underscore removed it and so I think we should too.

If you want to use AMD, you can always wrap the code by yourself. That shouldn't be very hard.

What do you think, guys? /cc @epeli @edtsech

rwz avatar Jul 27 '12 06:07 rwz

I have the same problem as @cardinal27513, i don't know, how to make underscore.string work as either _s or _.string. I mentioned the problem here: http://stackoverflow.com/questions/16053016/underscore-string-with-requirejs

sindikat avatar Apr 17 '13 17:04 sindikat

Unfortunately this is still broken

gone avatar May 06 '13 22:05 gone

Bit late here, but... I'm all in favor of dropping AMD like Underscore.js did. Shimming is in my experience the most robust way to use third party libraries with RequireJS.

esamattis avatar May 14 '13 16:05 esamattis

:+1 to dropping AMD. It's broken right now with RequireJS (particularly when using the optimizer).

evanstern avatar May 15 '13 15:05 evanstern

@rwz I don't see why you need to drop AMD support. I'm having the same problem here but like @miwillhite mentioned it works fine by removing the 'underscore.string' name. Why not just remove the name and leave the line in place for those that want AMD support?

jwarkentin avatar May 22 '13 19:05 jwarkentin