2014-12-02 61 views
0

我是新來r.js優化但requirejs變量需要路徑不r.js工作

集結config.js風扇

({ 
    appDir: "./src/main/webapp/webresources/javascript/", 
    baseUrl: "./", 
    dir: "./target/webresources/js", 

    optimizeCss: "none", 
    mainConfigFile: "./src/main/webapp/webresources/javascrip/app.js", 

    inlineText: true, 
    removeCombined: true, 

    modules: [ 
    { 
     name: "app", 
    } 
    ] 
}) 

app.js看起來像

if (typeof _JSHome=== "undefined") { 
    _JSHome = "/webresources/javascript/edulastic"; 
} 

if (typeof EditorValue === "undefined") { 
    EditorValue = ""; 
} 

require.config({ 

    baseUrl: '/webresources/javascript', 
    waitSeconds: 180, 
    paths: { 
     tpl     : _JSHome+'/js/tpl', 
     underscore    : 'underscore-min', 
     backbone    : 'backbone-min', 
     text     : 'text/text', 
     jquery     : 'jquery', 
     jqueryuitouchpunch  : "jquery.ui.touch-punch", 
     modernizr    : 'modernizr', 
     hammer     : 'hammer.min', 
     detectizr    : 'detectizr', 
     bootstrap    : _edulasticJSHome+'/bootstrap/js/bootstrap.min', 
     fastClick    : "mobileutils/fastclick/fastclick.min", 
     asMetaData    : _JSHome+'/js/app/metaData', 
     highCharts    : '/webresources/javascript/highcharts/highcharts-min', 
    }, 
}); 

當我運行 r.js -o build-config.js在我的項目的根目錄,我發現了以下錯誤:

Try only using a config that is also valid JSON, or do not use mainConfigFile and instead copy the config values needed into a build file or command line arguments given to the optimizer.

Source error from parsing: e:/mongrel_mpq/src/main/webapp/webresources/javascript/app.js: > ReferenceError: _JSHome is not defined

重複,但沒有解決之道 - Require.js optimizer and variables in paths

+0

糾正我,如果我錯了,但如果_JSHome沒有聲明,不會試圖分配它在未定義的檢查拋出該錯誤,因爲變量尚未被聲明? – kingdamian42 2014-12-02 17:44:09

+0

如果是抱怨有效的json,請嘗試刪除「name:」app「後面的逗號,」 – vernak2539 2014-12-02 17:56:24

回答

1

app.js具有的配置是計算配置。例如,您將_JSHome設置爲一個值,然後在配置中使用它。在運行時,這沒有問題。但是,r.js不是設計爲執行什麼您傳遞給mainConfigFile。它所做的是查找傳遞給require.config(或requirejs.config)並使用JSON對象的JSON對象。這就好像r.js將進入您的app.js文件並複製require.config(...)中的所有文本,然後將其粘貼到其自己的執行上下文中。當它嘗試使用它從您的app.js文件中捕獲的內容時,會出現_JSHome未定義的錯誤,因爲在其中r.js解釋您的配置,_JSHome未定義的上下文中。

簡單的解決方案是有一個不計算的配置。但是,如果您需要計算配置,則要獲得r.js以使用它,則需要在r.js看到它之前計算它。這可以作爲構建系統的一部分完成(使用Grunt,Gulp,make等)。

+0

可以請您發佈這樣的解決方案嗎?我猜想這可能是問題所在,但不能告訴r.js一些變量。 – 2014-12-02 18:14:32