RequireJS modules loading out of order when loading one module into another

我怕爱的太早我们不能终老 提交于 2019-12-11 04:07:14

问题


I have written a web application which uses requirejs for AMD:

require(dependencies, function(dependencies) { 
    (function($) {
        $.fn.plugin = function(options) { return this; };
    })(window.jQuery);
});

I then used almond and r.js to compile it into a single javascript file, using the build config:

({
    baseUrl: ".",
    paths: {
        'jquery' : 'vendor/jquery-1.9.1.min'
        'almond' : "../node_modules/almond/almond"
    },
    name : "almond",
    include : "main",
    out : "plugin.js",
    wrap : true
})

Next, I want to use this plugin in another app, also based on requirejs. Here I have:

require.config({ paths: { 'jquery': 'vendor/jquery-1.9.1.min' } });
require(['jquery','plugin'], function ($) {
    $('#plugin').plugin(options);
});

However, the code loads out of order:

  1. Load jquery and plugin js files.
  2. Execute $('#plugin').plugin(options).
  3. Run (function($) { $.fn.plugin = function(options) { return this; }; })(window.jQuery);

Obviously, (2) is an error since (3) needs to be run first. The question is, why?

UPDATE 1

One proposed solution is to change the first block of code to:

define(dependencies, function(dependencies) { 
    (function($) {
        $.fn.plugin = function(options) { return this; };
    })(window.jQuery);
});

Once compiled, this becomes:

define("main", dependencies, function(dependencies) { 
    (function($) {
        $.fn.plugin = function(options) { return this; };
    })(window.jQuery);
});

Unfortunately, now the code inside this block is NEVER executed.

来源:https://stackoverflow.com/questions/17557091/requirejs-modules-loading-out-of-order-when-loading-one-module-into-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!