当前位置 : 主页 > 网页制作 > Dojo >

如何告诉Dojo构建忽略“缺失”模板

来源:互联网 收集:自由互联 发布时间:2021-06-15
我们的应用程序有许多小部件,它们使用通过JSP动态生成的模板. 在前端代码中,使用dojo / text插件包含它们.这可以确保在模板解析完成后,Widget生命周期不会启动,并且工作正常. 不幸的是
我们的应用程序有许多小部件,它们使用通过JSP动态生成的模板.

在前端代码中,使用dojo / text插件包含它们.这可以确保在模板解析完成后,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

我理解这里发生了什么,构建过程试图内化字符串,但是当它去查找它时,它无法找到它,因此将其标记为缺少依赖项.

我在这里看到了很多选择:

>不知何故,告诉Dojo忽略这个缺失的依赖 – 这没关系,但我需要能够具体,以便我得到任何其他可能缺失的依赖的警报
>不知何故,告诉Dojo不要尝试内化这个模板 – 这也没关系,因为这里没有任何内化.
>不知何故,将依赖关系存根,以便依赖关系解析通过,但内部化不会发生.

我见过参考文献
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']

有什么建议?

我遇到了完全相同的问题,在阅读了大量的dojo文档和源代码后,我得出的结论是,如果几乎不可能做到这一点很困难.然而,有一个非常简单和优雅的解决方法.但在告诉您如何首先解决问题之前,为什么首先需要解决方法(因此您可以根据自己的情况调整解决方案):

第一个问题,资源是不可发现的

根据dojo构建系统Reference Guide的Overview部分:

[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,也不能将它们放在配置文件图层定义的排除列表中(比较Creating Builds中的图层部分).

顺便说一句,据我所知documentation to depsScan transform,internStringsSkipList只能用于跳过使用遗留表示法指定的资源(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中)不在资源数组中:

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.js(on Github)和this forum post.

简单的解决方法

只需创建自己的插件即可加载资源.你自己的插件不会注册插件解析器(参见上面的第二个问题),你编译的所有内容都是可怕的

warn(224)遇到插件依赖,但没有构建时插件解析器.

这是我动态加载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,只要你没有指定你的自定义插件解析器(它希望文件在物理上存在于磁盘上),项目就可以正常编译.

网友评论