RequireJS优化器是否在没有“app.js”的情况下以及依赖于内联模块的模块上工作?

时间:2013-02-04 18:14:50

标签: javascript requirejs

在RequireJS示例中,它显示您可以从脚本标记中引用app.js(或任何您想要调用的文件):

<script data-main="js/app.js" src="js/require.js"></script>

由于我无法控制的原因,我不能这样做。在模板层中生成了几个需要保留的动态变量。因此,我创建了一个内联的“配置”模块,其他模块可以读取。

<script type="text/javascript">
        define('config', function() { 
            return {
                markup_id: {
                    "content": "search",
                    "page": "index",
                    "media": "mobile"
                },
                page_context: {
                    "siteconfig": {
                        "mobile_video_player_id": /* */,
                        "mobile_video_player_key": /* */,
                        "mobile_ad_site": /* */,
                        "omniture_mobile_env": /* */,
                        "searchserver": /* */,              
                    },
                    "omniture": {
                        "gn": /* */,

                    }
                }
            }
        });
    </script>       

我所做的是为每个模板,我放置了一个内联的require.config。作为示例(删除了特定路径信息):

<script type="text/javascript">
/* This code is on a template page inside a script tag. */
require.config({
            baseUrl: /* */,
            paths: {
                'jquery': /* */,
                'jquery-mobilead': /* */,
                'jquery-photogalleryswipe': /* */
            },
            /* Enforce ordering of jQuery plugins - which require jquery */
            shim: {
                'jquery-mobilead': {
                    deps: ['jquery'],
                    exports: 'jQuery.fn.mobileAd'
                },
                'jquery-photogalleryswipe': {
                    deps: ['jquery'],
                    exports: 'jQuery.fn.photoGallerySwipe'
                },
                'gallery': {
                    deps: ['jquery-photogalleryswipe', 'jquery-mobilead']
                }
            },
            urlArgs: 'buildlife=@buildlife@'
        });

        require( ['jquery', 'site', 'gallery', 'jquery-photogalleryswipe', 'jquery-mobilead'], function($, site, gallery) {
                //This function will be called when all the dependencies
                //listed above are loaded. Note that this function could
                //be called before the page is loaded.
                //This callback is optional.

                /* Initialize code */
                $(document).ready(function() {
                    /* sitewide code - call the constructor to initialize */
                    site.init();
                    /* homepage contains a reference to a function - execute the function */
                    gallery.initGallery();
                });
            }
        );
</script>

我认为优化器无法优化模板中的代码。

然而我根据RequireJS API文档确实有模块JS文件     /modules/gallery.js     /modules/channel.js     /modules/site.js     / *等* /

这些模块与其他模块有依赖关系,但这些模块依赖于'config'模块,该模块与模板一起定义。如果我针对这些文件运行优化器,优化器是否会正常工作,因为其中一个模块config,in是模板?

1 个答案:

答案 0 :(得分:0)

我认为我基于阅读解决了自己的问题 How to load bootstrapped models in Backbone.js while using AMD (require.js)

我已将模板变量重组为require全局对象,并在require.js之前将其声明。现在,模板仍然可以生成这些值,但由于require.config和require代码可以放入外部JS文件,因此优化器应该能够立即查看这些文件。我还没有尝试过运行优化器。

<script>
        /* This script block is in the template */
        var require = {
            config: {
                markup_id: {
                "content": "search",
                "page": "index",
                "media": "mobile"
                },
                page_context: {
                   "siteconfig": {
                    "mobile_video_player_id": /* */,
                    "mobile_video_player_key": /* */,
                    "mobile_ad_site": /* */,
                    "omniture_mobile_env": /* */,
                    "searchserver": /* */,              
                  },
                "omniture": {
                    "gn": /* */,

                }
            }
        };
    </script>
 <script data-main="/m/j/main-search" src="/m/j/require.js"></script>
相关问题