简体   繁体   English

在RequireJS配置中预定义AMD模块依赖性

[英]Predefining AMD module dependencies in RequireJS config

For the sake of loading times, I'm interested in predefining all AMD module dependencies. 为了加载时间,我对预定义所有AMD模块依赖项感兴趣。 This is because at the moment, the module file must load before require.js can work out its dependencies. 这是因为目前,必须先加载模块文件,然后require.js才能确定其依赖性。 Here's an illustration to show what I mean: 这是说明我的意思的例证: 在此处输入图片说明

Is there any way to do this with require.js? 有什么办法可以做到require.js吗? I know you can define dependencies for the shimmed modules, but can you do this for your own custom AMD modules? 我知道您可以为填充模块定义依赖关系,但是您可以为自己的自定义AMD模块执行此操作吗?

You are looking for something you can put in the configuration you pass to RequireJS that would do what you want. 您正在寻找可以放在传递给RequireJS的配置中的功能,该功能可以完成您想要的操作。 There is no analogue to shim for modules that call define . 对于调用define模块, shim没有类似物。 However, what you could do is add the deps option to your configuration: 但是,您可以做的是在您的配置中添加deps选项:

deps: ['module', 'dep1', 'dep2', 'dep3']

This will tell RequireJS to start loading immediately your module and the dependencies. 这将告诉RequireJS立即开始加载您的模块和依赖项。 You will have to maintain this list yourself but this would be true with shim too. 您将必须自己维护此列表,但对于shim也是如此。

Otherwise, you can do what kryger suggested in a comment : use r.js to build module into a single bundle that contains it and all of its dependencies. 否则,您可以执行kryger在注释中建议的操作:使用r.jsmodule构建到包含该module及其所有依赖项的单个包中。 Whenever module is loaded, all of its dependencies are loaded at the same time. 每次加载module ,都会同时加载其所有依赖项。 This is more efficient than using deps but might make things slightly more complicated if you ever need to load any of the dependencies by themselves. 这比使用更高效的deps但可能使事情变得复杂一些 ,如果你需要自行加载任何的依赖关系。 You'd have to use the runtime option bundles to tell RequireJS where these modules are. 您必须使用运行时选项bundles来告诉RequireJS这些模块在哪里。 And just like deps you'd have to maintain this list yourself. 就像部门deps一样,您必须自己维护此列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM