1

我在使用 r.js 时遇到了一些问题,希望有人能对此有所了解。

考虑以下垫片:

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

以及以下任意插件(注意:它们不需要是 jQuery 插件,但 2 必须依赖于 1)。

插件1:

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

插件2:

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

r.js 将构建以下内容:

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

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

太好了 - 一切都按正确的顺序排列。

但是,如果我需要设置

wrapShim: true

在构建配置中,我将其作为输出:

(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));

我不确定我是否误读了将 wrapShim 设置为 true 的意义,但这不应该编译为:

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

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

?

看起来 wrapShim 完全忽略了 shim 中设置的依赖项。

4

2 回答 2

2

这是一个错误,在此处跟踪修复: https ://github.com/jrburke/r.js/issues/813

于 2015-05-29T00:07:45.530 回答
0

在撰写这篇文章时进一步检查,如果依赖项以更长的形式列出:

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

然后正确注入依赖项。

于 2015-05-18T22:46:00.440 回答