2013-04-29 20 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构建路径来访问模板。