问题
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:
- Load jquery and plugin js files.
- Execute $('#plugin').plugin(options).
- 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