我們有一個大規模的JavaScript應用程序,我們試圖使用grunt-contrib-requirejs進行連接和縮小。我們使用Aura框架。我們使用bower將其他存儲庫中的依賴關係引入我們的主應用程序。requirejs優化器 - 需要幫助優化我們的應用程序
這裏是我們應用結構
- app/
|
|_ main.js
|_ index.html
|_ css
|_ bower_components/
|_ core/
|_ widget1/
|_ main.js
|_ views/
|_ models/
|_ widget2/
|_ extension1/
|_ extension2/
|_ other third party libraries, etc
- Gruntfile.js
- bower.json
main.js:
requirejs.config({
shim: {
...
},
paths: {
'core': 'bower_components/core/app/main',
'aura': 'bower_components/aura/dist/',
'jquery': 'bower_components/jquery/jquery',
'backbone': ...,
... lots of other paths
}
});
define(['core', 'jquery', 'jqueryui'], function(core, $) {
// Here we start Aura, which finds our extensions and widgets in
// bower_components
});
我們目前requirejs任務配置:
requirejs: {
compile: {
options: {
mainConfigFile: 'app/main.js',
name: 'main',
include: ['bower_components/widget1/main', 'bower_components/widget2/main',
'bower_components/extension1/main', 'bower_components/extension2/main'],
exclude: ['jquery'],
insertRequire: ['main'],
out: 'dist/app.js'
}
}
}
這個連接我們的app/main及其依賴關係,但是當我們嘗試運行它時,我們得到的錯誤如下:
GET404(Not Found) 儘管下劃線是我們包含的許多小部件的依賴關係。
我們已經在the r.js examples中廣泛嘗試了不同的選項,並通讀了許多試圖找到答案的疊層流問題。
我們希望有關如何使用這種結構建設成爲一個縮小的文件,這樣的建議:
更新#2:正確的文件結構
- dist/
|_ index.html // copied using grunt copy
|_ css // copied using grunt copy
|_ app.js // Built with grunt requirejs
UPDATE
我們已經在我們的墊片包括underscore
其中修正了上述錯誤,但我們仍然收到另一個錯誤:
Failed to load resource: the server responded with a status of 404 (Not Found)
http://my-machine.com:8080/bower_components/core/app/main.js
這包括在最小化的文件,所以我不明白爲什麼它無法找到該文件:
define("bower_components/core/app/main.js", ["aura/aura", "bootstrap"], function(){
...
});
更新#3
的定義從上面通過優化工具生成的文件來!最初定義該模塊,core/app/main.js
,看起來像:
define(['aura', 'bootstrap'], function(Aura) {
...
});
你有沒有在墊片中加下劃線? – Evgeniy
可以請你更新你的代碼結構的描述和共享構建的文件 – Evgeniy
順便說一句,我已經使用了一個技巧來澄清與路徑 - 構建後,我已經在頁面require.config({baseUrl:'/路徑/到/ scripts'})。無論如何,爲了幫助你,我需要你分享建立的文件+完整的文件結構。 – Evgeniy