2013-04-29 72 views
8

我們的應用程序有許多使用通過JSP即時生成的模板的小部件。如何讓Dojo構建忽略「缺失」模板

在前端代碼中,它們包含在使用dojo /文本插件中。這可確保Widget生命週期不會在模板解析完成之前啓動,而且工作得很好。

不幸的是,當我們試圖執行我們的建設,我們得到了一個311錯誤:

error(311) Missing dependency. module: app/navigation/NavigationManager; dependency: dojo/text!/author/app/templates/NavigationManager-content.html; error: Error: text resource (/author/app/templates/NavigationManager-content.html/x) missing

我知道這裏發生了什麼,在生成過程試圖內在的字符串,但是當它去尋找它無法找到它,因此將其標記爲缺失的依賴項。

我看到了一些選擇這裏:

  1. 不知何故,告訴道場忽略這個丟失的依賴 - 這將是罰款,但我需要能夠具體,讓我得到警告到任何其他可能缺失的依賴關係
  2. 不知何故,告訴Dojo不要嘗試並內化這個模板 - 這也可以,因爲這裏沒有內部化的東西。
  3. 以某種方式,將依賴關係存根以便依賴關係解析通過,但不會發生內在化。

我見過的 internStringsSkipList 值引用,但有下列情形幫助的:

internStringsSkipList: ['/author/pepper/templates/NavigationManager-content.html'] 
internStringsSkipList: ['dojo/text!/author/pepper/templates/NavigationManager-content.html'] 
internStringsSkipList: ['/author/pepper/templates/NavigationManager-content.html/x'] 

有什麼建議?

回答

9

我遇到了完全相同的問題,在閱讀了大量的dojo文檔和源代碼後,我得出結論:如果幾乎不可能做到這一點非常困難。然而,有一個非常簡單和優雅的解決方法。不過,告訴你如何首先解決的問題,爲什麼沒有在第一位置需要一個解決辦法之前(這樣你就可以調整解決你自己的情況):

第一個問題,資源是不可發現

根據道場構建系統Reference Guide的概述部分:

[The build system] 「discovers」 a set of resources and then applies a synchronized, ordered set of resource-dependent transforms to those resources. (…) When a resource is discovered, it is tagged with one or more flags that help identify the role of that resource. (…) After a resource is discovered and tagged, the system assigns a set of transforms that are to be applied to that resource.

因此,在短期,在飛行中產生的任何資源不能由生成系統發現的,因爲他們不駐留在文件系統上。如果它們不能被發現,那麼它們不能被標記,也不能對其應用變換。特別是,resourceTags不會被調用這些資源,您不能將它們放在配置文件圖層定義的exclude列表中(比較部分Creating Builds中的層)。

順便說一句,據我明白documentation to depsScan transforminternStringsSkipList只能用於跳過資源指定使用傳統的符號(dojo.something,例如dojo.moduleUrl)。

第二個問題,插件解析器需要一個物理文件

符號dojo/text!/some/url表示使用dojo/text.js組件作爲一個插件。我發現這個音符this ticket

Every AMD plugin should have a plugin resolver in util/build/plugins and have it registered in util/build/buildControlDefault .

如果檢查util/build/plugins/text.js(如on Github),你會看到,被拋出的錯誤,因爲依賴(dojo/text!後一部分被存儲在moduleInfo)是不是在resources array:

textResource = bc.resources[moduleInfo.url]; 
if (!textResource){ 
    throw new Error("text resource (" + moduleInfo.url + ") missing"); 
} 

而這正是因爲在「發現」階段無法發現資源。

困難的解決方案

在困難的解決方案,可能會或可能無法正常工作,你需要改變如何轉型depsScan作品。基本上,當depsScan遇到dojo/text!/some/url它調用插件解析器來檢查依賴項是否存在。從depsScan documentation

Once all dependencies are found, the transform ensures all dependencies exist in the discovered modules. Missing dependencies result in an error being logged to the console and the build report.

這可能是通過重新定義transformJobs包含自定義轉換爲depsScan可能。有關更多見解,請參閱util/build/buildControlDefault.json Github)和this forum post

的簡單的解決方法

只需創建自己的插件加載資源。自己的插件將沒有註冊的插件解析器(見上面的第二個問題)和所有編譯時,你會得到的是可怕的

warn(224) A plugin dependency was encountered but there was no build-time plugin resolver.

這是我這樣的插件的例子加載一個JSON資源動態:

define(["dojo/text", "dojo/_base/lang", "dojo/json"], 
function(text,lang,json){ 
    return lang.delegate(text, { 
    load: function(id, require, load){ 
     text.load(id, require, function(data){ 
     load(json.parse(data)); 
     }); 
    } 
    }); 
}); 

它重新使用dojo/text添加其自定義加載功能。這是發佈在this dojo-toolkit forum post上的另一個例子的改編。您可以在JSFiddle上看到他們的代碼。

在項目中,我使用這樣的插件:

define(["./json!/path/to/an/json"], 
function(values){ 
    return values; 
}); 

你的插件可以只返回加載的模板而不解析它作爲JSON,只要你不指定自定義插件解析器(其中將期望文件在磁盤上物理存在)項目將編譯好。

+0

Amiramix,我有同樣的問題,我努力遵循你的答案。你介意我是否直接與你聯繫? – 2014-03-26 16:08:44

+0

當然,在我的個人資料中,你可以找到我的個人網站,在那裏你可以看到我的電子郵件(除非你想用其他方式與我聯繫?)。 – Amiramix 2014-03-27 00:44:12

0

這不是你的問題,而是給那些面臨error(311)問題的最常見的解決方案將是這樣的:

不要用斜線開始模板路徑。

壞:

"dojo/text!/app/template/widget.html" 

好:

"dojo/text!app/template/widget.html" 

你的模板路徑是不正常的,純醇」 URL。它仍然是Dojo構建的一部分,因此您使用Dojo構建路徑來訪問模板。