2012-06-17 91 views
4

加載我與骨幹網和jquerymobile沿着優化我requirejs的應用程序,下面是我的文件結構:requirejs優化失敗的jQuery

/application 
    /app 
     /models 
     /views 
     /collections 
    /scripts 
     main.js 
     text.js 
     /assets 
      backbone.js 
     /libs 
      /jquery 
       /jquery.js 
       /jquery-mobile.js 
app.js 
r.js 
/public 
    /css 
     /style.css 

在控制檯中,我試圖運行到FF命令:

node ../r.js -o name=main out=../build.js baseUrl=. paths.models=../app/models paths.app=../app 

我確信,路徑明確界定,併除非這個錯誤正常工作(這是運行命令時什麼我得到):

Tracing dependencies for: main 
Error: Module loading did not complete for: jquery 
    at Function.traceDependencies (/home/dunhak/public_html/my_path/etc/application/r.js:15117:19) 

非常感謝!

+0

您是否找到解決方案? – chrisan

回答

4

其中一些將取決於您加載腳本的順序。 jQuery是在require.js之前加載的還是其他方式?

的jQuery的最後幾行可以容納線索的解決方案:

if (typeof define === "function" && define.amd && define.amd.jQuery) { 
    define("jquery", [], function() { return jQuery; }); 
} 

我的猜測是,你正在做的事情,如:

var $ = require('jquery'); //lower-case Q 

個人而言,我一直在做:

var $ = require('jQuery'); //upper-case Q 

這一切可能取決於您的require.config - 我使用類似:

require.config({ 
    baseUrl: "/js", 

    paths : { 
     jQuery: 'lib/jquery-1.7.1.min' 
     //, etc... 
    } 
}) 

另一件需要考慮的事情:您可能不希望將jQuery作爲優化輸出的一部分包含進來 - 而是從CDN(etc)加載預縮小的版本。在這種情況下,您需要按照此處所述排除jquery模塊:http://requirejs.org/docs/jquery.html

modules: [ 
    { 
     name: "main", 
     exclude: ["jquery"] 
    } 
] 
+1

這是一個偉大的幫助兄弟!我節省了很多時間。我意識到,如果我爲通過CDN託管的庫(在本例中爲jQuery)「清空」路徑,這會更好。我所做的是,我在我的命令中添加了paths.jquery = empty。非常感謝 :) –