【问题标题】:Using the optimizer in requirejs to group and load files在 requirejs 中使用优化器对文件进行分组和加载
【发布时间】:2011-03-18 19:01:51
【问题描述】:

我们刚刚开始在我们的项目中使用 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。

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

【问题讨论】:

    标签: javascript requirejs


    【解决方案1】:

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

    以下是我得到的有用答案,我希望可以帮助其他人......

    嗨,我目前正在我的一个项目中这样做,这是我的 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 
             }) 
     } 
    } 
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      相关资源
      最近更新 更多