requirejsgrunt-contrib-requirejsrequirejs-optimizer

Trouble with require js shims and r.js


I'm having a few issues with r.js I'm hoping someone can shed some light on.

Consider the following shim:

shim: {
    plugin: ['jquery'],
    plugin2: ['jquery', 'plugin']
}

And the following arbitrary plugins (note: they don't need to be jQuery plugins, but 2 must depend on 1).

Plugin 1:

(function ($) {
    $.test = function () {
        console.log('from plugin 1');
    }
})(jQuery);

Plugin 2:

(function ($) {
    $.test();
})(jQuery);

r.js will build the following:

(function ($) {
    $.test = function () {
        console.log('from plugin 1');
    }
})(jQuery);
define("plugin", function(){});

(function ($) {
    $.test();
})(jQuery);
define("plugin2", function(){});

Which is great - everything is in the correct order.

However, if I need to set

wrapShim: true

in the build config, I get this as an output:

(function(root) {
    define("plugin", [], function() {
        return (function() {
            (function ($) {
                $.test = function () {
                    console.log('from plugin 1');
                }
            })(jQuery);
        }).apply(root, arguments);
    });
}(this));

(function(root) {
    define("plugin2", [], function() {
        return (function() {
            (function ($) {
                $.test();
            })(jQuery);
        }).apply(root, arguments);
    });
}(this));

I'm not sure if I'm misreading the point of setting wrapShim to true, but shouldn't this be compiling to:

define("plugin", ["jquery"], function() ...

and

define("plugin2", ["jquery", "plugin"], function() ...

?

It appears that wrapShim is totally ignoring dependencies set in the shim.


Solution

  • This was a bug, fix tracked here: https://github.com/jrburke/r.js/issues/813