2

我们刚刚开始在我们的项目中使用 requirejs,我们很喜欢它。目前我们只是用它来管理网站运行的许多 jQuery 插件,所以几乎没有触及它的潜力……但由于时间限制,这不得不等待。

我在让优化器发挥作用时遇到了一些问题,我想知道你是否可以告诉我是否有可能做到这一点。我的目录结构:

> /common/
> /common/*.js - common plugins used on most pages on the site
> /infrequent/
> /infrequent/infrequentModule.js
> /infrequent/ckeditor
> /infrequent/ckeditor/1000s.js
> /require.js
> /jquery.js
> /main.js - this file contains all scripts in common. It looks at the page and call the necessary plugins.

优化/构建希望:

  • main.js 将包含所有常见的脚本。在大多数页面上,这个脚本将是唯一加载的(除了 require.js)
  • main.js 查看页面,如果它需要一个不常用的插件/模块,它会加载该异步。所以在一个不常见的页面上,我可能会加载 require.js、main.js 和 infrequentModule.js。

我在构建脚本中尝试了各种方法来排除和包含文件以获得所需的结果,但无济于事。有没有人有任何想法或知道这实际上是否可能?

4

1 回答 1

3

好的,所以我在谷歌组上得到了一些关于 require.js 的好答案 - http://groups.google.com/group/requirejs/browse_thread/thread/359b02473f4e707

以下是我得到的有用答案,希望可以帮助其他人……</p>

嗨,我目前正在我的一个项目中这样做,这是我的 build.js:

paths: { 
                //load libs from cdn 
                "jqueryUI": "jqueryUI", //temp static file for build 
                "swfobject": "swfobject", //temp static file for build 
                "Templates": "../Templates/Settings" 
        }, 
modules: [{ 
            name: "appName", 
                        excludeShallow: ["jqueryUI", "swfobject", "Modules/ 
module.preferences"] 
        } 

然后在我的一个模块中,我需要调用 Modules/module.preferences,当我需要它时会执行它:

function loadPreferences(){ 
 require(["Modules/module.preferences"],function(){ 
                     //Execute some init code after the module has 
loaded 
         }) 
 } 
} 

这实现了您所描述的我在页面加载时加载了 require 和 main 的地方,然后 module.preferences 在我需要时加载异步。希望有帮助,杰夫。

于 2011-03-22T11:45:06.923 回答